Solved

DateFormat timezone problem

Posted on 2006-11-18
7
4,959 Views
Last Modified: 2008-01-09
Hi All,

I have the following scenario. I have the date as string in the format of yyyy-MM-dd HH:mm:ss and i need to convert this date in a different timezone say for ex: mexico/general for display purpose in the client. How should i go with it? I tried the following:

        String nowday = "2006-11-17 18:52:00";
       
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date now = new java.util.Date();
        try{
             now = sim.parse(nowday);
        }catch(ParseException e){}    
       
       
        SimpleDateFormat dmj= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // Mexico/General
       DateFormat dfCST = DateFormat.getDateTimeInstance();
        dfCST.setTimeZone(TimeZone.getTimeZone("CST"));

        System.out.println("Here:  " + dfCST.format(now));
        try
        {
            System.out.println("Here (YYYY-MM-DD HH:mm:ss):  " + dmj.parse(dfCST.format(now)));
        }
        catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 It throws unparseble date exception. What am i doing wrong?
Any advice.

aks
0
Comment
Question by:aks143
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17971185
The format of dfCST.format(now) is not the same as the one specified in dmj.

A very simple program (see DF.java) gave me this output:

Here:   2006-11-18 13:05:00
Mexico: 18-nov-2006 6:05:00

So if you want to parse the output of the dfCST.format() method then you should declare a different SimpleDateFormat, namely:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");

Good luck.
Mark

PS: i have commented the code for re-parsing in the file.

-- DF.java
import java.text.*;
import java.util.*;

public class DF
{
    final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //final static SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");

    public static void main(String[] args)
    {
        try
        {
            //Parse date
            String sNow = "2006-11-18 13:05:00";
            java.util.Date now = sdf.parse(sNow);

            //Timezone Mexico/General
            DateFormat dfCST = DateFormat.getDateTimeInstance();
            dfCST.setTimeZone(TimeZone.getTimeZone("CST"));

            //Time in here
            System.out.println("Here:\t" + sdf.format(now));

            //Time in Mexico/General
            System.out.println("Mexico:\t" + dfCST.format(now));

            //Re-parse date
            //java.util.Date mexico = sdf2.parse(dfCST.format(now));
        }
        catch(ParseException e)
        {
            System.out.println("Parse exception!");
            e.printStackTrace();
        }
    }
}
0
 

Author Comment

by:aks143
ID: 17971203
even the following works for me now. what do u think?

        String nowday = "2006-11-17 18:52:00";
       
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date now = new java.util.Date();
        try{
             now = sim.parse(nowday);
        }catch(ParseException e){}    
       
       
        SimpleDateFormat dmj= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dmj.setTimeZone(TimeZone.getTimeZone("Mexico/General"));
       
        System.out.println("Here:  " + nowday);
        System.out.println("There (YYYY-MM-DD HH:mm:ss):  " + dmj.format(now));

I will test your code now.
0
 

Author Comment

by:aks143
ID: 17971211
in your code, the mexico time is in format Nov 18, 2006 6:05:00 AM
Now if i want to reformat it to yyyy-MM-dd HH:mm:ss, what should i do? Simply changing the SimpleDateFormat gives the parse exception..as was the case in my first code. But with my second code, it looks fine. What do u say?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Accepted Solution

by:
ADSLMark earned 50 total points
ID: 17971307
I would use your code. I only repaired your previous code for whatever reason. I made a note about re-parsing which was kinda strange, but still I fixed that (that was the question).

One remark, I would place the try/catch around all the code you put in your second post. It's just that if the parsing fails then you do not want to continue the rest of the statements, since they won't execute correctly (if not throw a runtime exception). So.. just change to:

try
{
        String nowday = "2006-11-17 18:52:00";
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date now = sim.parse(nowday);        

        SimpleDateFormat dmj= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dmj.setTimeZone(TimeZone.getTimeZone("Mexico/General"));
       
        System.out.println("Here:  " + nowday);
        System.out.println("There (YYYY-MM-DD HH:mm:ss):  " + dmj.format(now));
}
catch(ParseException e)
{
        System.out.println("Whoops! Parsing error!");
}

Good luck.
Mark
0
 

Author Comment

by:aks143
ID: 17971454
Yeah sure. The exception handling is very reqd. Thanks for your help. Regards, aks.
0
 

Author Comment

by:aks143
ID: 17971466
just a short question, is it good practise to use the time zone id OR short name.

with id -- TimeZone.getTimeZone("Mexico/General")
with short name -- TimeZone.getTimeZone("CST")

i think, id is preferred. Isn't it?
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17971692
Don't know. I guess i would use CST, since the timezones in which Mexico lies is unlikely to change, but they might decide to call it different (for example Mexico/Central)..

Anyway.. life time of the program probably does not outlive the time before such a thing is changed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 123
advertisement module in core php 4 144
github account with ecipse 1 42
custom annotations 9 29
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now