3

I am trying to make the SimpleDateFormat class to do what I need without success.

Here is the date format I need:

Fri 29 May 2015 10:22:30 GMT-0400 (Eastern Daylight Time)

Here is the closest format definition I was able to come with:

SimpleDateFormat("EEE dd MM yyyy HH:mm:ss Z (zzzz)", Locale.ENGLISH)

This will output:

Fri 29 May 2015 10:22:30 -0400 (Eastern Daylight Time)

Is there a formatting that can do what I need or do I have no other choice than manipulating the String to insert the missing GMT tag?

Braiam
  • 1
  • 11
  • 47
  • 78
Achille
  • 607
  • 1
  • 11
  • 23

3 Answers3

3

You can add the literal GMT surrounded by quotes:

new SimpleDateFormat("EEE dd MM yyyy HH:mm:ss 'GMT'Z (zzzz)", Locale.ENGLISH);
M A
  • 71,713
  • 13
  • 134
  • 174
0

Try using EEE dd MM yyyy HH:mm:ss zZ (zzzz)

Working example

Srini
  • 1,626
  • 2
  • 15
  • 25
  • Sorry, that will give you "EDT-0400" not "GMT-0400" at the end. – Andy Lowry May 29 '15 at 19:39
  • @user2656928 I just tried it and it gives me GMT. [Here](http://ideone.com/ml5Ici)'s a working example. – Srini May 29 '15 at 19:53
  • I run your program and get EDT. Is your system default timezone GMT by any chance? But then you wouldn't get "-0400" afterward. – Andy Lowry May 29 '15 at 20:28
  • @user2656928, this was my output : `Fri 29 05 2015 21:24:03 GMT+0000 (Greenwich Mean Time)` – Srini May 29 '15 at 20:29
  • Right. So your default timezone is GMT, which is "+0000" from GMT (since it *is* GMT). If you had gotten "GMT-0400" then something would have been very screwy. The OP appears to be running in EDT (as I am), but wants 'GMT' stuck in front of the "-0400" timezone offset (since that's what the offset is relative to). Hence using "z" won't work - it'll provide the local timezone, which is not what the offset relates to. – Andy Lowry May 29 '15 at 20:33
  • @user2656928, Ah, I see. Thanks for clarifying that. Short of throwing in the 'GMT' string literal, I can't think of a way to do this then. – Srini May 29 '15 at 20:54
0

java.time

I recommend you use the modern date-time API*.

Using the available symbols, the closest match can be obtained with the pattern, EEE dd MMM uuuu HH:mm:ss OOOO (zzzz). However, it will display a : as the separator in the timezone offset. If you want the exact format, you can use the pattern, EEE dd MMM uuuu HH:mm:ss 'GMT'XX (zzzz) in which GMT has been used as a String literal.

Demo:

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.of(LocalDate.of(2015, Month.MAY, 29), LocalTime.of(10, 22, 30),
                ZoneId.of("America/New_York"));

        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("EEE dd MMM uuuu HH:mm:ss OOOO (zzzz)", Locale.ENGLISH);
        String formatted1 = dtf1.format(zdt);
        System.out.println(formatted1);

        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("EEE dd MMM uuuu HH:mm:ss 'GMT'XX (zzzz)", Locale.ENGLISH);
        String formatted2 = dtf2.format(zdt);
        System.out.println(formatted2);
    }
}

Output:

Fri 29 May 2015 10:22:30 GMT-04:00 (Eastern Daylight Time)
Fri 29 May 2015 10:22:30 GMT-0400 (Eastern Daylight Time)

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Just because you post an answer using `java.time` (which I think is appropriate, we hsould push towards using modern APIs) doesn't mean the *question* is related to `java.time`. I've undone the re-tagging. – Joachim Sauer May 12 '21 at 19:59
  • 1
    @JoachimSauer - That was not the reason why I added the tag, `java.time`; I added this tag to make it easier for someone searching for this type of Q/A using the tag, `java-time`. Nevertheless, you have 10 more years of experience on SO than I have and therefore you know much more than I do. – Arvind Kumar Avinash May 12 '21 at 20:06