import android.text.TextUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTimeUtils {
private static final SimpleDateFormat yyyyMMddHHmm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
private static final SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat yyyyMMddTHHmmssSSS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
static final SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static final SimpleDateFormat dateFormater5 = new SimpleDateFormat("yyyy年MM月dd日");
static final SimpleDateFormat dateFormater6 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
static final SimpleDateFormat dateFormater7 = new SimpleDateFormat("MM月dd日 HH:mm");
static final SimpleDateFormat dateFormater8 = new SimpleDateFormat("HH:mm");
public static String localToService(Date date) {
return yyyyMMddTHHmmssSSS.format(date);
}
public static String getFriendlyTime(Date date) {
if (date == null) {
return "";
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return getFriendlyTime(calendar);
}
public static String getFriendlyTime(Calendar serverCalendar) {
if (serverCalendar == null) {
return "";
}
Calendar localCalendar = Calendar.getInstance();
int serverYear = serverCalendar.get(Calendar.YEAR);
int serverMonth = serverCalendar.get(Calendar.MONTH);
int serverDay = serverCalendar.get(Calendar.DAY_OF_MONTH);
int localYear = localCalendar.get(Calendar.YEAR);
int localMonth = localCalendar.get(Calendar.MONTH);
int localDay = localCalendar.get(Calendar.DAY_OF_MONTH);
if (serverYear < localYear) {
return dateFormater6.format(serverCalendar.getTime());
} else if (serverYear == localYear && serverMonth < localMonth) {
return dateFormater7.format(serverCalendar.getTime());
} else if (serverYear == localYear && serverMonth == localMonth && serverDay < localDay) {
if (localDay - serverDay == 1) {
return "昨天 " + dateFormater8.format(serverCalendar.getTime());
} else if (localDay - serverDay == 2) {
return "前天 " + dateFormater8.format(serverCalendar.getTime());
} else {
return dateFormater7.format(serverCalendar.getTime());
}
} else if (serverYear == localYear && serverMonth == localMonth && serverDay == localDay) {
long hour = (localCalendar.getTimeInMillis() - serverCalendar.getTimeInMillis()) / 3600000;
if (hour == 0) {
long minute = (localCalendar.getTimeInMillis() - serverCalendar.getTimeInMillis()) / 60000;
if (minute < 1) {
return "刚刚";
} else {
return minute + "分钟前";
}
} else if (hour == 1) {
return hour + "小时前";
} else {
return "今天 " + dateFormater8.format(serverCalendar.getTime());
}
}
return dateFormater6.format(serverCalendar.getTime());
}
/**
* @param date
* @return
*/
public static Date serviceTypeToDate(String date) {
if (TextUtils.isEmpty(date)) {
return null;
}
try {
if (!date.contains(".")) {
date = date + ".000";
}
return yyyyMMddTHHmmssSSS.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* @param date
* @return
*/
public static String serviceTypeToLocal2(String date) {
String str = "";
try {
if (!date.contains(".")) {
date = date + ".000";
}
Date date2 = yyyyMMddTHHmmssSSS.parse(date);
str = yyyyMMddHHmm.format(date2);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
}
DateTimeUtils
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 版权信息 文章版权更好时代原创出品 执笔作者杨一方(已签约) 正文字数1204字 阅读时间4分钟 首发日期2017...
- 1、选择限制 “X”,“Y”,“Z”三个轴图标以及“视觉坐标”全部点亮时,我们可以对模型进行任意坐标方向的移动,缩...