1

I have tried many different types of solutions using java.text.SimpleDateFormat but couldn't quite get it right.

The input string I receive is Tue Nov 5 00:00:00 UTC+0530 2013.
The format that I want is dd-MMM-yy.
Below is the code that I use:

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss zZ yyyy", Locale.ENGLISH);
date = formatter.parse(s);
System.out.println(date);

I receive an error: unreported exception ParseException; must be caught or declared to be thrown date = formatter.parse(s);

I tried a lot of change in my formats but still I receive this error. Can anyone please let me know the exact format of the string that I am passing?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Freakyuser
  • 2,774
  • 17
  • 45
  • 72
  • 1
    You're getting a *compile-time* error. That doesn't depend on the format you're using. It just means that your source code is invalid - `SimpleDateFormat.parse` can throw `ParseException`, and you're neither catching it nor declaring that it can be thrown. – Jon Skeet Nov 06 '14 at 13:49
  • 1
    Note that printing out the `Date` won't help much - you'll need to use a separate `SimpleDateFormat` with your "target" format. – Jon Skeet Nov 06 '14 at 13:49
  • My bad! I forgot to add the ` try catch `block. Thanks for spotting. – Freakyuser Nov 06 '14 at 14:02
  • @Freakyuser Please search StackOverflow before posting. Any of hundreds of Questions and Answers have example code that you could have studied to see the handling of exceptions during date-time string parsing. – Basil Bourque Nov 07 '14 at 06:43
  • possible duplicate of [ParseException Java](http://stackoverflow.com/questions/16116652/parseexception-java). And this one, [ParseException; must be caught (Try/Catch) (Java)](http://stackoverflow.com/q/16112676/642706). – Basil Bourque Nov 07 '14 at 06:45
  • @BasilBourque thank you. Before I posted question, I had handled exception, which is a basic thing. While posted, it was my mistake that I missed it. Actual thing in my mind was the mistake in the format I made. For that if you want to re-edit my question. – Freakyuser Nov 07 '14 at 07:16
  • 1
    @Freakyuser That format issue has also been handled in hundreds of Questions and Answers, such as [this one](http://stackoverflow.com/q/14366655/642706). Again, please search before posting. StackOverflow is getting polluted with needless duplicate questions. – Basil Bourque Nov 07 '14 at 07:27
  • @BasilBourque I have flagged my own question. Thank you once again for pointing out. Sorry for the inconvenience caused. – Freakyuser Nov 07 '14 at 07:37
  • @Freakyuser In a comment you said your real problem was the format, not the ParseException. Not so. I tried your code while adding a Try-Catch (see [my answer](http://stackoverflow.com/a/26796539/642706) for the code). Your format works, as-is, with your given input string. So indeed, your only problem is not handling the exception as explained in the [correct accepted answer](http://stackoverflow.com/a/26781113/642706). – Basil Bourque Nov 07 '14 at 08:03

2 Answers2

4

Handle Exceptions

You have not handled exceptions in your code. That is why the compiler gives errors. You need to handle the ParseException that may be thrown during parsing.

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss zZ yyyy", Locale.ENGLISH);
try{
   date = formatter.parse(s);
   System.out.println(date);
}catch(ParseException ex){
  //exception
  ex.printStackTrace();
}

Or you can add throws ParseException to your method .

According to your comment it seems to be you are trying to convert a date[String] to another format. If I am correct then the following example may help you.

String inputstring="Tue Nov 5 00:00:00 UTC+0530 2013";
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss zZ yyyy", Locale.ENGLISH);
DateFormat outformat = new SimpleDateFormat("dd-MMM-yy");
try {
    String result = outformat.format(formatter.parse(inputstring));
    System.out.println(result);
} catch (ParseException ex) {
    ex.printStackTrace();
}

Output:

04-Nov-13
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
0

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154