cancel
Showing results for 
Search instead for 
Did you mean: 

Date Transfer: UTC time zone to PEK time zone

daixiong_jiang3
Active Participant
0 Kudos

Hi I am new to Java programming so could you pls help to provide the java code for a date transfer?

I have the input date such as "2004-08-07 09:07:00.0" need to be transfered to "2004-08-07 17:07:00.0",in other words:

output date = input date + 8 hrs.

I have the following code but it does not work

public String ZUTC2PEK(String Input_Date, Container container) throws StreamTransformationException
{
       Date date = new Date(Input_Date);

       DateFormat utcFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
       DateFormat pekFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

       TimeZone utcTime = TimeZone.getTimeZone("UTC");
       TimeZone pekTime = TimeZone.getTimeZone("PEK");
       
       utcFormat.setTimeZone(pekTime);
       pekFormat.setTimeZone(utcTime);
       return pekFormat.format(date);  
}

Accepted Solutions (1)

Accepted Solutions (1)

rajasekhar_reddy14
Active Contributor
0 Kudos

Hi ,

You have to use import java.util.GregorianCalendar; to convert one time region to other

checkout this example code,a lter this as per your requirement

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
  public static void main(String[] args) {
    Calendar localTime = Calendar.getInstance();
    localTime.set(Calendar.HOUR, 17);
    localTime.set(Calendar.MINUTE, 15);
    localTime.set(Calendar.SECOND, 20);

    int hour = localTime.get(Calendar.HOUR);
    int minute = localTime.get(Calendar.MINUTE);
    int second = localTime.get(Calendar.SECOND);

    System.out.printf("Local time  : %02d:%02d:%02d\n", hour, minute, second);

    Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany"));
    germanyTime.setTimeInMillis(localTime.getTimeInMillis());
    hour = germanyTime.get(Calendar.HOUR);
    minute = germanyTime.get(Calendar.MINUTE);
    second = germanyTime.get(Calendar.SECOND);

    System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);
  }
}

Regards,

Raj

daixiong_jiang3
Active Participant
0 Kudos

Raj,

thanks for your reply.

But as i mentioned, I am new to Java programming so at present I do not know how to incorporate your code into my user defined function in PI,could you pls show more details?

Thanks.

Former Member
0 Kudos

Hi,

Create one simple UDF with 2 input parameters, input parameter names are sourceDate and hours. Source date means current date wht ever you want to give the input and hours is howmany hours you want to add to the current date (in this case 8).

After giving the parameters click on crete function and copy this code.

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

Calendar cal = Calendar.getInstance();

try

{

Date requiredDate = (Date)df.parse(sourceDate);

cal.setTime(requiredDate);

cal.add(Calendar.HOUR, Integer.parseInt(hours));

}

catch(Exception e)

{

}

return (df.format(cal.getTime()));

Regards

Ramesh

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi

Did u include the below in the import section?

java.util.Date;java.text.SimpleDateFormat;java.text.DateFormat;

Regards

Ramg

daixiong_jiang3
Active Participant
0 Kudos

Ramg,

Yes, I have imported "java.util.Date;java.text.SimpleDateFormat;java.text.DateFormat" as you mentioned,and the code is also coming from you, but when i do a test i go the following exception:

com.sap.aii.utilxi.misc.api.BaseRuntimeException: Exception:[java.lang.IllegalArgumentException] in class

thanks a lot.