Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display a date with a custom timezone?

Lets say I have a string that represents a date that looks like this:

"Wed Jul 08 17:08:48 GMT 2009"

So I parse that string into a date object like this:

DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
Date fromDate = (Date)formatter.parse(fromDateString);

That gives me the correct date object. Now I want to display this date as a CDT value.

I've tried many things, and I just can't get it to work correctly. There must be a simple method using the DateFormat class to get this to work. Any advice? My last attempt was this:

formatter.setTimeZone(toTimeZone);
String result = formatter.format(fromDate);
like image 607
Kyle Boon Avatar asked Jul 22 '09 16:07

Kyle Boon


People also ask

Do dates have time zones?

DateTime Variations Dates and times (together known as DateTimes) do vary over different regions of the world and in response to adjustments to match solar time. The primary time standard is called Coordinated Universal Time (also known as UTC). However, local time standards can be affected by: Local Time Zones.

How do you format a date?

The international standard recommends writing the date as year, then month, then the day: YYYY-MM-DD. So if both Australians and Americans used this, they would both write the date as 2019-02-03. Writing the date this way avoids confusion by placing the year first.

How do you add timezone in Excel?

To use the Time Zone function, first select the cell where you want the converted time to appear. Then click on the Formulas tab and select Insert Function. In the Insert Function dialog box, scroll down until you see the TIMEZONE function and select it. Click OK.


2 Answers

Use "zzz" instead of "ZZZ": "Z" is the symbol for an RFC822 time zone.

DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

Having said that, my standard advice on date/time stuff is to use Joda Time, which is an altogether better API.

EDIT: Short but complete program:

import java.text.*;
import java.util.*;

public class Test
{
    public List<String> names;

    public static void main(String [] args)
        throws Exception // Just for simplicity!
    {
        String fromDateString = "Wed Jul 08 17:08:48 GMT 2009";
        DateFormat formatter = new SimpleDateFormat
            ("EEE MMM dd HH:mm:ss zzz yyyy");
        Date fromDate = (Date)formatter.parse(fromDateString);
        TimeZone central = TimeZone.getTimeZone("America/Chicago");
        formatter.setTimeZone(central);
        System.out.println(formatter.format(fromDate));
    }
}

Output: Wed Jul 08 12:08:48 CDT 2009

like image 184
Jon Skeet Avatar answered Sep 28 '22 17:09

Jon Skeet


Using:

formatter.setTimeZone(TimeZone.getTimeZone("US/Central"));

outputs:

Wed Jul 08 12:08:48 CDT 2009

for the date in your example on my machine. That is after substituting zzz for ZZZ in the format string.

like image 45
laz Avatar answered Sep 28 '22 17:09

laz