Skip to main content
Announcements
July 15, NEW Customer Portal: Initial launch will improve how you submit Support Cases. IMPORTANT DETAILS
cancel
Showing results for 
Search instead for 
Did you mean: 
JackStrong
Contributor
Contributor

How to keep datetime string value during applying the time zone

Hi Everyone.

I met challenge and I don't know how to resolved it.

The case is very easy:

  1. Read the file extracted by source system (eg. file name events_20210902120101.json).
  2. Modify the file this way that it is needed to put the timestamp value coming from the file name (20210902120101) into the new field/key in the source file. The new value in the new filed should be like this: "2021-09-02T12:01:01.000-07:00" (string because there is no date data type in JSON standard). It means that I need to leave the datetime string as it is (without subtraction hours) and then convert the whole value into the Pacific time zone (PST).

 

I tried to do this way:

TalendDate.formatDateInTimeZone("yyyy-MM-dd'T'HH:mm:ss.SSSXXX",TalendDate.parseDate("yyyyMMddHHmmss",context.dateTime), "PST")

but it causes that the value is not correct - "2021-09-02T03:01:01.000-07:00" (my time zone is +2:00 so this is why -9 hours was subtracted).

 

Please advice how to do that.

 

Best regards,

JackStrong

Labels (2)
3 Replies
root
Creator II
Creator II

@Michal Swiniarski​ 

From what I understand, any date/ time api's will look at the local JVM timezone.

 

I ran into a similar thing and ended up writing some custom java code... It is super crude and can be more efficient. But, it helps me with what I wanted to do.

 

Be very careful when you implement this as it can cause system level issues as the JVMs timezone will need to be switched back and forth... If you fail to switch to the original TZ, things will get real messy. You have been warned!

 

-----------

// Imports for tJava Advanced Settings

import java.util.TimeZone;

import java.time.format.DateTimeFormatter;

import java.time.OffsetDateTime;

import java.time.ZoneOffset;

 

 

SimpleDateFormat outputDateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" );

 

TimeZone original = TimeZone.getDefault();

System.out.println( "Original TZ ID: " + TimeZone.getDefault().getID() );

 

String dateFromDatabase = "2020-08-14T08:59:34.961+05:30";

Date tempOriginalDateNOW = new Date();

System.out.println( "Original Date: " + dateFromDatabase);

 

Date tempOriginalDate = outputDateFormat.parse( dateFromDatabase );

 

String tempOriginalDateFormatted = outputDateFormat.format(tempOriginalDate);

 

System.out.println( "Parsed dateFromDatabase: " + tempOriginalDateFormatted);

 

if ( dateFromDatabase.equalsIgnoreCase(tempOriginalDateFormatted) )

{

System.out.println( "Dates are matching - need not do anything" );

 

}

else

{

System.out.println( "Dates are NOT matching - do something" );

// Essentially, what we need to do is find the original Offset value (part towards the end - (+ / -) XX:YY, get appropriate TimeZone ID, and set that as the default timezone, and then, once conversion is completed, set it back to the default... This is because of how the current java.util.Date works...

 

 

// This is a modern API provided in Java8 and above

// https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html#getOffset--

OffsetDateTime fromdb = OffsetDateTime.parse(

dateFromDatabase, 

DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")

);

 

 

System.out.println("Offset from Original Date: " + fromdb.getOffset().getId());

 

// Here, we set the default timezone of the JVM to the offset zone received from the Source. This means, the system time is different now... 

// SUPER CAREFUL

TimeZone.setDefault(

TimeZone.getTimeZone(

ZoneOffset

.of( fromdb.getOffset().getId() )

)

);

 

System.out.println ( "New TZ based on OffSet: " + TimeZone.getDefault().getID() );

 

Date tempDate=outputDateFormat.parse(dateFromDatabase);

 

String date = outputDateFormat.format(tempDate);

System.out.println( dateFromDatabase );

System.out.println(date);

 

 

// Since the offset is different, we need to switch it back to what was the original one. That, so far, we know is Eastern time... so, that needs to be the same and verified.

TimeZone.setDefault(original);

System.out.println ( "Original system TZ : " + TimeZone.getDefault().getID() );

 

System.out.println(new Date() );

 

}

-----------

 

Hope this helps!

MattE
Creator II
Creator II

I couldn't get the Talend UTC functions to behave correctly so I've also written my own if it's any use to you

 

https://community.talend.com/s/feed/0D55b00006OYdkWCAT?language=en_US

tornilleiro
Contributor III
Contributor III

Hi,

The local settings of JVM over which Talend is running can affet the date funcions. I would suggest you to force in settings UTC , this will avoid that the functions uses your +2 setting.

0695b00000IdkBjAAJ.png 

Try if help you!

Regards