Java Data Type How to - Display a date with a custom timezone








Question

We would like to know how to display a date with a custom timezone.

Answer

Use "zzz" instead of "ZZZ": "Z" is the symbol for an RFC822 time zone.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
// ww  w.ja  va 2s.co m
public class Main {
  public List<String> names;

  public static void main(String[] args) throws Exception {
    String fromDateString = "Wed Jul 08 17:08:48 GMT 2015";
    DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    Date fromDate = (Date) formatter.parse(fromDateString);
    TimeZone central = TimeZone.getTimeZone("America/Chicago");
    formatter.setTimeZone(central);
    System.out.println(formatter.format(fromDate));
  }
}

The code above generates the following result.