different Date in java/android
When I execute this code in plain java and in android, I get different results:
System.out.println(new Date(1311940549187l).toString());
In plain java, I get this output:
Fri Jul 29 13:55:49 CEST 2011
In android, I get this output:
Fri Jul 29 11:55:49 GMT+00:00 2011
I understand it has something to do with the time zone. But I really need to get the same output. How can I generate a Date
object based on a Long
value, and开发者_如何学编程 get the same date in both environments ?
Edit: I already use SimpleDateFormat and the two datetimes have 2 hours offset. Please tell me how to get rid of the offset.
this is how I use the SimpleDateFormat: new SimpleDateFormat("MM-dd-yyyy HH:mm")
The date is the same in both environment, but toString() gives a different result because the two environments are configured with a different timezone.
To format a date for a specific timezone, use a SimpleDateFormat and set the timezone to a specific value (GMT for instance).
UPDATE:
TimeZone gmt = TimeZone.getTimeZone("GMT");
SimpleDateFormat format = new SimpleDateFormat("...");
format.setCalendar(Calendar.getInstance(gmt));
You could use a SimpleDateFormat
and convert the java code to match the output of what android is giving you.
SimpleDateFormat
Or Vice versa (Android convert to match java)
Android SimpleDateFormat
dFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm")
TimeZone tz = TimeZone.getTimeZone("GMT:00");
dFormat .setTimeZone( tz );
tl;dr
Instant
.ofEpochMilli( 1_311_940_549_187L )
.toString()
2011-07-29T11:55:49.187Z
java.time
The modern solution uses java.time classes. The terrible java.util.Date
class was replaced by java.time.Instant
. Both represent a moment as seen in UTC. The Instant
class is much better behaved in that it does not inject time zones in unexpected ways like Date
does. Never use java.util.Date
.
Parse your count of milliseconds since epoch reference of first moment of 1970 in UTC as a java.time.Instant
object.
Instant instant = Instant.ofEpochMilli( 1_311_940_549_187L ) ;
Generate text in standard ISO 8601 format.
String output = instant.toString() ;
2011-07-29T11:55:49.187Z
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 brought some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android (26+) bundle implementations of the java.time classes.
- For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
- If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
精彩评论