Solved

Date conversion

Posted on 2004-03-24
24
192 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
Gigs: Get Your Project Delivered by an Expert

Select from 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.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from 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

Suggested Solutions

Title # Comments Views Activity
Need to track down Infection in a Server 2008 domain user profile 7 51
VB Script to add site to Java Exception List 4 74
Java DateChooser? 3 36
servlet example 17 28
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…
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.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

813 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

14 Experts available now in Live!

Get 1:1 Help Now