Form System.currentTimeMillis(), in milliseconds In format "yyyy-MM-dd HH:mm:ss" - Android java.util

Android examples for java.util:Date Convert

Description

Form System.currentTimeMillis(), in milliseconds In format "yyyy-MM-dd HH:mm:ss"

Demo Code

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

public class Main {
  private static SimpleDateFormat sDateTimeFormat = null;

  private static SimpleDateFormat getDefaultFormatInstance() {
    if (sDateTimeFormat == null) {
      sDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    }//from   w  w  w  .j  a  va2s. c o  m
    return sDateTimeFormat;
  }

  /**
   * @param time
   *          returned by System.currentTimeMillis(), in milliseconds.
   * @return In format "yyyy-MM-dd HH:mm:ss"
   */
  public static String formatDateTime(long time) {
    return getDefaultFormatInstance().format(new Date(time));
  }

}

Related Tutorials