Reputation: 849
SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
edto.setDate(formatter.parse(startHrs.getText() + ":" + startMin.getText());
Hello, I'm trying to format my time into something like 12:08 PM
to add into sqlite as type DATETIME
. In my DTO class the corresponding time field is of Date
type.
I am getting error for the above that such string is unparseable. Why is this.
Please note startHrs
and startMin
are two JTextFields Components taking in the value of Hours (12) and Minutes (08) respectively.
How can I successfully parse the date?
Errors:
java.text.ParseException: Unparseable date: "12:12"
at java.text.DateFormat.parse(DateFormat.java:337)
Upvotes: 2
Views: 815
Reputation: 7016
SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
edto.setDate(formatter.parse(startHrs.getText() + ":" + startMin.getText());
In above code formatter string is hour:minute:am or pm but i can see as input you are only providing hour and minute which results in unparsable date. Either correct your parser format string or provide input in hour:minute:AM or hour:minute:PM format.
Upvotes: 0
Reputation: 7413
You need to add AM or PM
edto.setDate(formatter.parse(startHrs + ":" + startMin + " AM"));
It works for me
Upvotes: 2
Reputation: 41220
your dateformat
is h:mm a
which does not match with 12:12
. It will match with 12:12 PM
.
Try -
edto.setDate(formatter.parse(startHrs.getText() + ":" + startMin.getText()+" PM");
I would suggest you to use HH
instead of h
which represent Hour in day (0-23) there you don't need to predict Am/pm marker
.
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
edto.setDate(formatter.parse(startHrs.getText() + ":" + startMin.getText());
Upvotes: 1
Reputation: 2436
You need to use the 'a' to set up PM / AM - http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Try parsing:
12:12 AM
Upvotes: 0