Java 需要在 Talend Studio 中获取当前日期时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30102527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:08:55  来源:igfitidea点击:

Need to Get the current DateTime in the Talend Studio?

javastringdatedatetimetalend

提问by Rajesh kannan

I am working the Talend studio tool for data migration. Now I want to set the Current DateTime in the Date field. I get the DateTime from this code TalendDate.getDate("yyyy-MM-dd HH:mm:ss")but it returns String type data. But I need Datetype to insert.Is there any String to date (Sample insert is like this:1999-12-13 16:14:48) conversion is in the Talend Studio.

我正在使用 Talend Studio 工具进行数据迁移。现在我想在日期字段中设置当前日期时间。我从这段代码中得到 DateTime,TalendDate.getDate("yyyy-MM-dd HH:mm:ss")但它返回 String 类型的数据。但我需要Date插入类型。迄今为止是否有任何字符串(示例插入是这样的:)1999-12-13 16:14:48转换在 Talend Studio 中。

采纳答案by Jordi Castilla

You can use routine function TalendDate.parseDateto convert a Stringto a Date.

您可以使用例程函数TalendDate.parseDate将 a 转换String为 a Date

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

If you want the current datetime:

如果你想要当前的日期时间:

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

But this makes no sense.

但这毫无意义。

Parsedate 函数准备接收字符串并将其转换为DateDate对象。DateDate对象有它自己的格式,所以你不必关心如何存储,you need to change the Dateformat in the moment you show it, but not when you store it您需要在显示时更改Date格式,而不是在存储时更改格式

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

When you need to show/print it use SimpleDateFormatfor example if you want to show 2015-07-05 16:00:00 you must do like this:

当您需要显示/打印它时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 ));

回答by kavi temre

its very simple with the use of DateFormat in java

在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);
    }