Reputation: 4520
I have no idea why Java makes it near impossible to do such a simple thing. I have tried many solutions I find online, but there doesn't seem to be a simple clean and working solution.
Here is my latest attempt at a solution
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println( sdf.format(calendar.getTime()) );
My excepted output: 27/02/2014 17:06:00
My real output: 03/03/2014 20:35:44
How does this even make any sense.
Upvotes: 3
Views: 3475
Reputation: 175
After hours DuckDuckGoging I solved it in Android with (the call requires API level 24):
Calendar calendario = Calendar.getInstance(TimeZone.getTimeZone(Time.getCurrentTimezone()));
Upvotes: 0
Reputation: 340350
ZonedDateTime.now()
The real problem is using the notoriously troublesome java.util.Date and .Calendar classes. Use the modern java.time classes instead.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ; // Capture the current moment, with wall-clock time used by people of a particular region.
See this code run live at IdeOne.com.
zdt.toString(): 2018-01-28T15:12:58.942-08:00[America/Los_Angeles]
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
To generate strings in other formats, search Stack Overflow for DateTimeFormatter
.
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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
UPDATE: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. Leaving this section for history.
One important difference is that while a java.util.Date has no timezone, a DateTime (in Joda-Time) and a ZonedDateTime (in java.time) both truly know their own assigned time zone.
Example code in Joda-Time 2.3. If you choose to not specify a time zone, you get the JVM's default time zone. Use proper time zone names, never the 3 or 4 letter codes.
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime now = new DateTime( timeZone );
DateTime nowDefaultTimeZone = new DateTime();
String output is in ISO 8601 format (ex: 2014-02-27T23:03:14+3:00
) by default. To create string representations in other formats, search StackOverflow for "joda format".
Upvotes: 2
Reputation: 1
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println(sdf.format(new Date()));
TimeZone timeZone=TimeZone.getDefault();
System.out.println(timeZone.getDisplayName());
TimeZone americaTimeZone=TimeZone.getTimeZone("America/Los_Angeles");
sdf.setTimeZone(americaTimeZone);
Calendar calendar=Calendar.getInstance(americaTimeZone);
System.out.println(sdf.format(calendar.getTime()));
SimpleDateFormat default format with default TimeZone;
Upvotes: 0
Reputation: 240996
set timezone in SimpleDateFormat
instance instead
sdf.setTimezone("America/Los_Angeles");
Upvotes: 4