Get current date in Java in GMT timezone

Posted on 25-04-2013 11:59 by graham
As is well known, there are two main date/time types in Java: java.util.Date and java.util.Calendar. While getting the current datetime with the Calendar data type is easy, doing the same using the Date type poses some problems.

Getting current GMT date using java.util.Calendar

The code to do this is simple:
Calendar currentGMTDate = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

Getting current GMT date using java.util.Date

The problem with the Date data type is that it is always converted to the local time zone. To convert it to GMT time zone, we need to add/subtract the timezone offset from the result.

The following method returns the current date in GMT timezone.
public static Date getGMTTime()
Calendar c = new GregorianCalendar();
c.setTime(new Date());
c.add(Calendar.MINUTE, c.getTime().getTimezoneOffset());
return c.getTime();

NOTE: this implementation uses a deprecated method c.getTime().getTimezoneOffset().


Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems