/**
 * 时间变为时间戳
 * @param s
 * @return
 * @throws ParseException
*/
public static String dateToStamp(String s) throws ParseException{
    String res;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = null;
	try {
	    date = simpleDateFormat.parse(s);
	} catch (java.text.ParseException e) {
		e.printStackTrace();
	}
    long ts = date.getTime();
    res = String.valueOf(ts);
    return res;
}

System.out.println(dateToStamp("2020-08-06 00:00:00"));
执行结果:1596643200000

注:日期后必须跟时分秒,不然会报错

java时间搓在线转换 java时间转换成时间戳_时间戳

原因:SimpleDateFormat只能格式化比自己精度长的时间,或者相同的时间精度,不能格式化比自己精度短的时间