Solved

Date conversion

Posted on 2004-03-24
24
183 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Starting to learn JAVA, 7 59
Problem to start Neon 20 86
what is a "java.lang.System Property"   ? 20 57
rhino JavaScript import, load 25 67
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

910 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

23 Experts available now in Live!

Get 1:1 Help Now