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,274
  • 19
  • 77
  • 125
user1767260
  • 1,537
  • 8
  • 26
  • 49

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
  • 15,742
  • 4
  • 41
  • 57