Search code examples
javadatetalend

parsing IST date format


I'm using Talend 5.5.

In my project there is a part which I need to convert dates in IST format to this format: "yyyy-MM-dd" .

For example:

wed 0ct 08 00:00:00 IST 2014

TO

2014-10-08

I placed this code in a tMap component:

new SimpleDateFormat("yyyy-MM-dd")
 .parse(new SimpleDateFormat("yyyy-MM-dd")
 .format(new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(row4.Date)))

row4.date is the date that i want to convert and is equal to "wed 0ct 08 00:00:00 IST 2014" for example.

and I'm getting this exception:

java.lang.NullPointerException
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1380)
at java.text.DateFormat.parse(DateFormat.java:355)

What is the reason for the exception?


Solution

  •    if(null!=row4 && null!=row4.Date)//use null check    
         new SimpleDateFormat("yyyy-MM-dd")
         .parse(new SimpleDateFormat("yyyy-MM-dd")
         .format(new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(row4.Date)))
    

    One way to handle Null pointer is to do null checks and another and better way is to use try catch blocks so that it does not interrupt your code execution.