== UTC time and Java 1.3 ==
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jerome_Iffrig
Posted On:   Wednesday, August 7, 2002 03:41 AM

I need to covert dates into the UTC equivalent (e.g. number of seconds elapsed since epoch).


However the JAVA1.3 API documentation says that the static long Date.UTC(int year, int month, int date, int hrs, int min, int sec) method is predicated :o(

They ask to use the Calendar class instead, but I cannot figure out how to get the same result (easy to implement) as Date.UTC()


Has anyone done it already? Do you have a piece of code to show me?


Thanks!

Re: == UTC time and Java 1.3 ==

Posted By:   eimi_nos  
Posted On:   Wednesday, August 7, 2002 04:49 AM

I hope this helps.



import java.util.*;

static long getLongTime(int yr, int mon, int day, int hour,int min, int sec){
int year = yr;
int month = mon - 1;//MONTH is base zero.
TimeZone zone = TimeZone.getTimeZone("UTC");
Calendar ca = java.util.Calendar.getInstance(zone);
ca.set(ca.ERA, 1);
if(yr < 0){
ca.set(ca.ERA, 0);
year = -yr;
}
ca.clear();
ca.set(year, month, day, hour, min, sec);
ca.set(ca.MILLISECOND, 0);
Date date = ca.getTime();
return date.getTime();

}

About | Sitemap | Contact