on 06-28-2010 5:32 AM
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);
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
User | Count |
---|---|
84 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.