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.