JAVA时间戳

JAVA时间戳,第1张

JAVA时间戳
  • 1.毫秒-时间戳
  • 2.秒-时间戳
  • 3.分钟-时间戳
  • 4.小时-时间戳
  • 5.天-时间戳
  • 时间戳和时间之间转换

1.毫秒-时间戳
System.currentTimeMillis();
2.秒-时间戳
System.currentTimeMillis() / 1000;
3.分钟-时间戳
System.currentTimeMillis() / 1000 / 60;
4.小时-时间戳
System.currentTimeMillis() / 1000 / (60 * 60);
5.天-时间戳
System.currentTimeMillis() / 1000 / (60 * 60 * 24);
时间戳和时间之间转换
/*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/919755.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存