get String Date in yyyy-MM-dd HH:mm:ss format - Android java.util

Android examples for java.util:Date Time

Description

get String Date in yyyy-MM-dd HH:mm:ss format

Demo Code


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {

  public static String getStringDate() {
    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    String dateString = formatter.format(currentTime);
    return dateString;
  }//from   w  w w .j  a  va 2  s  . c om

  public static String getStringDate(String date) {

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    Date d = new Date();
    try {
      d = formatter.parse(date);
    } catch (Exception e) {
      e.printStackTrace();
    }
    String dateString = formatter.format(d);
    return dateString;

  }

}

Related Tutorials