Android, How can I Convert String to Date? Android, How can I Convert String to Date? android android

Android, How can I Convert String to Date?


From String to Date

String dtStart = "2010-10-15T09:27:37Z";  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  try {      Date date = format.parse(dtStart);      System.out.println(date);  } catch (ParseException e) {    e.printStackTrace();  }

From Date to String

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  try {      Date date = new Date();      String dateTime = dateFormat.format(date);    System.out.println("Current Date Time : " + dateTime); } catch (ParseException e) {    e.printStackTrace();  }


SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");Date d = dateFormat.parse(datestring)


using SimpleDateFormat or DateFormat class through

for e.g.

try{SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // here set the pattern as you date in string was containing like date/month/yearDate d = sdf.parse("20/12/2011");}catch(ParseException ex){    // handle parsing exception if date string was different from the pattern applying into the SimpleDateFormat contructor}