[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DateFormat timezone problem

Posted on 2006-11-18
7
Medium Priority
?
4,980 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
ADSLMark earned 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

873 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