zoukankan      html  css  js  c++  java
  • java把时间戳转换成时间_(转)java时间与时间戳互转

    java中时间精确到毫秒级,所以需求时间需要 除以1000

    //将时间转换为时间戳

    public static String dateToStamp(String s) throws Exception {
    String res;

    //设置时间格式,将该时间格式的时间转换为时间戳

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Date date = simpleDateFormat.parse(s);

    long time = date.getTime();

    res = String.valueOf(time);

    return res;

    }

    //将时间戳转换为时间

    public static String stampToTime(String s) throws Exception{
    String res;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    long lt = new Long(s);

    //将时间戳转换为时间

    Date date = new Date(lt);

    //将时间调整为yyyy-MM-dd HH:mm:ss时间样式

    res = simpleDateFormat.format(date);

    return res;

    }

    后天调用代码为,通过除以1000获取到日期和时间的时间戳

    //getTime()方法是获取当前时间的时间戳,但是得到的时间不是当前时间

    Long s = new Date().getTime()/1000;

    String s1 = TimeFormatUtil.stampToTime(String.valueOf(s));

    需要转换,先转换为"yyyy-MM-dd HH:mm:ss"这个格式的时间,然后在将这个时间转换为时间戳

    //先将当前时间转换为习惯时间

    String date = TimeFormatUtil.timeToStamp(newDate());//将习惯时间转换为时间戳

    String time = TimeFormatUtil.dateToStamp(date);

  • 相关阅读:
    对之前IoT项目的完善
    利用 esp8266 搭建简单物联网项目
    IOT(esp8266)
    ---分割线---
    百度云下载工具--雷鸟下载
    Win10安装Ubuntu子系统
    安装Ubuntu虚拟机
    搭建微信公众号后台(二)
    手把手教你基于CentOS8搭建微信订阅号后台服务(一)
    如何在PHP5中通过PDO连接SQLite3数据库
  • 原文地址:https://www.cnblogs.com/lemperor/p/15393480.html
Copyright © 2011-2022 走看看