Solved

Date conversion

Posted on 2004-03-24
24
201 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

840 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