1

I wanted to get a java.util.Date Object by parsing a String date : "2017-06-28 08:30 AM". This parsing is used in my Android application. This is working without any parsing exception in many Android versions. Like 7.0, 4.4.2 and 5.1. But this is not working in Android 6.0.

This is my error log and SimpleDateFormat,

W/System.err: java.text.ParseException: Unparseable date: "2017-06-28 08:30 AM" (at offset 17)
at java.text.DateFormat.parse(DateFormat.java:579)
at biz.spsolutions.peopleedge.RosterClockInActivity.setData(RosterClockInActivity.java:531)
at biz.spsolutions.peopleedge.RosterClockInActivity.onCreate(RosterClockInActivity.java:391)
at android.app.Activity.performCreate(Activity.java:6877)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3208)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3351)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

And This is my SimpleDateFormat,

SimpleDateFormat currentFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a")

Also I have tried in this format too,

SimpleDateFormat currentFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa")

How can I handle this exception ? Have any ideas ?

Terance Wijesuriya
  • 1,928
  • 9
  • 31
  • 61
  • 1
    try this `2017-06-28 08:30 am` – M D Jun 28 '17 at 04:15
  • 2
    @Barrier check http://www.brightec.co.uk/ideas/android-marshmallow-and-simpledateformat if it helps. – gvmani Jun 28 '17 at 04:32
  • @M D : OK , I will try and inform you – Terance Wijesuriya Jun 28 '17 at 04:42
  • @gvmani : It is not working. I tried it. – Terance Wijesuriya Jun 28 '17 at 04:43
  • 3
    what is your locale? (if it's not english, AM/PM don't mean anything) – njzk2 Jun 28 '17 at 04:54
  • 1
    Both of your formats work on my computer (except you are missing a semicolon). I should prefer some changes, though: (1) Give explicit locale as in `new SimpleDateFormat("yyyy-MM-dd hh:mm a", Locale.ENGLISH)`; this will likely solve your problem. (2) Give explicit time zone: `currentFormat.setTimeZone(TimeZone.getTimeZone("Asia/Harbin"));`. (3) Best, get [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) and use [Basil Bourque’s answer](https://stackoverflow.com/a/44793901/5772882). – Ole V.V. Jun 28 '17 at 05:42

1 Answers1

6

tl;dr

No problem when using the modern java.time classes.

LocalDateTime.parse( 
    "2017-06-28 08:30 AM" , 
    DateTimeFormatter.ofPattern( "uuuu-MM-dd hh:mm a" , Locale.US ) 
)

2017-06-28T08:30

Using java.time

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes. For Android, see last bullet below.

Note that I specify Locale.US as part of my formatter. The locale determines the cultural norms used in determining issues such as capitalization. I suspect your JVM’s current default may set to a locale expecting "AM/PM" to be in lowercase. Always specify the expected/desired locale rather than rely implicitly on the current default locale which can changed at any time by any code in app of your JVM. For more info, see other Question.

String input = "2017-06-28 08:30 AM" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd hh:mm a" , Locale.US ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

See this code run live at IdeOne.com.

ldt.toString(): 2017-06-28T08:30


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    @DawoodibnKareem (a) Notice that I did show the use of `Locale` in my Answer. (b) There is simply no reason to using the bloody awful mess that is the old `Date`/`Calendar`/`SimpleDateFormat` classes. I claim that avoiding those classes is the correct solution to every such Question. (c) I will add a specific mention about locale being a possible cause of the problem. – Basil Bourque Jun 28 '17 at 05:54
  • 1
    @DawoodibnKareem, I see your point. I also agree with you that the OP’s problem is very likely related to locale. The answer *does* give a correct locale for the input string and hence solves the problem. Solving a problem in another way than the question anticipated is welcome on Stack Overflow and has helped thousands of users, often better than they had expected. While this may not be the answer that was asked for, it could still well be the answer that the OP wants. – Ole V.V. Jun 28 '17 at 05:54
  • 2
    @OleV.V. You're right. I hereby retract my objection and apply a well-deserved upvote. – Dawood ibn Kareem Jun 28 '17 at 05:56