一、Timestamp类型的小时差计算
@Test
public void test111() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse("2020-09-23 00:50:00");
long time1 = d.getTime();
long time = sdf.parse("2020-09-23 10:00:00").getTime();
long l = time - time1;
double a =(double)l /(60*60*1000);
DecimalFormat df = new DecimalFormat("0.0");
String format = df.format(a);
System.out.println("time: "+time);
System.out.println("time1: "+time1);
System.out.println("l: "+l);
System.out.println("l1: "+a);
System.out.println("format: "+format);
}
// 输出:
// time: 1600826400000
// time1: 1600793400000
// l: 33000000
// l1: 9.166666666666666
// format: 9.2
二、TZ的时间处理:转换成Timestamp类型
@Test
public void test121() throws ParseException {
String s= "2021-04-23T16:00:00.000Z";
String s1 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat(s1);
date = sdf.parse(s);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8);
Date time = calendar.getTime();
System.out.println("UTC时间: " + date);
System.out.println("北京时间: " + calendar.getTime()); //calendar.getTime() 返回的是Date类型,也可以使用calendar.getTimeInMillis()获取时间戳
System.out.println("Date时间:" + time);
System.out.println("Timestamp类型:" + new Timestamp(time.getTime()));
}
输出:
// UTC时间: Fri Apr 23 16:00:00 CST 2021
// 北京时间: Sat Apr 24 00:00:00 CST 2021
// Date时间:Sat Apr 24 00:00:00 CST 2021
// Timestamp类型:2021-04-24 00:00:00.0