在Talend Studio中如何获取当前日期和时间?

5

我正在使用Talend Studio工具进行数据迁移。现在我想在日期字段中设置当前日期时间。我从此代码获取日期时间:TalendDate.getDate("yyyy-MM-dd HH:mm:ss"),但它返回字符串类型的数据。但我需要Date类型来插入数据。在Talend Studio中是否有将字符串转换为日期时间(样本插入如下: 1999-12-13 16:14:48)的方法。

2个回答

18

您可以使用常规函数TalendDate.parseDateString转换为Date

TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", yourStringData);

如果您想获取当前日期和时间:

TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss"));

但这没有意义。

ParseDate函数准备接收一个字符串并将其转换为Date对象。Date对象有自己的格式,所以您无需关心它是如何存储的,您需要在显示日期时更改Date格式,而不是在存储日期时更改格式

// this will produce a correct Date Object to store in your Date field
Date currentDate = TalendDate.getCurrentDate();  

当你需要显示/打印它时,请使用SimpleDateFormat,例如,如果您想显示2015-07-05 16:00:00,则必须这样做:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss);
System.out.println("My date formatted is: " + sdf.format(currentDate ));

TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",TalendDate.getDate("yyyy-MM-dd HH:mm:ss"))这段代码可以使用吗?Jordi先生。 - Rajesh kannan
我想要插入当前日期和时间,格式为“yyyy-MM-dd HH:mm:ss”。那么我应该使用这个TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",TalendDate.getDate("yyyy-MM-dd HH:mm:ss")). @Jordi Castilla - Rajesh kannan
请阅读我的更新答案,你错过了重要的内容,希望能够澄清。 - Jordi Castilla

0

使用Java中的DateFormat非常简单

public static void convert(String inputDate) throws ParseException {

        DateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        Date d = format.parse(inputDate); // example 1999-12-13 16:14:48
                System.out.println(d);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接