Formatting a Java Date into a Specific TimeZone and Conversion Between TimeZone's

| No TrackBacks
Date objects in Java, and probably most other robust languages, simply represent a snapshot of a point in time.  In other words, java.util.Date knows nothing about the time zone you're referring to when instantiating or manipulating a Date object.  Fact is, java.util.Date does not have to care about your time zone, because internally a Date is really nothing more than a count of the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

If it helps, think about it this way: X milliseconds since the epoch is X milliseconds since the epoch in the US-Pacific time zone, X milliseconds since the epoch in GMT-0 (London), X milliseconds since the epoch in India, etc.  In short, when it's 1273947282085 milliseconds since the epoch, it's 1273947282085 milliseconds since the epoch everywhere in the world at the same time regardless of what time zone you're sitting in.  And since Java's util.Date is simply a snapshot of the number of milliseconds at a specific point in time, you can see why Date doesn't care about your time zone.  It's irrelevant.

But Mark, how do I convert a java.util.Date into a different time zone?  You can't, and that question makes no sense.  That's like asking me to "take a picture of the sound."  Here's some B.S. code that you should not use, but I've put it here for illustrative purposes:

// Do NOT use this, it does nothing and makes no sense.
public static final Date convertIntoTimeZone(Date date, TimeZone tz) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setTimeZone(tz);
return cal.getTime();
}

You can't convert a Date into a different time zone, but you can use Java's handy DateFormat class to format a Date into the time zone of your choice.  To put it differently, let Date do its thing.  Then, when you're ready to display or print out a String representation of Date, that's when you tell DateFormat what time zone you want it in.  So, here's some code that makes sense, and actually works:

final Date currentTime = new Date();

final SimpleDateFormat sdf =
new SimpleDateFormat("EEE, MMM d, yyyy hh:mm:ss a z");

// Give it to me in US-Pacific time.
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println("LA time: " + sdf.format(currentTime));

// Give it to me in GMT-0 time.
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("GMT time: " + sdf.format(currentTime));

// Or maybe Zagreb local time.
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Zagreb"));
System.out.println("Zagreb time: " + sdf.format(currentTime));

// Even 10 hours and 10 minutes ahead of GMT
sdf.setTimeZone(TimeZone.getTimeZone("GMT+0010"));
System.out.println("10/10 ahead time: " + sdf.format(currentTime));

Yay for Dates and time zones!  Enjoy.

Did You Find this Helpful?

Did you find this post helpful, or at least, interesting?

  

Send Mark a Direct Message

If you'd like to send me a direct message, please do so below. However, I do not publicly post comments or messages submitted directly to me. So, if you're going to try to SPAM me, or my blog, you're pretty much wasting your time.

400 characters remaining

Error

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer, a casual entrepreneur, and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

No TrackBacks

No trackbacks attached to this entry.

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on May 15, 2010 11:51 AM.

HTTP Digest Access Authentication using MD5 and HttpClient 4 was the previous entry in this blog.

OAuth and the Twitter API: Generate a one-time access token and token secret is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.