Solved

DateFormat timezone problem

Posted on 2006-11-18
7
4,956 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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 …

747 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

13 Experts available now in Live!

Get 1:1 Help Now