今天把几个有关日期格式化的通用格式记录一下,以前总是用到了才去找,太麻烦了。
其实总的来说,我们可能遇见的也就是几种形式:
1.直接显示一个格式的日期
2.拿到一个格式的日期转换成另外一种
3.其他常用的方式
长时间使用发现操作也挺简单,就是容易忘记了。
直接格式化使用:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.format(new Date());//格式化日期为yyyy-MM-dd 的格式,这里的format的参数是一个Date对象。
先解析再重新格式化:
这个情况出现在我们拿到了一个字符串的时间,要求显示成另外的一种格式。
如拿到了2017-03-01 12:23:20 这个时间,要求显示为:3月1日
String time = "2017-03-01 12:23:20";
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy年MM月dd");
SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM月dd hh时mm分");
@Test public void test2(){ try { String time = "2017-03-01 12:23:20"; Date date = simpleDateFormat1.parse(time); String format = simpleDateFormat2.format(date); String format1 = simpleDateFormat3.format(date); Logger.e(TAG,format); Logger.e(TAG,format1); } catch (ParseException e) { e.printStackTrace(); } }
结果是:
这个使用方式基本上可以转换我们需要的各种格式。
还有一个人为的转换,要求我们把一个时间转换成如:
1分钟前,2小时前,1天前这种。
这个没有找到好的方法,目前可以通过手动的方式实现:
public static String getTimeDes(String time) {
String result = time;
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = df.parse(time);
long dateTimeStamp = date.getTime();
long minute = 1000 * 60;
long hour = minute * 60;
long day = hour * 24;
long halfamonth = day * 15;
long month = day * 30;
long now = new Date().getTime();
long diffValue = now - dateTimeStamp;
if (diffValue < 0) {
// toast("结束日期不能小于开始日期!");
}
long monthC = diffValue / month;
long weekC = diffValue / (7 * day);
long dayC = diffValue / day;
long hourC = diffValue / hour;
long minC = diffValue / minute;
if (monthC >= 1) {
result = date.getMonth() + 1 + "月" + date.getDate() + "日";
} else if (weekC >= 1) {
result = date.getMonth() + 1 + "月" + date.getDate() + "日";
} else if (dayC > 1) {
result = date.getMonth() + 1 + "月" + date.getDate() + "日";
} else if (dayC == 1) {
result = "昨天 " + date.getHours() + ":" + date.getMinutes();
} else if (hourC >= 1) {
result = Integer.parseInt(hourC + "") + "小时前";
} else if (minC >= 1) {
result = Integer.parseInt(minC + "") + "分钟前";
} else {
result = "刚刚";
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
还有一个可能能用到的时间转换,在做播放视频,音频时格式化,我们知道一个毫秒的时间值,需要转换成hh:mm:ss这样的格式。
如我们可能知道一个视频当前播放时间是:123000毫秒
/**
* 把秒转换成:12:20:30这里形式
*
* @param timeMs
* @return
*/
private static String formatSecond2Time(int timeMs) {
int seconds = timeMs % 60;
int minutes = (timeMs / 60) % 60;
int hours = timeMs / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
/**
* 把毫秒转换成:1:20:30这里形式
*
* @param timeMs
* @return
*/
public static String formatMillisecond2Time(int timeMs) {
int totalSeconds = timeMs / 1000;
return formatSecond2Time(totalSeconds);
}
实例:
结果:@Test public void test3(){ int time = 1234000; String ss = DateUtils.formatMillisecond2Time(time); Logger.e(TAG,"格式化后:"+ss); }
以上几种简单的操作做一个记录,方便后续使用。