Jodatime DateTimeFormatter needs to print GMT time instead of UTC

I am writing this formatter:

TimeZone timeZone = TimeZone.getTimeZone("GMT");
private static final String FORMAT_RFC_1123_DATE_TIME = "EEE, dd MMM yyyy HH:mm:ss zzz";
private final DateTimeFormatter dateTimeFormat = forPattern(FORMAT_RFC_1123_DATE_TIME).withLocale(US).withZone(DateTimeZone.forTimeZone(timeZone));
date.toString(dateTimeFormat)

      

The transformation is fine, but instead Sun, 06 Nov 1994 07:49:37 GMT

it writes Sun, 06 Nov 1994 07:49:37 UTC

.

I know UTC is the right way out, but I really need one with GMT.

How can I solve this?

+3


source to share


2 answers


It looks like JodaTime doesn't support this. If you try

org.joda.time.DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT"))

      

In fact, you get UTC

. The javadoc from DateTimeZone

states:



This library only uses the term UTC.

Perhaps you could subclass DateTimeZone

yourself and get what you need, but from what I can tell otherwise, JodaTime won't do what you are trying to do. Below is an example, although I am not claiming that it works in any shape or form.

public class GMTDTZ extends DateTimeZone {
    public GMTDTZ() { super("GMT"); }

    @Override public String getNameKey(long instant) { return null; }

    @Override public int getOffset(long instant) { return 0; }

    @Override public int getStandardOffset(long instant) { return 0; }

    @Override public boolean isFixed() { return true;}

    @Override public long nextTransition(long instant) { return instant; }

    @Override public long previousTransition(long instant) { return instant; }

    @Override public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof DateTimeZone)) return false;
        return (((DateTimeZone) o).getID().equals("GMT"));
    }
}

      

+2


source


I tried the following code and it worked:

date.withZone(DateTimeZone.forID("GMT"))
    .toString("EEE, dd MMM yyyy HH:mm:ss zzz");

      



My joda library version is 2.9.1. By the way,

+1


source







All Articles