3

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
7
  • stackoverflow.com/questions/14157476/… Commented Aug 18, 2018 at 2:51
  • 5
    @ADM That answer is over 5 years old and recommends Joda Time. All the functionality of Joda Time has been in the standard Java library for quite some time now. Commented Aug 18, 2018 at 3:07
  • Possible duplicate of Android Format date with time zone Commented Aug 18, 2018 at 3:39
  • 2
    FYI, the terribly troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle. Commented Aug 18, 2018 at 7:06
  • 1
    @jsDevia That Question you linked is not a duplicate. (a) That one is Android, this one is Java. (b) That one is only about text format using offset-from-UTC, while this one asks about the name of a time zone in addition to the offset. Commented Aug 18, 2018 at 7:08

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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]

2 Comments

Ok thank you! And how to I get [America/New_York] in android?
@Johnnyboy You use java.time.ZonedDateTime if your Android runs Java 8, or ThreeTen Android Backport, if not.
0

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.

Comments

0

This helps

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

1 Comment

Joda-Time? An option, though most think that its replacement, java.time, is better. Available on (older) Android through ThreeTenABP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.