== Date handling, UTC ... I am lost ==
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   joey_corleon
Posted On:   Saturday, October 12, 2002 04:48 AM

Hi, I am developing a web application in which I convert all the dates into UTC (a unsigned long representing a date) for storing in a database (in the database, the dates are represtned as UTC). How shall I do this? The major bean provides me with: [year][month][day][hour][minute][seconds] The JAVA doc says: public static long UTC(int year, int month, int date, int hrs, int min, int sec) Deprecated . As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min, sec) or GregorianCalendar(year + 1900, month, date, hrs, min, sec), using a UTC TimeZone, followed by Calendar.getTime().getTime() But I don't get this - Do anyone has a littl   More>>

Hi,


I am developing a web application in which I convert all the dates into UTC (a unsigned long representing a date) for storing in a database (in the database, the dates are represtned as UTC).


How shall I do this? The major bean provides me with:
[year][month][day][hour][minute][seconds]


The JAVA doc says:


public static long UTC(int year, int month, int date, int hrs, int min, int sec)

Deprecated . As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min, sec) or GregorianCalendar(year + 1900, month, date, hrs, min, sec), using a UTC TimeZone, followed by Calendar.getTime().getTime()


But I don't get this - Do anyone has a little sample code (a little course :o) ) on how I can convert a date into UTC - and vis-versa?


One more question I have: How shall in this case handle the TimeZone of a user on the other side of earth? e.g. Shall I store the date as its timezone representation, or shall I store everything a LOCAL time (e.g. GMT 0) then translate it into the right timeZone when needed to be displayed to a given user on a particular part of the world?


Thanks for your help.

   <<Less

Re: == Date handling, UTC ... I am lost ==

Posted By:   Ivo_Limmen  
Posted On:   Saturday, October 12, 2002 08:19 AM

Yes, dates are a big issue in Java.
To get your UTC value of date:

long utc = myDate.getTime();

Converting your UTC to a date is simple:

Date myDate = new Date(myUtc);

When you have a month, year, ect. converting it into a UTC will be troublesome:

Calendar calendar = new GregorianCalendar(myYear, myMonths, myDate, myHours, myMinutes, mySeconds);

// Get the date from the calendar
// Get the utc from the date
long utc = calendar.getTime().getTime();
About | Sitemap | Contact