Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the following string to date or calendar object in Java?

Tags:

java

android

Have a string like 2011-03-09T03:02:10.823Z, how to convert it to Date or calendar object in Java?

like image 806
user496949 Avatar asked Mar 15 '11 02:03

user496949


1 Answers

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);
// ...
like image 161
BalusC Avatar answered Sep 17 '22 23:09

BalusC