The string I want to format looks like this: String datetime = "9/1/10 11:34:35 AM"
Following pattern for SimpleDateFormat works:
SimpleDateFormat sdf = SimpleDateFormat("M/d/yy h:mm:ss");
Date d = sdf.parse(datetime);
System.out.println(d);
Output> [Wed Sep 01 11:34:35 CEST 2010]
However I need to parse the AM/PM marker as well, and when I add that to the pattern I receive an exception.
Pattern that doesn't work:
SimpleDateFormat sdf = SimpleDateFormat("M/d/yy h:mm:ss a");
I have tried with this also with same exception:
SimpleDateFormat sdf = SimpleDateFormat("M/d/yy h:mm:ss aa");
Exception:
java.text.ParseException: Unparseable date: "9/1/10 11:34:35 AM"
I have looked through the API at http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#text but canät seem to find where I do wrong.
Any suggestions?
One possibility is that your default Locale
has different symbols for AM/PM. When constructing a date format you should always supply a Locale
unless you really want to use the system's default Locale
, e.g.:
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yy h:mm:ss a", Locale.US)
Modern answer:
String datetime = "9/1/10 11:34:35 AM";
LocalDateTime dt = LocalDateTime.parse(datetime,
DateTimeFormatter.ofPattern("M/d/yy h:mm:ss a", Locale.ENGLISH));
This produces a LocalDateTime
of 2010-09-01T11:34:35. Beware of two digit years, though; DateTimeFormatter
will assume 2000 through 2099. For my birthday this would have been incorrect.
We still need to provide the locale. Since AM/PM markers are hardly used in practice in other locales than English, I considered Locale.ENGLISH
a fairly safe bet. Please substitute your own.
The other answers were fine answers in 2010 and 2011. Already in 2014 the above was valid and I would have preferred it.
I am taking an example of date given below and print the formatted date into 24-hour format if suits your requirement.
String inputdate="9/1/10 11:34:35 AM";
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd/MM/yy hh:mm:ss aa",Locale.getDefault());
try {
System.out.println(""+new SimpleDateFormat("dd/MM/yy HH:mm:ss",Locale.getDefault()).format(simpleDateFormat.parse(inputdate)));
} catch (ParseException e) {
e.printStackTrace();
}
If you still have any query, Please respond. Thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With