简体   繁体   中英

Talend - Transform "EEE MMM dd hh:mm:ss z yyyy" in "yyyy-MM-dd"

I receive a date from a file in this format: "EEE MMM dd hh:mm:ss z yyyy" and I'm trying to convert this value into Date "yyyy-MM-dd". For that I'm using:

TalendDate.parseDate("yyyy/MM/dd", TalendDate.formatDate("yyyy/MM/dd", TalendDate.parseDate("EEE MMM dd hh:mm:ss z yyyy",context.date)))

The context.date is defined here:

context.date = input_row.mtime_string;

But when I run my JavaRow component I get the following error:

Exception in component tJavaRow_1

"java.lang.RuntimeException: java.text.ParseException: Unparseable date: "Thu Aug 09 10:38:45 BST 2018"

How can I solve this?

Many Thanks!

You could achieve the format using the below code snippet -

System.out.println(input_row.newColumn);
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
Date date = parserSDF.parse(input_row.newColumn);
String dDate = null; 
parserSDF = new SimpleDateFormat("yyyy-MM-dd");
dDate = parserSDF.format(date);
System.out.println(dDate);

Also, you need the below libraries to be imported(Advanced settings section of tJavaRow)-

import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Date;

I had directly passed the input value from file(in my scenario tFileInputDelimited ) into tJavaRow as - input_row.newColumn and then used SimpleDateFormat class to both parse and format dates according to the formatting pattern.

Read more here .

If you want to convert the date into LocalDate then below code might help:

private LocalDate getLocalDate(String date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd hh:mm:ss z yyyy", Locale.getDefault());
        return LocalDate.parse(date, formatter);
    }

And once you get the LocalDate, you can transform it to any format. As the question expects yyyy-MM-dd then just call toString() on LocalDate object.

LocalDate curr = LocalDate.now();

System.out.println(curr.toString());

It will display the date like "2019-11-20".

Hope this helps to someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM