使用时区信息以毫秒为单位转换日期时间
Converting datetime in millisec with timezone info
我有一个简单的问题。如何实现这种格式的日期1438117140000+0300
。第一部分 1438117140000
以毫秒为单位的时间,我转换没有问题,第二部分时区信息让我头疼,如何获取它??
看起来 +0300 表示距格林威治标准时间 +3 小时。所以将 0300(3 小时)转换为毫秒并添加到 1438117140000。然后转换为日期时间,因为你已经
您可以使用 String.format
来达到这个目的:
Date now = new Date();
System.out.println(String.format("%tQ%tz", now, now));
执行刚刚打印出来的代码:
1438635740416+0300
date/time 的转换在 documentation 中指定。在这里,我使用了以下两个转换字符:
'Q': Milliseconds since the beginning of the epoch starting at 1
January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE.
'z': RFC 822 style numeric time zone offset from GMT, e.g. -0800. This
value will be adjusted as necessary for Daylight Saving Time. For
long, Long, and Date the time zone used is the default time zone for
this instance of the Java virtual machine.
将您必须的值拆分为“+”,然后将 3 小时作为毫秒添加到拆分方法返回的数组的第一个值。
我有一个简单的问题。如何实现这种格式的日期1438117140000+0300
。第一部分 1438117140000
以毫秒为单位的时间,我转换没有问题,第二部分时区信息让我头疼,如何获取它??
看起来 +0300 表示距格林威治标准时间 +3 小时。所以将 0300(3 小时)转换为毫秒并添加到 1438117140000。然后转换为日期时间,因为你已经
您可以使用 String.format
来达到这个目的:
Date now = new Date();
System.out.println(String.format("%tQ%tz", now, now));
执行刚刚打印出来的代码:
1438635740416+0300
date/time 的转换在 documentation 中指定。在这里,我使用了以下两个转换字符:
'Q': Milliseconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE.
'z': RFC 822 style numeric time zone offset from GMT, e.g. -0800. This value will be adjusted as necessary for Daylight Saving Time. For long, Long, and Date the time zone used is the default time zone for this instance of the Java virtual machine.
将您必须的值拆分为“+”,然后将 3 小时作为毫秒添加到拆分方法返回的数组的第一个值。