Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi Everyone.
I met challenge and I don't know how to resolved it.
The case is very easy:
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
@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!
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
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.
Try if help you!
Regards