[resolved] Set TimeZone

One Star

[resolved] Set TimeZone

Hi,
I have a CSV that I'm importing to MySQL.
The time field is in 'Local Time'; however before I import to the table I need to convert it to UTC time.
My TimeZone is 5:30 UTC. Therefore I need to -5:30 from the mentioned time in the CSV and then import.
I tried this
http://www.talendforge.org/forum/viewtopic.php?id=9322
but it ain't correct. In fact I need to opposite of this.
Thanx in advance.
John

Accepted Solutions
One Star

Re: [resolved] Set TimeZone

Thanks for the replies; I kind of modified the existing code that I had looked up here.
http://www.talendforge.org/forum/viewtopic.php?id=9322
So, my date/time format is thus
2012-01-19 16:26:16
I needed to convert the time to -5:30
Therefore the time (prior to import) needs to be this
2012-01-19 10:56:16
Below is the code I used, successfully.
================================================
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.HOUR_OF_DAY, -5);
cal.add(java.util.Calendar.MINUTE, -30);
return cal.getTime();
}
}
=================================================
Hope it helps anyone who has the same problems.
John

All Replies
Four Stars

Re: [resolved] Set TimeZone

Pl check this post, you will get idea
http://www.talendforge.org/forum/viewtopic.php?id=30482
If you are sure that you want to convert it to timezone is UTC, then why not to just add -5.30 to existing timestamp?
what is the format of your incoming date?
Vaibhav
Six Stars

Re: [resolved] Set TimeZone

The JVM will try to determine the correct TZ on it's own. On Windows, this is pretty straight forward. On Linux, there are several ways (locale,etc) and it can be hit or miss. Simply set the JVM timezone yourself in the load job, eg:
System.setProperty("user.timezone","America/Chicago");
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("America/Chicago"));
Then write the values to a MySQL TIMESTAMP column, this way MySQL will convert and store it in UTC for you. It will convert it back to the TZ of the local JVM when querying through JDBC, all magical like.
One Star

Re: [resolved] Set TimeZone

Thanks for the replies; I kind of modified the existing code that I had looked up here.
http://www.talendforge.org/forum/viewtopic.php?id=9322
So, my date/time format is thus
2012-01-19 16:26:16
I needed to convert the time to -5:30
Therefore the time (prior to import) needs to be this
2012-01-19 10:56:16
Below is the code I used, successfully.
================================================
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.HOUR_OF_DAY, -5);
cal.add(java.util.Calendar.MINUTE, -30);
return cal.getTime();
}
}
=================================================
Hope it helps anyone who has the same problems.
John