前言 Java中表示时间有三种类型:
1 2 3 String dateStr = "2020-01-01 15:00:11" ;  Date date = new  Date();  long  time = date.getTime(); 
 
  这三种类型的不同在于,String类型不带时区信息,Date和long类型会带时区信息。开发项目时,接口参数的时间字段最好用long类型表示,这样发送方和接收方收到的时间会一致。用String类型也能一致,但双方可能都要做格式转换。用Date类型做接口参数时,接收方和收到的值可能与发送方的不一致,不适合验签。
Date Date转String 看一下电脑上的时间,得知当前时间是8时区的2020-02-25 15:41:21(系统时区是8时区)。 按照yyyy-MM-dd HH:mm:ss的格式打印当前时间:
1 2 3 4 Date d = new  Date();  String format = "yyyy-MM-dd HH:mm:ss" ;  SimpleDateFormat sdf = new  SimpleDateFormat(format); System.out.println(sdf.format(d));  
 
打印结果和电脑上显示的一致。
转换时考虑时区 若想知道当前时间对应的GMT0时间是多少,就要在转换时加上时区:
1 2 3 4 5 6 Date d = new  Date();  String format = "yyyy-MM-dd HH:mm:ss" ; SimpleDateFormat sdf = new  SimpleDateFormat(format); String timezone = "GMT+0" ;  sdf.setTimeZone(TimeZone.getTimeZone(timezone)); System.out.println(sdf.format(d));  
 
打印结果2020-02-25 07:41:21(GMT0) 和当前时间2020-02-25 15:41:21(GMT8) 可以对应。
Date转long 1 2 Date date = new  Date(); long  time = date.getTime();
 
Long long转Date 1 2 long  currentTimeMillis = System.currentTimeMillis();Date d = new  Date(currentTimeMillis); 
 
long转String 先long转Date,再Date转String。
String String转Date 若已知时间字符串”2020-02-25 10:44:05”是8时区,系统也是8时区,那么转换时可以简单的写:
1 2 3 4 5 6 7 8 9 10 try  {	String timeStr = "2020-02-25 10:44:05" ; 	String format = "yyyy-MM-dd HH:mm:ss" ;  	SimpleDateFormat sdf = new  SimpleDateFormat(format); 	Date date = sdf.parse(timeStr); 	System.out.println(date);  } catch  (ParseException e) { 	e.printStackTrace(); } 
 
打印出的CST时间10:44:05 和时间字符串2020-02-25 10:44:05(GMT8) 一致。
CST时间:China Standard Time,中国标准时间,等于GMT8
 
转换时考虑时区 但如果已知”2020-02-25 10:44:05”这个字符串是0时区,那么就要指定时区转换:
1 2 3 4 5 6 7 8 9 10 11 12 try  {	String timeStr = "2020-02-25 10:44:05" ; 	String format = "yyyy-MM-dd HH:mm:ss" ;  	SimpleDateFormat sdf = new  SimpleDateFormat(format); 	String timezone = "GMT+0" ;  	sdf.setTimeZone(TimeZone.getTimeZone(timezone)); 	Date date = sdf.parse(timeStr); 	System.out.println(date);  } catch  (ParseException e) { 	e.printStackTrace(); } 
 
打印出的CST时间18:44:05 和时间字符串2020-02-25 10:44:05(GMT0) 一致。
复杂格式的时间字符串转换 处理复杂的时间字符串,如PDT时间 :
PDT时间:Pacific Daylight Time,太平洋夏季时间,等于UTC-7,UTC=GMT0,因此PDT=GMT8-15 PST时间:Pacific Standard Timee,太平洋标准时间,等于PDT-1,因此PST=GMT8-16
 
1 2 3 4 5 6 7 8 9 10 try  {	String timeStr = "03:00:14 Jun 13, 2019 PDT" ;  	String format = "HH:mm:ss MMM dd, yyyy z" ;  	SimpleDateFormat sdf = new  SimpleDateFormat(format, Locale.ENGLISH);  	Date date = sdf.parse(timeStr); 	System.out.println(date);  	 } catch  (ParseException e) { 	e.printStackTrace(); } 
 
打印出的CST时间18:00:14 和时间字符串2019-06-13 03:00:14(PDT) 一致。
处理UTC时间 (UTC=GMT0):
1 2 3 4 5 6 7 8 9 10 11 12 try  {	String timeStr = "2019-04-24T02:30:00" ; 	String format = "yyyy-MM-dd'T'HH:mm:ss" ;  	SimpleDateFormat sdf = new  SimpleDateFormat(format); 	String timezone = "GMT+0" ;  	sdf.setTimeZone(TimeZone.getTimeZone(timezone)); 	Date date = sdf.parse(timeStr); 	System.out.println(date);  	 } catch  (ParseException e) { 	e.printStackTrace(); } 
 
打印出的CST时间10:30:00 和时间字符串2019-04-24 02:30:00(UTC) 一致。
String转long 先String转Date,再Date转long。