[resolved] Convert Historical UTC Time in Local Time with Daylight Rules

One Star

[resolved] Convert Historical UTC Time in Local Time with Daylight Rules

Hello,
exist a java function to convert a UTC Datetime to Lokal Time?
I want to tell the function a historical date an want to have back the local time.
The function must check the date and decide it is summertime or wintertime...
Thanks
Sebastian1234
Community Manager

Re: [resolved] Convert Historical UTC Time in Local Time with Daylight Rules

Hello guy
Can you give us an example to describe your requirment?
Best
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] Convert Historical UTC Time in Local Time with Daylight Rules

Hello,
from a database (MS SQL Server 2008) i retrive data with datetime cols.
This datetime values are all in UTC.
On SQL Server exist no function to convert this UTC to Local Time.
So I think i can do this with a talend build in function.
There is no function. Ok i can write. But i'm really bad in java.
Expample Data
2008-05-13 15:14
2008-02-13 15:14
This is UTC. The Function should convert to Europe/Amsterdam.
2008-05-13 17:14 (Take Attantion of Daylight!! Summertime)
2008-05-13 16:14
From the last sunday in march to the last sunday in october it is +2 the rest of time it is +1.
This only correct for my timezone!
newfunction_utc_to_localtime("2009-05-13 15:14", "Europe/Amsterdam")
Can you really help?
I do a bad workaround: I export to csv and convert with python, cause im not good in sql and java.
But this is quick and dirty...
Community Manager

Re: [resolved] Convert Historical UTC Time in Local Time with Daylight Rules

Hello
You can write some jave code to convert UTC to local time, go to 'Repository-->Code-->Routines' and create a new routine, let's calle it as: F9322 and then call this routine on tMap.
// template routine Java
package routines;
import java.util.Date;
public class F9322 {
static java.util.Calendar cal = java.util.Calendar.getInstance();
static int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
static int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
public static Date convertUTCtoLocalTime(Date date) {
cal.setTime(date);
cal.add(java.util.Calendar.MILLISECOND, (zoneOffset + dstOffset));
return cal.getTime();
}
}

Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] Convert Historical UTC Time in Local Time with Daylight Rules

Hello,
YES! This is want i want!
Thanks for your fast answer.
I will have a try!
Sebastian1234
One Star

Re: [resolved] Convert Historical UTC Time in Local Time with Daylight Rules

Sorry for replying to an old thread, but thought I'd add to this because I came across this thread in my search to figure out how to convert UTC to Local Time. The code that Shong has provided didn't totally work for me because it's calculating the DSTOffSet based on the current time. Rather, I believe we want to calculate the DSTOffset based on the incoming date that is trying to be converted. At least, for my use case, this is what I believe i need.
So, here's my revised code, note that I put the DSTOffSet calculation after the call to cal.setTime, which I believe now makes the DSTOffset value correct.

package routines;
import java.util.Date;
public class convert_utc_local {
static java.util.Calendar cal = java.util.Calendar.getInstance();
static int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
// static int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
public static Date convertUTCtoLocalTime(Date date) {
cal.setTime(date);
int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
cal.add(java.util.Calendar.MILLISECOND, (zoneOffset + dstOffset));
return cal.getTime();
}
}
One Star

Re: [resolved] Convert Historical UTC Time in Local Time with Daylight Rules

Got this working:
// template routine Java
package routines;
import java.util.Date;
public class UserDefined {
static java.util.Calendar cal = java.util.Calendar.getInstance();
static int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
static int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
/**
* convertUTCtoLocalTime: converts a UTC time given to local time
*
*
* {talendTypes} Date
*
* {Category} User Defined
*
* {param} date type: The date to convert
*
* {example} convertUTCtoLocalTime(sysdate) # returns date converted to local time
*/
public static Date convertUTCtoLocalTime(Date date) {
cal.setTime(date);
cal.add(java.util.Calendar.MILLISECOND, (zoneOffset + dstOffset));
return cal.getTime();
}

/**
* convertLocalTimetoUTC: converts a local time given to UTC time
*
*
* {talendTypes} Date
*
* {Category} User Defined
*
* {param} date type: The date to convert
*
* {example} convertLocalTimetoUTC(sysdate) # returns date converted to local time.
*/
public static Date convertLocalTimetoUTC(Date date) {
cal.setTime(date);
cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
return cal.getTime();
}
}