Java.text.ParseException: Unsurpassed date: "13:19 PM"

I'm just trying to make out simple time! Here is my code:

String s = "01:19 PM"; Date time = null; DateFormat parseFormat = new SimpleDateFormat("hh:mm aa"); try { time = parseFormat.parse(s); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

I get this exception:

 java.text.ParseException: Unparseable date: "01:19 PM" at java.text.DateFormat.parse(Unknown Source) 
+6
source share
6 answers

It works:

  public static void main(String[] args) throws Exception { String s = "01:19 PM"; Date time = null; DateFormat parseFormat = new SimpleDateFormat("hh:mm a", Locale.ENGLISH); System.out.println(time = parseFormat.parse(s)); } 

Outputs:

  Thu Jan 01 13:19:00 KST 1970 
+5
source

The letter a is an Am / pm marker, but local . Obviously, AM and PM valid in the English locale, but, for example, they are not valid in the Hungarian language.

You get a ParseException because you have a non-lingual locale set, and PM not valid in your locale.

 // This is OK, English locale, "PM" is valid in English Locale.setDefault(Locale.forLanguageTag("en")); new SimpleDateFormat("hh:mm aa").parse("01:19 PM"); // This will throw Exception, Hungarian locale, "PM" is invalid in Hungarian Locale.setDefault(Locale.forLanguageTag("hu")); new SimpleDateFormat("hh:mm aa").parse("01:19 PM"); 

To work around this problem, you can specify Locale in the constructor:

 // No matter what is the default locale, this will work: new SimpleDateFormat("hh:mm aa", Locale.US).parse("01:19 PM"); 
+5
source

LocalTime

Modern answer using LocalTime class.

 LocalTime time = null; DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH); try { time = LocalTime.parse(s, parseFormatter); } catch (DateTimeParseException dtpe) { System.out.println(dtpe.getMessage()); } 

This turns the line from question 01:19 PM into LocalTime equal to 13:19 .

We still need to provide the locale. Since the AM / PM markers are practically not used in practice in other locations than English, I considered Locale.ENGLISH fairly safe bet. Please replace yours.

When this question was asked in 2014, there was no modern replacement for the old Date and SimpleDateFormat classes, the modern Java date and time API. Today I believe that the old classes are long outdated and warmly recommend the use of modern ones. As a rule, they showed that they are more convenient in programming and convenient in work.

Just for one simple thing, if we cannot give a locale on a system with a default locale that does not recognize AM and PM, the modern formatter will give us an exception with the message Text '01:19 PM' could not be parsed at index 6 . Index 6, where he says PM , was already on our way. Yes, I know that there is a way to get the index from an exception thrown by an obsolete class, but most programmers never knew and therefore did not use it.

More importantly, the new API offers the LocalTime class, which gives us what we want and need here: just the time of day without a date. This allows us to more accurately model our data. There are a number of stack overflow issues caused by confusion, which in turn is caused by the fact that Date necessarily includes both a date and a time when sometimes you only want one or the other.

+2
source

I think that instead of "hh:mm aa" it should be "h:mm a"

+1
source

According to the official documentation, you should use this format string: "h:mm a"

But also your format string is correct, because I have no errors executing your code.

0
source

Try this format: "K:ma"

Also check the docs: SimpleDateFormat
Also, check your language, your problem seems to depend on the locale.

0
source

Source: https://habr.com/ru/post/974416/


All Articles