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
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • https://stackoverflow.com/questions/14157476/android-format-date-with-time-zone – ADM Aug 18 '18 at 02: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. – Jim Garrison Aug 18 '18 at 03:07
  • Possible duplicate of [Android Format date with time zone](https://stackoverflow.com/questions/14157476/android-format-date-with-time-zone) – jsDevia Aug 18 '18 at 03:39
  • 2
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 18 '18 at 07: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. – Basil Bourque Aug 18 '18 at 07:08
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 18 '18 at 07:40
  • Is your real problem how to convert an old-fashioned `Date` object into a modern `ZonedDateTime`? (There’s no need to go through formatting and parsing for that.) – Ole V.V. Aug 18 '18 at 07:43

4 Answers4

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
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]

Ashraff Ali Wahab
  • 1,088
  • 9
  • 19
  • Ok thank you! And how to I get [America/New_York] in android? –  Aug 18 '18 at 02:57
  • 2
    @Johnnyboy You use [`java.time.ZonedDateTime`](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html) if your Android runs Java 8, or [ThreeTen Android Backport](https://github.com/JakeWharton/ThreeTenABP#threeten-android-backport), if not. – Andreas Aug 18 '18 at 03:51
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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

This helps

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZ") .withLocale(Locale.ENGLISH);
Morpheus
  • 550
  • 1
  • 9
  • 23
  • Joda-Time? An option, though most think that its replacement, java.time, is better. Available on (older) Android through [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. Aug 18 '18 at 07:39