醉过才知酒浓,爱过才知情重,来过才知错与过,你不能做我的诗,正如我不能做你的梦!
public class timeFormatConversion {
public static void main(String[] args) {
//1:系统当前时间, 返回类型是long
long currentTime = System.currentTimeMillis();
System.out.println(“currentTime:”+currentTime);
//date类型
Date nowDate = new Date();
System.out.println(“date1:”+nowDate);
//格式可以自己随意定义的字符串
SimpleDateFormat sdf1 = new SimpleDateFormat(“yyyyMMdd”);
SimpleDateFormat sdf2 = new SimpleDateFormat(“hh:mm”);
SimpleDateFormat sdf3= new SimpleDateFormat(“yyyy-MM-dd hh:mm”);
String date = sdf1.format(currentTime);
System.out.println("date:"+date);
String time = sdf2.format(currentTime);
System.out.println("time:"+time);
String s = sdf3.format(currentTime);
System.out.println("s:"+s);
String nowDatef = sdf3.format(nowDate);
System.out.println("nowDatef:"+nowDatef);
}
}
输出的结果是:
currentTime:1547263811242
date1:Sat Jan 12 11:30:11 CST 2019
date:20190112
time:11:30
s:2019-01-12 11:30
nowDatef:2019-01-12 11:30
结论:
这是在Java中将日期转换的方法:无论从数据里哪些什么的类型的数据,都可以通过SimpleDateFormat自定义前端想要的格式,当然也可以在数据库中直接转换好返给前端,但是会稍微影响一下sql的性能。
下面是在sql中将date转换成String 类型的日期格式
select DATE_FORMAT(time ,’%Y-%m-%d %H:%i:%S’)
from table
where mobile=#{mobile}
格式可以自定义:
‘%Y%m%d%H%i%s’
‘%Y-%m-%d %H:%i:%S’
自己总结所用,如有错误之处,请各路大神指正