Converting date long value to current Timezone value

ぐ巨炮叔叔 提交于 2019-12-13 05:29:20

问题


I have a date as long value, I want to display the Date format in words depends upon the country time zone. Please help me in converting this.


回答1:


Take a look at the java.text.DateFormat class. It provides several option how to output the current Data and Time.

For example the following code prints the current date and time in the long format option for the en_UK locale: DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.UK).format(new Date());

Prints: 23 January 2012 08:17:36 CET

To display any date, just pass the long value to the constructor of the Date object. DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.UK).format(new Date(<your long value here>));




回答2:


I have a date as long value

You need to understand date as long value is the absolute value (UTC) of time in milli seconds since Epoch i.e. '1st Jan 1970 00:00:00'. This value has no timezone information embedded in it. Now if you want to display this time in your current local timezone then simply do this:

Date dt = new Date(1327303085000l);
System.out.printf("Date: %s%n", dt);

It prints:

Date: Mon Jan 23 02:18:05 EST 2012

Note the when it prints the date it automatically prints it with my current timezone.

However if you want customized formattng to display your date then you can take a look at the DateFormat class in Java.



来源:https://stackoverflow.com/questions/8968249/converting-date-long-value-to-current-timezone-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!