TimeZone的使用
TimeZone主要是解决跨时区的时间问题,最近在项目中使用了一下,还是比较简单的,在此做下记录。
1. 知道本地时间要计算美国洛杉矶的对应时间
/*以本地时区的方式解析时间*/
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date localDate = sdf1.parse("2021-11-22 13:20:00");
System.out.println(sdf1.getTimeZone());
String usaDateString = sdf1.format(localDate);
/*根据指定时区解析时间 */
TimeZone usaZone = TimeZone.getTimeZone("America/Los_Angeles");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf2.setTimeZone(usaZone);
System.out.println(sdf2.getTimeZone());
String usaDateString = sdf2.format(localDate);
中间注释掉的代码是走了弯路,不需要计算时区之间的差,直接解析就OK了
其中
yyyy-MM-dd HH:mm:ss
默认本地时区
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。