convert date string with format yyyy-MM-dd to millisecond - Java java.util

Java examples for java.util:Millisecond

Description

convert date string with format yyyy-MM-dd to millisecond

Demo Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {
    /**//ww  w  .j  a v  a 2  s .c  o  m
     * SimpleDateFormat with pattern yyyy-MM-dd
     */
    private static SimpleDateFormat formatYYYY_MM_DD;

    /**
     * convert date string with format yyyy-MM-dd to millisecond
     *
     * @param date
     * @return -1 if exception throw
     */
    public static long convertDateStr2Millis(String date) {
        if (date == null || date.trim().length() == 0)
            return -1;
        try {
            return formatYYYY_MM_DD.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }
}

Related Tutorials