on ‎2011 Jul 14 7:06 AM
Hi All,
I have an input date time iIhave to convert this to UTC for example
if its 1994-11-05T08:15:30-05:00 it has to be converted to 1994-11-05T13:15:30Z
Thanks
Request clarification before answering.
Hi Kiran ,
You can use Java UDF to covert the timeZone
// Suppose this is a date and time in the EST timezone
String value = "2006-11-28 09:45:12";
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df1.setTimeZone(TimeZone.getTimeZone("EST"));
// Parses the value and assumes it represents a date and time in the EST timezone
Date d = df1.parse(value);
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df2.setTimeZone(TimeZone.getTimeZone("CET"));
// Formats the date in the CET timezone
System.out.println(df2.format(d));
public static String convertLocalTimeToUTC(String p_city, String p_localDateTime) throws Exception{
String lv_dateFormateInUTC="";//Will hold the final converted date
Datelv_localDate = null;
Stringlv_localTimeZone ="";
SimpleDateFormat lv_formatter;
SimpleDateFormat lv_parser;
//Temp for testing(mapping of cities and timezones will eventually be in a properties file
if(p_city.equals("LON")){
lv_localTimeZone="Europe/London";
}else if(p_city.equals("NBI")){
lv_localTimeZone="EAT";
}else if(p_city.equals("BRS")){
lv_localTimeZone="Europe/Brussels";
}else if(p_city.equals("MNT")){
lv_localTimeZone="America/Montreal";
}else if(p_city.equals("LAS")){
lv_localTimeZone="PST";
}
//create a new Date object using the timezone of the specified city
lv_parser = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
lv_parser.setTimeZone(TimeZone.getTimeZone(lv_localTimeZone));
lv_localDate = lv_parser.parse(p_localDateTime);
//Set output format prints "2007/10/25 18:35:07 EDT(-0400)"
lv_formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss z'('Z')'");
lv_formatter.setTimeZone(TimeZone.getTimeZone(lv_localTimeZone));
System.out.println("convertLocalTimeToUTC: "+p_city+": "+" The Date in the local time zone " + lv_formatter.format(lv_localDate));
//Convert the date from the local timezone to UTC timezone
lv_formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
lv_dateFormateInUTC = lv_formatter.format(lv_localDate);
System.out.println("convertLocalTimeToUTC: "+p_city+": "+" The Date in the UTC time zone " + lv_dateFormateInUTC);
return lv_dateFormateInUTC;
}
/**
* Test method for {@link com.scratch.datetime.DateTimeUtil#convertUTCtoLocalTime(java.lang.String, java.lang.String)}.
*/
public final void testConvertUTCtoLocalTime() {
try {
assertEquals("testConvertLocalTimeToUTC:LON ", "03-11-2008 11:00:00 GMT(+0000)" ,DateTimeUtil.convertUTCtoLocalTime("LON", "03-11-2008 11:00:00"));
assertEquals("testConvertLocalTimeToUTC:NBI ", "03-11-2008 14:00:00 EAT(+0300)" ,DateTimeUtil.convertUTCtoLocalTime("NBI", "03-11-2008 11:00:00"));
assertEquals("testConvertLocalTimeToUTC:BRS ", "03-11-2008 12:00:00 CET(+0100)" ,DateTimeUtil.convertUTCtoLocalTime("BRS", "03-11-2008 11:00:00"));
assertEquals("testConvertLocalTimeToUTC:MNT ", "03-11-2008 06:00:00 EST(-0500)" ,DateTimeUtil.convertUTCtoLocalTime("MNT", "03-11-2008 11:00:00"));
assertEquals("testConvertLocalTimeToUTC:LAS ", "03-11-2008 03:00:00 PST(-0800)" ,DateTimeUtil.convertUTCtoLocalTime("LAS", "03-11-2008 11:00:00"));
} catch (Exception e) {
e.printStackTrace();
fail("convertUTCtoLocalTime: Exception :" + e);
}
}
public static Calendar getTimeZoneDate(Date userDate,String userTimeZone,String serverTimeZone){
Calendar serverCalendar=null;
Calendar localCalendar=new GregorianCalendar(TimeZone.getTimeZone(userTimeZone),Locale.US);
int DATE=userDate.getDay();
int MONTH=userDate.getMonth();
int YEAR=1900+userDate.getYear();
int HOUR_OF_DAY=userDate.getHours();
int MINUTE=userDate.getMinutes();
//int SECOND=userDate.getMinutes();
//localCalendar.set(YEAR,MONTH,DATE,HOUR_OF_DAY,MINUTE,SECOND);
localCalendar.set(Calendar.DATE, DATE);
localCalendar.set(Calendar.MONTH, MONTH);
localCalendar.set(Calendar.YEAR, YEAR);
localCalendar.set(Calendar.HOUR_OF_DAY, HOUR_OF_DAY); // 0..23
localCalendar.set(Calendar.MINUTE, MINUTE);
//localCalendar.set(Calendar.SECOND, SECOND);
BVLog.error("I'n in TimeZoneUtil-->"+localCalendar.getTime().toString());
if(serverTimeZone !=null){
serverCalendar=new GregorianCalendar(TimeZone.getTimeZone(serverTimeZone),Locale.ENGLISH);
}
else{
BVLog.error("I'n in TimeZoneUtil Else Part");
serverCalendar=new GregorianCalendar();
}
serverCalendar.setTimeInMillis(localCalendar.getTimeInMillis());
BVLog.error("I'n in TimeZoneUtil serverCalendar -->"+serverCalendar.getTime().toString());
return serverCalendar;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 9 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.