No Problem
Your code works* if you catch the exception as directed in the correct answer.
String input = "Tue Nov 5 00:00:00 UTC+0530 2013";
java.text.SimpleDateFormat sdformatter = new java.text.SimpleDateFormat( "EEE MMM d HH:mm:ss zZ yyyy" , Locale.ENGLISH );
java.util.Date date = null;
try {
date = sdformatter.parse( input );
} catch ( ParseException ex ) {
System.out.println( "ERROR: " + ex ); // … handle exception
}
System.out.println( "date: " + date + " (adjusted to Kolkata time via Joda-Time: " + new DateTime( date , DateTimeZone.forID( "Asia/Kolkata" ) ) );
When run.
date: Mon Nov 04 10:30:00 PST 2013 (adjusted to Kolkata time via Joda-Time: 2013-11-05T00:00:00.000+05:30
Joda-Time
That same format works in Joda-Time 2.5.
The java.util.Date/.Calendar/java.text.SimpleDateFormat classes bundled with Java are notoriously troublesome, confusing, and flawed. Avoid them. Use either Joda-Time or the new java.time package built into Java 8 (inspired by Joda-Time).
String input = "Tue Nov 5 00:00:00 UTC+0530 2013";
DateTimeFormatter formatter = DateTimeFormat.forPattern( "EEE MMM d HH:mm:ss zZ yyyy" );
DateTime dateTime = formatter.parseDateTime( input ).withZone( DateTimeZone.forID( "Asia/Kolkata" ) );
System.out.println( "dateTime: " + dateTime );
When run.
dateTime: 2013-11-05T00:00:00.000+05:30
Alternate Format
In your case, you could ignore the UTC
as it is redundant with the offset ( +0530
). An offset is assumed to be from UTC. You can ignore characters by using quote marks.
DateTimeFormatter formatter = DateTimeFormat.forPattern( "EEE MMM d HH:mm:ss 'UTC'Z yyyy" );
*Your code works for me using Java 8 Update 25. Earlier versions of java.text.SimpleDateFormat had varying behaviors with the Z-letter and offsets. But, again, you should not even be using SimpleDateFormat.