Format string in yyyy-MM-dd HH:mm:ss format to long - Android java.util

Android examples for java.util:Date Convert

Description

Format string in yyyy-MM-dd HH:mm:ss format to long

Demo Code

import java.text.SimpleDateFormat;

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

  public static boolean isNotBlank(String str) {
    return str != null && !str.trim().equals("");
  }/*  w ww . j  a v a  2s  .c  o  m*/

  public static long convert2long(String date) {
    try {
      if (isNotBlank(date)) {
        SimpleDateFormat sf = new SimpleDateFormat(TIME_FORMAT);
        return sf.parse(date).getTime();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return 0l;
  }

}

Related Tutorials