Converting date long value to current Timezone val

2019-08-29 12:56发布

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.

标签: java timezone
2条回答
Rolldiameter
2楼-- · 2019-08-29 13:20
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.

查看更多
神经病院院长
3楼-- · 2019-08-29 13:21

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>));

查看更多
登录 后发表回答