cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Date and Time format in UTC java UDF

Former Member
0 Likes
2,845

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Likes

CHK THIS:

input will be var1

execution type: single value..



String date1="";
String date2="";
String date3="";
    date1=var1.substring(0,10);
    date2=var1.substring(11,19);
   date3=date1+"T"+date2+"Z";
return date3;

Mapping:

Date -> UDF -> Output Date

Former Member
0 Likes

Amitsri , thanks but this looks normal concating z to the existing one .....I have to add +4 hours to existing time ..that means iam converting input to UTC format...thanks

former_member854360
Active Contributor
0 Likes

Hi Kran,

I have tested this code.

Here you go for your exact requirement.

Your input should be only one to this UDF p_localDateTime

And do use substring to provide the input to dd-MM-yyyy HH:mm:ss format before passing it to the UDF.

Example : 1994-11-05T08:15:30-05:00 Use substring and concat to convert it to dd-MM-yyyy HH:mm:ss format 1994-11-05 08:15:30

Also in Function tab of message mapping editor import the additional thing.

java.text.SimpleDateFormat

java.util.Date


public String converttoUTC(String p_localDateTime, Container container) throws StreamTransformationException{
String lv_dateFormateInUTC="";//Will hold the final converted date   
Date lv_localDate = null;   
String lv_localTimeZone ="";   
SimpleDateFormat lv_formatter;   
SimpleDateFormat lv_parser;   
    

  
//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("EST"));   
try
{
lv_localDate = lv_parser.parse(p_localDateTime);   
}
catch (java.text.ParseException e) 
{
}
  
//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));   
  
  
  
//Convert the date from the local timezone to UTC timezone   
lv_formatter.setTimeZone(TimeZone.getTimeZone("UTC"));   
lv_dateFormateInUTC = lv_formatter.format(lv_localDate);   

  
  
return lv_dateFormateInUTC;  
}

Former Member
0 Likes

Hi I tried but iam getting this error:

'class' or 'interface' expected ^

Iam using the code as below:

Imports java.util.Date;java.text.SimpleDateFormat;

public String UTC(String p_localDateTime ,Container container){

String lv_dateFormateInUTC="";//Will hold the final converted date

Date lv_localDate = null;

String lv_localTimeZone ="";

SimpleDateFormat lv_formatter;

SimpleDateFormat lv_parser;

//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("EST"));

try

{

lv_localDate = lv_parser.parse(p_localDateTime);

}

catch (java.text.ParseException e)

{

}

//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;

}

former_member854360
Active Contributor
0 Likes

Hi Kiran,

Dont write import statement in UDF.

In function tab of message mapping editor add it under the standard imports.

Click on + sign and add to addtinal import.Also dont write import statement in prefix to it

Only write

java.text.SimpleDateFormat

java.util.Date.*

Also find the Updated UDF .

Here you dont need to use any sybstring or Concat

INPUT to UDF: 1994-11-05T08:15:30-05:00

OUTPUT from UDF: 1994-11-05T13:15:30Z


public String calculate(String p_localDateTime, Container container) throws StreamTransformationException{
String lv_dateFormateInUTC="";//Will hold the final converted date   
Date lv_localDate = null;   
String lv_localTimeZone ="";   
SimpleDateFormat lv_formatter;   
SimpleDateFormat lv_parser;   

String date1="";
String date2="";
String date3="";
    date1=p_localDateTime.substring(0,10);
    date2=p_localDateTime.substring(11,19);
   date3=date1+" "+date2;


    

  
//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("EST"));   
try
{
lv_localDate = lv_parser.parse(date3);   
}
catch (java.text.ParseException e) 
{
}
  
//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));   
  
  
  
//Convert the date from the local timezone to UTC timezone   
lv_formatter.setTimeZone(TimeZone.getTimeZone("UTC"));   
lv_dateFormateInUTC = lv_formatter.format(lv_localDate);   

   date1="";
 date2="";
date3="";
    date1=lv_dateFormateInUTC.substring(0,10);
    date2=lv_dateFormateInUTC.substring(11,19);
   date3=date1+"T"+date2+"Z";
return date3;
}

Edited by: Debashish on Jul 14, 2011 9:13 AM

Edited by: Debashish on Jul 14, 2011 9:18 AM

Former Member
0 Likes

Thanks for this..this is almost there ....but its converting 1994-11-05 08:15:30 to 17-04-0011T13:15:30Z

former_member854360
Active Contributor
0 Likes

Hi Kiran,

Dont use any substring or concat.

Directly provide 1994-11-05T08:15:30-05:00 as input to the UDF without any conversion and enjoy..........

Former Member
0 Likes

nah still same 17-04-0011T13:15:30Z

former_member854360
Active Contributor
0 Likes

Sorry Kiran,

use this and please let me know if it works.

Problem was because of dateformat.

String lv_dateFormateInUTC="";//Will hold the final converted date   
Date lv_localDate = null;   
String lv_localTimeZone ="";   
SimpleDateFormat lv_formatter;   
SimpleDateFormat lv_parser;   

String date1="";
String date2="";
String date3="";
    date1=p_localDateTime.substring(0,10);
    date2=p_localDateTime.substring(11,19);
   date3=date1+" "+date2;


    

  
//create a new Date object using the timezone of the specified city   
lv_parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
lv_parser.setTimeZone(TimeZone.getTimeZone("EST"));   
try
{
lv_localDate = lv_parser.parse(date3);   
}
catch (java.text.ParseException e) 
{
}
  
//Set output format prints "2007/10/25  18:35:07 EDT(-0400)"   
lv_formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z'('Z')'");   
lv_formatter.setTimeZone(TimeZone.getTimeZone(lv_localTimeZone));   
  
  
  
//Convert the date from the local timezone to UTC timezone   
lv_formatter.setTimeZone(TimeZone.getTimeZone("UTC"));   
lv_dateFormateInUTC = lv_formatter.format(lv_localDate);   

 date1="";
 date2="";
date3="";
    date1=lv_dateFormateInUTC.substring(0,10);
    date2=lv_dateFormateInUTC.substring(11,19);
   date3=date1+"T"+date2+"Z";
return date3;

Former Member
0 Likes

hey cool man , you nailed it...got the final one...I might have to twaek it a bit after i get exact format..thanks dude.....

Former Member
0 Likes

I have the same problem on the date, how you solved with the UDF function?


thanks

umberto

Answers (1)

Answers (1)

former_member854360
Active Contributor
0 Likes

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;   
}

Former Member
0 Likes

I saw two of these in a Java forum...could not make it work...

My input is example 1994-11-05T08:15:30-05:00 I have to convert that to 1994-11-05T13:15:30Z (output is in UTC)

former_member854360
Active Contributor
0 Likes

Hi Kiran,

You need to know your input timeZone as well.

Please let me know your input timeZone i will give you the Exact code.

Former Member
0 Likes

Company is in US so it will be EST