时间字符串相互转化

字符串转时间 & 时间转字符串
//字符串转时间
public static Date parseDate(String str) {
    //eg: str = "2020-09-23 17:08:555"
    Date date = null;
    try {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        date = simpleDateFormat.parse(str);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}
public static String parseStr(Date date) {
    String str = null;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    str = simpleDateFormat.format(date);
    return str;
}
获取时间戳
public static long getTimestamp(Date date) {
    if (date == null) {
        return 0L;
    } else {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis() / 1000L;
    }
}
posted @ 2020-09-23 17:40  一白二白  阅读(620)  评论(0编辑  收藏  举报