Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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.
Labels (2)
4 Replies
TXAggie00
Contributor III
Contributor III

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();
alevy
Specialist
Specialist

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));
Anonymous
Not applicable
Author

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?
alevy
Specialist
Specialist

How do you mean?