Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the time of other TimeZone by date string, the result is wrong

I only have a date string, and I want to see the time in other TimeZone by it. So I did it like that:

    String dateStr = "2014-05-15 16:14:58 PM";
    SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
    sdf.setTimeZone(TimeZone.getTimeZone("America/Denver"));
    Date date = sdf.parse(dateStr);
    System.out.println(date);

    SimpleDateFormat sdf1 =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
    System.out.println(sdf1.format(date));

This is the current TimeZone in my computer:

enter image description here

The result that the code ran was that:

 Fri May 16 06:14:58 CST 2014

 2014-05-16 06:14:58 AM

The result is wrong, I had the right result by changing the TimeZone to "America/Denver" in my computer, and I saw that:

America/Denver —— 2014-05-15 02:14:58 AM

I don't know why it likes that?

But if I had a Date not a date String, I do that :

public static String getFormatedDateString(String _timeZone) {
    TimeZone timeZone = null;
    if (StringUtils.isEmpty(_timeZone)) {
        timeZone = TimeZone.getDefault();
    } else {
        timeZone = TimeZone.getTimeZone(_timeZone);
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
    sdf.setTimeZone(timeZone);
    // TimeZone.setDefault(timeZone);
    return sdf.format(new Date());
}

System.out.println("America/Denver —— " + getFormatedDateString("America/Denver"));

The result likes that:

------Asia/Shanghai------
2014-05-15 16:32:04 PM (current date)
America/Denver —— 2014-05-15 02:32:04 AM

This result is right.

So I was confused, I could't find the problem when I just have a date string and I want to know the time of other TimeZone. Could any body help me?

like image 462
Rocky Hu Avatar asked May 15 '14 08:05

Rocky Hu


1 Answers

  1. Date object in Java is independent of the concept of timezone.
  2. What you want to do get the equivalent time in another timezone of a date string which is 'supposed' to be in your own timezone.

However, 2nd point appears backwards in your code:

String dateStr = "2014-05-15 16:14:58 PM";
SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
sdf.setTimeZone(TimeZone.getTimeZone("America/Denver"));
Date date = sdf.parse(dateStr);

What these 4 lines do is consider the date string as a point in time in "America/Denver" timezone.

When you parse it to the date object, it would give you the equivalent time in your own timezone.

You want it the other way round:

Hence staying close to your code (you can just use a single SimpleDateFormat instance effectively, which you can figure out later),

Drop the setTimezone on the first sdf:

String dateStr = "2014-05-15 16:14:58 PM";
SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
//sdf.setTimeZone(TimeZone.getTimeZone("America/Denver"));
Date date = sdf.parse(dateStr);
System.out.println(date);

Add the same setTimezone to the other sdf:

SimpleDateFormat sdf1 =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
sdf1.setTimeZone(TimeZone.getTimeZone("America/Denver"));
System.out.println(sdf1.format(date));

Now, you are parsing your date String to a date in your current (JVM's) timezone. Then format the same date to a different timezone's String.

Output I get with the changed code (my JVM's timezone being IST):

Thu May 15 16:14:58 IST 2014 // Parsed the date string in IST
2014-05-15 04:44:58 AM // Equivalent time in Denver
like image 113
StoopidDonut Avatar answered Oct 25 '22 22:10

StoopidDonut