user10217397
user10217397

Reputation:

How to get date format with timezone in java

I'm new to java. I'd like to get this date format with the timezone in text at the end there.

2017-11-23T23:43:45-05:00[America/New_York]

What I have is this for now, I'd also like to add the user's default timezone as the timezone at the end there. Here's what I currently have:

SimpleDateFormat trust = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());

Which gives me (Example):

2018-08-17T22:39:39-0400

Upvotes: 3

Views: 13690

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 340178

tl;dr

ZonedDateTime                        // Represent a moment as seen in the wall-clock time used by the people of a particular region (a time zone). 
.now(                                // Capture the current moment.
    ZoneId.of( "America/New_York" )  // Specify the time zone through whose wall-clock time we want to perceive the current date and time.
)                                    // Returns a `ZonedDateTime` object.
.toString()                          // Generate text in standard ISO 8601 format extended to append the name of the time zone in square brackets.

2017-11-23T23:43:45-05:00[America/New_York]

ISO 8601

The ISO 8601 standard defines many practical formats for representing date-time values as human-readable text. The first chunk of your desired format is one such standard format:

2017-11-23T23:43:45-05:00

Your desired format extends standard format by appending the name of the time zone in square brackets.

2017-11-23T23:43:45-05:00[America/New_York]

That extended standard format is exactly the behavior of the ZonedDateTime::toString method. You will find that class bundled with Java.

ZonedDateTime.now().toString()  // Generate text in standard ISO 8601 format extended to append the name of the time zone in square brackets.

Avoid legacy date-time classes

The SimpleDateFormat class is part of the troublesome old date-time classes that were supplanted years ago by the java.time classes. Never use these awful legacy classes.

ZonedDateTime

Get the current moment as seen in the wall-clock time used by the people of a particular region (a time zone).

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/New_York" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Generate a String object containing text in your desired format.

String output = zdt.toString() ;  // Generate text in standard ISO 8601 format extended to append the name of the time zone in square brackets.

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.

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.

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.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Upvotes: 8

Morpheus
Morpheus

Reputation: 574

This helps

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZ") .withLocale(Locale.ENGLISH);

Upvotes: 0

Jim Garrison
Jim Garrison

Reputation: 86774

A given offset can map to more than one name, for example UTC-0500 can be America/New_York or America/Toronto or America/Montreal or America/Nassau (there are probably more). More precise geographic information would be needed to choose the correct one, and the "correct one" could depend on other factors as well.

This is why there's no simple API to return this value, there's no canonical right answer. You will have to decide your own rules for determining the string and then use the TZ database to retrieve it.

Upvotes: 0

Ashraff Ali Wahab
Ashraff Ali Wahab

Reputation: 1108

Use yyyy-MM-dd'T'HH:mm:ssXXX to print time as 2017-11-23T23:43:45-05:00 and there is no format specifier for [America/New_York]. You can use [zzzz] which will print as [Eastern Standard Time] not as [America/New_York]

Upvotes: 1

Related Questions