1、String转Date
String str = "1957-3-23 00:00:00"; ?
//1、定义转换格式 ?
SimpleDateFormat formatter? = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ?
//2、将str转化为date类型? 输出为:Sat Mar 23 00:00:00 GMT+08:00 1957 ?
Date? date = formatter.parse(new Date()); ?
2、Date转String
//1、定义转换格式 ?
SimpleDateFormat formatter? = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ?
//2、将当前时间转化为String类型
String dateString = formatter.format(new Date());
3、获取N分钟后的时间
Date now = new Date();
Date beforeDate = new Date(now.getTime() - 1000*60*timeRange); //timeRange分钟前的时间
4、获取时间差
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
String fromDate = simpleFormat.format("2016-05-01 12:00"); ?
String toDate = simpleFormat.format("2016-05-01 12:50"); ?
long from = simpleFormat.parse(fromDate).getTime(); ?
long to = simpleFormat.parse(toDate).getTime(); ?
int minutes = (int) ((to - from)/(1000 * 60)); ?
5、时间格式
yyyy:年 ?
MM:月 ?
dd:日 ?
hh:1~12小时制(1-12) ?
HH:24小时制(0-23) ?
mm:分 ?
ss:秒 ?
S:毫秒 ?
E:星期几 ?
D:一年中的第几天 ?
F:一月中的第几个星期(会把这个月总共过的天数除以7) ?
w:一年中的第几个星期 ?
W:一月中的第几星期(会根据实际情况来算) ?
a:上下午标识 ?
k:和HH差不多,表示一天24小时制(1-24)。 ?
K:和hh差不多,表示一天12小时制(0-11)。 ?
z:表示时区?? ?