One Star

Adding the local Date-Time without Time Zone

Hi,
in a tMap I need to write the DateTime String, but without Timezone. It means the Greenwitch time.
The TalendDate.getDate("yyyy-MM-dd HH:mm:ss") geve to me the local time, obviously.
Thank you,
regards.
4 REPLIES
Six Stars

Re: Adding the local Date-Time without Time Zone

The java Date object has no concept of timezone. The issue is converting the Date either using SimpleDateFormat or Date.toString(). In this case, it uses the local timezone in order to translate the Date (which is nothing more than the number of milliseconds since January 1st 1970, UTC) into a formatted date string.
Your best bet is to write a custom code routine and modify the TalendDate.getDate() code. You will need to set the TimeZone in the Calendar instance. For example:
replace this part of the code:
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.format(Calendar.getInstance().getTime(), result, new FieldPosition(0));
return result.toString();

with this:
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.format(Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime(), result, new FieldPosition(0));
return result.toString();
Seven Stars

Re: Adding the local Date-Time without Time Zone

The way I came up with to do this is:
Calendar c = Calendar.getInstance();
TimeZone tz = c.getTimeZone();
Long ms = c.getTimeInMillis();
Date dt = new Date(ms-tz.getOffset(ms));
One Star

Re: Adding the local Date-Time without Time Zone

The java Date object has no concept of timezone. The issue is converting the Date either using SimpleDateFormat or Date.toString(). In this case, it uses the local timezone in order to translate the Date (which is nothing more than the number of milliseconds since January 1st 1970, UTC) into a formatted date string.
Your best bet is to write a custom code routine and modify the TalendDate.getDate() code. You will need to set the TimeZone in the Calendar instance. For example:
replace this part of the code:
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.format(Calendar.getInstance().getTime(), result, new FieldPosition(0));
return result.toString();

with this:
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.format(Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime(), result, new FieldPosition(0));
return result.toString();


Thank you. How Can I do this? Sorry, I've not modified the code in Talend.
The way I came up with to do this is:
Calendar c = Calendar.getInstance();
TimeZone tz = c.getTimeZone();
Long ms = c.getTimeInMillis();
Date dt = new Date(ms-tz.getOffset(ms));


Can I use this code to a tJava and assign to a context variable?
EDIT: SOLVED with alevy's solution. I've created a routine with a simple class who implements the method. Then I callend him in tJava.
Only one question. If I want to consider Time Zone in addition to uneversal Time?
Seven Stars

Re: Adding the local Date-Time without Time Zone

How do you mean?