0

I wanted the date in GMT as String like the javascript code below:

const date = new Date();
date.toGMTString();

I am referring to: Parsing Java String into GMT Date and my code looks like below but this errors out:

private static String getDateGMT() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println(sdf.format(date));
        return sdf.format(sdf);
    }

How do I get the Date in "Thu, 04 Nov 2021 06:50:37 GMT" format?

Aavik
  • 967
  • 19
  • 48
  • Try this: `new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z")` – Francesco Nov 04 '21 at 08:20
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `OffsetDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 04 '21 at 08:26
  • 2
    *How do I get the Date in "Thu, 04 Nov 2021 06:50:37 GMT" format?* If you mean that literally, you can’t. A `Date` neither has got a format nor a time zone nor an offset from GMT. – Ole V.V. Nov 04 '21 at 08:27
  • 3
    Nor is GMT a format. It part of a time zone - the part that the UK observes during winter time, at the moment. (It can *sort of* be regarded as a time zone in its own right roughly like UTC, although neither is strictly speaking a time zone.) Also note that writing "but this errors out" without specifying the error is unhelpful. – Jon Skeet Nov 04 '21 at 08:31
  • I took this opportunity for writing [a modern answer using java.time](https://stackoverflow.com/a/69837541/5772882) to the question that you are linking to. – Ole V.V. Nov 04 '21 at 10:17

3 Answers3

5

java.time and DateTimeFormatter.RFC_1123_DATE_TIME

private static String getDateGMT() {
    return OffsetDateTime.now(ZoneId.of("Etc/GMT"))
            .format(DateTimeFormatter.RFC_1123_DATE_TIME);
}

This just returned:

Thu, 4 Nov 2021 08:31:33 GMT

Your desired format is built in, so no need to write your own format pattern string. This is good because writing a correct format pattern is always error-prone.

I recommend that you use java.time, the modern Java date and time API, for all of your date and time work.

Tutorial link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0
private static String getDateGMT() {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        return sdf.format(new Date());
    }
Aavik
  • 967
  • 19
  • 48
  • While this code may answer the question, it would be better to include some _context_, explaining _how_ it works and _when_ to use it. Code-only answers are not useful in the long run. – PCM Nov 04 '21 at 12:29
  • 1
    This code uses terrible date-time classes that were years ago supplanted by the modern *java.time* classes defined in JSR 310. Time to move on. – Basil Bourque Nov 04 '21 at 13:53
0
private static String getDateGMT() {    
    String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
    final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    
    return sdf.format(new Date(System.currentTimeMillis()));
}
libao zhou
  • 23
  • 3
  • 1
    Thanks for wanting to contribute, The main difference between your answer and the one by Aavik seems to be that you are not adhering to the Java naming conventions. Maybe repeating myself, I recommend neither of you uses `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. – Ole V.V. Nov 04 '21 at 08:50
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 04 '21 at 08:54