Search code examples
javadatedatetimetalenddata-conversion

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!


Solution

  • 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.