Have a string like 2011-03-09T03:02:10.823Z, how to convert it to Date or calendar object in Java?
You can use SimpleDateFormat#parse()
to convert a String
in a date format pattern to a Date
.
String string = "2011-03-09T03:02:10.823Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
Date date = new SimpleDateFormat(pattern).parse(string);
System.out.println(date); // Wed Mar 09 03:02:10 BOT 2011
For an overview of all pattern characters, read the introductory text of SimpleDateFormat
javadoc.
To convert it further to Calendar
, just use Calendar#setTime()
.
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// ...
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