Java时间问题

时间转时间戳:

String startTime = "2018-10-08";
String endTime = "2018-10-08";
startTime = startTime + " 00:00:00";
endTime = endTime + " 23:59:59";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone china = TimeZone.getTimeZone("Asia/Shanghai");
// 设置时区
simpleDateFormat.setTimeZone(china);
int start =  new Long(simpleDateFormat.parse(startTime).getTime() / 1000).intValue();
int end =  new Long(simpleDateFormat.parse(endTime).getTime() / 1000).intValue();

数据库比较用 WHERE d.update_time BETWEEN #{ start } AND #{end}

时间戳转时间:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone china = TimeZone.getTimeZone("Asia/Shanghai");
// 设置时区
simpleDateFormat.setTimeZone(china);
long lt = new Long("1538986696");
Date date = new Date(lt * 1000);
System.out.println(simpleDateFormat.format(date));

注意时区问题

猜你喜欢

转载自blog.csdn.net/yilia_jia/article/details/82970067