Solved

Date conversion

Posted on 2004-03-24
24
214 Views
Last Modified: 2010-03-31
How can I convert the result of java.util.Date() to CST(CentralStandardTime) considering daylight savings.
0
Comment
Question by:HOSROW
[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
  • 9
  • 6
  • 3
  • +2
24 Comments
 

Author Comment

by:HOSROW
ID: 10673011
How can I convert the result of java.util.Date() to CST(CentralStandardTime) considering daylight savings.
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 50 total points
ID: 10673053
Use GregorianCalendar
0
 

Expert Comment

by:jbas
ID: 10673494
are you want this?

import java.util.Date;
import java.text.*;

public class TTestDate
{
      public static void main(String[] args)
      {
            SimpleDateFormat bartDateFormat =
            new SimpleDateFormat(""yyyy-MM-dd-EEEE hh:mm:ss);

            Date date = new Date();

            System.out.println(bartDateFormat.format(date));
      }
}
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 92

Expert Comment

by:objects
ID: 10674091
The Date class does not store any timezone information.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10674125
Here's an example:

 // get the supported ids for GMT-08:00 (Pacific Standard Time)
 String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
 // if no ids were returned, something is wrong. get out.
 if (ids.length == 0)
     System.exit(0);

 // create a Pacific Standard Time time zone
 SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);

// get the supported ids for GMT-06:00 (Central Standard Time)
 ids = TimeZone.getAvailableIDs(-6 * 60 * 60 * 1000);

 // create a Central Standard Time time zone
 SimpleTimeZone cdt = new SimpleTimeZone(-6 * 60 * 60 * 1000, ids[0]);

 // set up rules for daylight savings time
 pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
 pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);

 // create a GregorianCalendar with the Pacific Daylight time zone
 // and the current date and time
 Calendar calendar = new GregorianCalendar(pdt);

Date myDateInMyTimeZone = new Date();
 calendar.setTime(myDateInMyTimeZone);

 // print out a bunch of interesting things
 System.out.println("ERA: " + calendar.get(Calendar.ERA));
 System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
 System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
 System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
 System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
 System.out.println("DATE: " + calendar.get(Calendar.DATE));
 System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
 System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
 System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
 System.out.println("DAY_OF_WEEK_IN_MONTH: "
                    + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
 System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
 System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
 System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
 System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
 System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
 System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
 System.out.println("ZONE_OFFSET: "
                    + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
 System.out.println("DST_OFFSET: "
                    + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));

calendar.setTimeZone(cdt);

// print out a bunch of interesting things
 System.out.println("ERA: " + calendar.get(Calendar.ERA));
 System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
 System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
 System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
 System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
 System.out.println("DATE: " + calendar.get(Calendar.DATE));
 System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
 System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
 System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
 System.out.println("DAY_OF_WEEK_IN_MONTH: "
                    + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
 System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
 System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
 System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
 System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
 System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
 System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
 System.out.println("ZONE_OFFSET: "
                    + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
 System.out.println("DST_OFFSET: "
                    + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));

Presto!
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10674131
Oh, btw, the code above is adapted from the Javadocs in java.util.Calendar
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10675892
           SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy zzz");
            sdf.setTimeZone(TimeZone.getTimeZone("CST"));
            System.out.println(sdf.format(new Date()));
0
 
LVL 92

Expert Comment

by:objects
ID: 10675927
>  SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy zzz");
>   sdf.setTimeZone(TimeZone.getTimeZone("CST"));
>     System.out.println(sdf.format(new Date()));

That just displays it, it doesn't actually convert anything.

You need to use the Calendar class as already mentioned.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10675995
SimpleDateFormat already has an internal Calendar
0
 
LVL 92

Expert Comment

by:objects
ID: 10676083
So??? It's not for doing conversions, nor can it be used for that purpose.
And even if it could it seems silly to create a SDF just to use it's 'internal' Calendar.
orangehead911's code will show you how to do conversion.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10676150
>>It's not for doing conversions

Are you sure? ;-)

>>And even if it could it seems silly to create a SDF just to use it's 'internal' Calendar.

Why? - since

a. you've probably got to format it anyway at some point
b. it's more likely to be error free if you let Sun do it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10858305
>>Accept: orangehead911 {http:#10673053}

Why so? - there's no need to use anything other than a DateFormat since it's almost certain that a DateFormat will be needed anyway
0
 
LVL 92

Expert Comment

by:objects
ID: 10864653
The question doesn't ask about formatting the date, the Calendar class is used for timezone conversions. DateFormat is not required.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10864663
As explained already the DateFormat class is almost certainly going to be needed. It already contains a Calendar
0
 
LVL 92

Expert Comment

by:objects
ID: 10864682
> . It already contains a Calendar

But its a lot easier and more flexible to just create your own Calendar providing you with full access to it's functionality. The internal Calendar in DateFormat is not intended for being used to do custom timezone conversions.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10864706
TimeZone conversion is actually one of the most important functions of the contained Calendar. You don't need to mess around with your own Calendar

sdf.setTimeZone(TimeZone.getTimeZone("CST"));
sdf.format(date);

will fulfil the requirement
0
 
LVL 92

Expert Comment

by:objects
ID: 10864716
The code posted by orangehead911 provides a far more flexible and efficient solution
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10866792
LOL
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11703049
As mentioned earlier - a Calendar is unnecessary for converting between time zones
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11703501
ok ;-)
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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

622 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