[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

DateFormat timezone problem

Posted on 2006-11-18
7
Medium Priority
?
4,975 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
Industry Leaders: 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

649 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