您的位置:首页 > 移动开发 > Android开发

android两个时间比对工具类

2016-07-09 19:41 323 查看
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* 短信列表时间判断类
* Created by xing on 2016/7/9.
*/
public class TimeUtil {

/**
* 获取系统时间
* @return String 类型的系统时间
*/
public static String getSystemTime() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
String str = formatter.format(curDate);
return str;
}

/**
* 短信列表时间判断  判断类型为年月日
* @param time
* @return
*/
public static String CompareYear (String time) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
String str = formatter.format(curDate);
//W截取年
time.substring(0,4);
str.substring(0,4);
if (!time.substring(0,4).equals(str.substring(0,4))){
return time;
} else {
//月
time.substring(5,8);
str.substring(5,8);
if (! time.substring(5,8).equals(str.substring(5,8))) {
return time.substring(5,7)+"月"+time.substring(8,10)+"日";
} else {
//日
time.substring(8,11);
str.substring(8,11);
if (!time.substring(8,11).equals(str.substring(8,11))) {
int a = Integer.parseInt(str.substring(8,10));
int b = Integer.parseInt(time.substring(8,10));
int c = a -b;
if (c <= 6) {
Calendar calendar = Calendar.getInstance();//获得一个日历
calendar.add(calendar.DAY_OF_WEEK, +0);
int number = calendar.get(Calendar.DAY_OF_WEEK);//星期表示1-7,是从星期日开始,
String[] strs = {"", "周日", "周一", "周二", "周三", "周四", "周五", "周六",};
if (number == 1) {
number = 8;
}
return strs[number -c];
}
return time.substring(8,10)+"日";
} else {
return time.substring(11,17);
}
}
}
}

/**
* 短信列表时间判断  判断类型为月日
* @param time
* @return
*/
public static String CompareMouth (String time) {
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
Date curDate = new Date(System.currentTimeMillis());
String str = formatter.format(curDate);
if (time.substring(0,2).equals(str.substring(0,2))) {
int a = Integer.parseInt(str.substring(3, 5));
int b = Integer.parseInt(time.substring(3, 5));
int c = a - b;
if (c <= 6) {
Calendar calendar = Calendar.getInstance();//获得一个日历
calendar.add(calendar.DAY_OF_WEEK, +0);
int number = calendar.get(Calendar.DAY_OF_WEEK);//星期表示1-7,是从星期日开始,
String[] strs = {"", "周日", "周一", "周二", "周三", "周四", "周五", "周六",};
if (number == 1) {
number = 8;
}
return strs[number - c]+ time.substring(5);
}
}
return time.substring(0,2)+"月"+time.substring(3,5)+"日"+time.substring(5);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 时间 比较 短信