Parse string To Calendar in yyyy-MM-dd HH:mm:ss format - Android java.util

Android examples for java.util:Date Parse

Description

Parse string To Calendar in yyyy-MM-dd HH:mm:ss format

Demo Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
  public final static String SQL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

  public static Calendar stringToCalendar(String date) {
    Date myNewDate = null;/*w  ww.j  a v  a 2  s.  c  o  m*/
    Calendar calendar = Calendar.getInstance();

    if (date == null || date.length() == 0)
      return null;

    SimpleDateFormat dateFormat = new SimpleDateFormat(SQL_DATE_FORMAT);
    try {
      myNewDate = dateFormat.parse(date);
      calendar.setTime(myNewDate);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return calendar;
  }

}

Related Tutorials