Parsing Date and time from "yyyy-MM-dd hh:mm:ss.SSS" -------> "hh:mm aa" in android
Solution 1
Try this
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat output = new SimpleDateFormat("hh:mm aa");
Date d = null;
Date d1 = null;
try
{
d = input.parse("2018-02-05 17:08:52.503");
d1 = input.parse("2018-02-05 17:06:55.372");
} catch (ParseException e) {
e.printStackTrace();
}
String formatted = output.format(d);
String formatted1 = output.format(d1);
Log.i("DATE", "" + formatted);
Log.i("DATE1", "" + formatted1);
OUTPUT
I/DATE: 05:08 PM
I/DATE1: 05:06 PM
EDIT
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat output = new SimpleDateFormat("hh:mm aa");
Date d = null;
Date d1 = null;
try
{
d = input.parse("2018-02-05 17:00:52.503");
d1 = input.parse("2018-02-05 17:00:55.372");
} catch (ParseException e) {
e.printStackTrace();
}
String formatted = output.format(d);
String formatted1 = output.format(d1);
Log.i("DATE", "" + formatted);
Log.i("DATE1", "" + formatted1);
OUTPUT
I/DATE: 05:00 PM
I/DATE1: 05:00 PM
Solution 2
SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Solution 3
Your question has been answered already. I just wanted to demonstrate that java.time
, the modern Java date and time API, is doing a somewhat better effort to be helpful with the very common incorrect case of format pattern letters for parsing. Let’s try to use your format pattern string with the modern DateTimeFormatter
:
DateTimeFormatter readFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss.sss");
This line throws java.lang.IllegalArgumentException: Too many pattern letters: s
. So it doesn’t accept three lowercase s
? It’s not telling us the case is incorrect, after all it cannot read our mind; but it does point to where one of the bugs is. Let’s correct:
DateTimeFormatter readFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss.SSS");
String from = "2018-02-05 17:00:52.503";
LocalDateTime dateTime = LocalDateTime.parse(from, readFormatter);
This time we get a couple of lines further down, then hit a java.time.format.DateTimeParseException: Text '2018-02-05 17:00:52.503' could not be parsed: Invalid value for ClockHourOfAmPm (valid values 1 - 12): 17
. Clock hour of AM/PM? We didn’t intend that, again it is very precise about where the bug is. Correct this one too:
DateTimeFormatter readFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
This time the string gets parsed into 2018-02-05T17:00:52.503
as desired.
On newer Android you can use java.time
directly. For older Android add ThreeTenABP to your project and make sure you import the date and time classes from org.threeten.bp
with subpackages.
Links
-
Oracle tutorial: Date Time explaining how to use
java.time
. -
Java Specification Request (JSR) 310, where
java.time
was first described. -
ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Solution 4
Use this readFormat
SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Related videos on Youtube
Nj_96
Updated on June 04, 2022Comments
-
Nj_96 almost 2 years
Need to convert the time to a specified format but getting a wrong result.Please help
DateFormat readFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss.sss"); DateFormat writeFormat = new SimpleDateFormat( "hh:mm aa"); //Expecting like 05:00 String from = "2018-02-05 17:00:52.503"; String t0 = "2018-02-05 17:00:55.372"; Date date = null; //IST Date date2=null; try { date = readFormat.parse(from); date2 =readFormat.parse(to); } catch (ParseException e) { e.printStackTrace(); } if (date != null) { from = writeFormat.format(date); //Expecting a result of 05.00 pm to = writeFormat.format(date2); //Expecting a result of 05:00 pm }
List item## Need out put like this ##
/*But getting an output as:
from= "05:08 pm" instead of 05:00 pm to = "05:06 pm" instead of 05:00 pm
*/
-
rupinderjeet about 6 yearstry
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
-
Ole V.V. about 6 yearsJust read your own title. :-) You need to watch the case of those format pattern letters closely. It’s so easy to use lowercase where uppercase should have been used or vice versa. You need uppercase
HH
and uppercaseSSS
. -
Ole V.V. about 6 yearsAs an aside consider throwing away the long outmoded and notoriously troublesome
SimpleDateFormat
and friends, and adding ThreeTenABP to your Android project in order to usejava.time
, the modern Java date and time API. It is so much nicer to work with.
-
Ratilal Chopda about 6 yearsyou can parse your date 2018-02-05 17:00:52.503 instead of my date 2018-02-05 17:08:52.503
-
Ratilal Chopda about 6 years@NithinN.j please check my Edit answer.
-
Ratilal Chopda about 6 years@Nithin N.j Thank you, Happy to help you
-
Nj_96 about 6 yearsIt works.I just declared DateFormat readFormat , DateFormat writeFormat instead of SimpleDateFormat readFormat and SimpleDateFormat writeFormat . Thanks for the help bro...
-
Ole V.V. about 6 yearsThis is an improvement over the format pattern string in the question, but still will not parse
2018-02-07 12:42:43.296
correctly. Also code-only answers are seldom helpful, better to explain how you are solving the problem, for us all to learn.