?
Solved

DateFormat timezone problem

Posted on 2006-11-18
7
Medium Priority
?
4,972 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 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