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. String strDateTime = "31 October 2011 11:19:56 GMT";
  10.  
  11. SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy HH:mm:ss z", Locale.ENGLISH);
  12. sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
  13.  
  14. Date date = sdf.parse(strDateTime);
  15.  
  16. String strDate = sdf.format(date);
  17. System.out.println(strDate);
  18.  
  19. // Some other format
  20. sdf = new SimpleDateFormat("MMMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
  21. sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
  22. strDate = sdf.format(date);
  23. System.out.println(strDate);
  24.  
  25. // The last format with some other timezone
  26. sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
  27. strDate = sdf.format(date);
  28. System.out.println(strDate);
  29. }
  30. }
Success #stdin #stdout 0.23s 53904KB
stdin
Standard input is empty
stdout
31 October 2011 11:19:56 GMT
October 31 11:19:56 GMT 2011
October 31 07:19:56 EDT 2011