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