fork download
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.Locale;
  5. import java.util.TimeZone;
  6.  
  7. public class Main {
  8. public static void main(String[] args) throws ParseException {
  9. SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
  10. Date date = sdf.parse("2011-11-22T00:00:00-06:00");
  11. // Display in default format i.e. the value of date#toString
  12. System.out.println(date);
  13.  
  14. // #################Display in custom format and timezone#################
  15. // At timezone offset of -06:00 hours
  16. sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
  17. System.out.println(sdf.format(date));
  18.  
  19. // In UTC
  20. sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
  21. System.out.println(sdf.format(date));
  22.  
  23. // In New York
  24. sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
  25. System.out.println(sdf.format(date));
  26. }
  27. }
Success #stdin #stdout 0.22s 53468KB
stdin
Standard input is empty
stdout
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00