I am trying to get the the day and time in UTC as milliseconds but repeatedly get the same problem. The result should be something like '63530139420000' but each time the value '1394547490884' is returned.

To get the date and time in UTC I use the following method:

long dateutc = System.currentTimeMillis();

Can anyone tell me what is the problem?

有帮助吗?

解决方案

You can use this.

DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateutc = df.format(new Date());

其他提示

Your code correctly gets the number of milliseconds since January 1, 1970 00:00:00 UTC.

Interestingly if you take the value your teacher has specified and divide by the number of milliseconds in a year you get 2014.5. So either your teacher doesn't know what currentTimeMillis() does or he wants milliseconds since year 0 (which doesn't make any sense to me).

To calculate number of milliseconds since a given date all you need to do is to create two date instances and subtract the milliseconds values from getTime().

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top