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,847

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

View Entire Topic
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