How to convert the following string 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);
// ...

I want to show "2017-01-11" to "Jan 11" and this is my solution.

   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
   SimpleDateFormat df_output = new SimpleDateFormat("MMM DD");
   Calendar cal=Calendar.getInstance();

   Date date = null;
        try {
              date = df.parse(selectedDate);
              String outputDate = df.format(date);
              date = df_output.parse(outputDate);


              cal.setTime(date);

          } catch (ParseException e) {
            e.printStackTrace();
          }

Tags:

Java

Android