如何在 Talend 作业中将当地时间转换为 UTC,反之亦然 [英] How can I convert local time to UTC and vice versa in a Talend job

查看:33
本文介绍了如何在 Talend 作业中将当地时间转换为 UTC,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库(不是 SQL),其中的时间字段都填充了本地时间.我有一个带有 UTC 时间字段的数据库 (SQL).现在我想在这些数据库之间交换信息,但只有当我可以从本地时间转换为 UTC 时,我才能实现这一点,反之亦然.我如何在 Talend 中实现这一目标?

I have a database (not SQL) with time fields all filled with local time. And I have a database (SQL) with time fields in UTC. Now I want to exchange information between those databases, but I can only realize this if I can convert from local time to UTC and vice versa. How can I achieve this in Talend?

我知道本地时间数据库中的数据,是荷兰本地时间.(GMT +1(冬季)GMT +2(夏季))

I know the data in the local time database, is the local time in the Netherlands. (GMT +1 (winter) of GMT +2 (summer))

Examples:
23-10-2015 16:00 Local time => 23-10-2015 14:00 UTC  (and vice versa)
26-10-2015 16:00 Local time => 26-10-2015 15:00 UTC  (and vice versa)

推荐答案

下面的截图有一个

tFixedFlowInput_1 - 使用 localDateTime(已填充)和 utcDateTime(未填充)定义架构

tFixedFlowInput_1 - define a schema with localDateTime (populated) and utcDateTime (unpopulated)

tJavaRow_1 - 在 localDateTime 上执行中央/欧洲到 UTC 时区的对话并填充 utcDateTime.这是唯一必不可少的部分.

tJavaRow_1 - performs Central/Europe to UTC timezone conversation on localDateTime and populates utcDateTime. This is the only essential piece.

tLogRow_1 - 显示结果

tLogRow_1 - shows the results

接下来,为 tFixedFlowInput 设置架构并添加一些数据

Next, setup the schema for the tFixedFlowInput and add some data

下一步...设置 tJavaRow_1 组件

Next... setup the tJavaRow_1 component

tJavaRow_1 高级设置/导入如下:

tJavaRow_1 Advanced Settings / Imports are below:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.text.ParseException;

tJavaRow_1 基本设置(真实代码)如下:

tJavaRow_1 Basic Settings (the real code) is below:

请注意,try 和 catch 块已被注释掉,以便抛出异常并且 Talend 作业可以处理它们.

Note that the try and catch block have been commented out so that exceptions are thrown and the Talend Job can handle them.

使用了两个 SimpleDateFormat 实例,每个实例都与一个时区相关联.localDateTime 是通过源格式化程序解析的.然后,目标格式化程序用于将日期转换为 UTC 并将其作为原始格式的字符串返回.从 UTC 返回本地是一个微不足道的变化.

Two instances of SimpleDateFormat are used, each associated with a time zone. The localDateTime is parsed through the source formatter. Then, the target formatter is used to convert the date to UTC and return it as a string in the original format. To go from UTC back to local is a trivial change.

String BASE_FORMAT = "dd-MM-yyyy HH:mm";
TimeZone utcTZ = TimeZone.getTimeZone("UTC");
TimeZone ceTZ = TimeZone.getTimeZone("Europe/Amsterdam");

SimpleDateFormat formatUTC = new SimpleDateFormat( BASE_FORMAT );
formatUTC.setTimeZone(utcTZ);

SimpleDateFormat formatCE = new SimpleDateFormat( BASE_FORMAT );
formatCE.setTimeZone(ceTZ); 

output_row.localDateTime = input_row.localDateTime;

// Commented out the try and catch, so the exception is thrown to Talend job
//try {
    Date dateTimeLocal = formatCE.parse(input_row.localDateTime);
    output_row.utcDateTime = formatUTC.format(dateTimeLocal);
//}       
//catch (ParseException pe) {
//  System.out.println( pe.getMessage());
//}

接下来,tLogRow_1 只显示流上的数据.这是使用示例数据运行的示例.

Next, the tLogRow_1 just displays the data on the flow. Here is an example from running with the sample data.

这篇关于如何在 Talend 作业中将当地时间转换为 UTC,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆