Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Date conversion

Posted on 2004-03-24
24
Medium Priority
?
220 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

963 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