1

I need to format a date and time as Nov 15 2012 18:55 GMT.

Time should be in GMT and 24hr format.I have tried using the following code but it gives time in 12hr format.Is there any way?

DateFormat currentDateTimeString = DateFormat.getDateTimeInstance();
currentDateTimeString.setTimeZone(TimeZone.getTimeZone("gmt"));
gmtTime = currentDateTimeString.format(new Date());
Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
user1767260
  • 1,543
  • 8
  • 26
  • 51

1 Answers1

5

You can use DateFormats to convert Dates to Strings in any timezone:

  DateFormat df = DateFormat.getTimeInstance();
  df.setTimeZone(TimeZone.getTimeZone("gmt"));
  String gmtTime = df.format(new Date());

OR

 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
 Date currentLocalTime = cal.getTime();
 DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss z");   
 date.setTimeZone(TimeZone.getTimeZone("GMT")); 
 String localTime = date.format(currentLocalTime); 
 System.out.println(localTime);
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54