• 【Java】时间戳与Date相互转换


    时间戳转Date

    public static void main(String[] args) {
        // 10位的秒级别的时间戳
        long time1 = 1527767665;
        String result1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time1 * 1000));
        System.out.println("10位数的时间戳(秒)--->Date:" + result1);
        Date date1 = new Date(time1*1000);   //对应的就是时间戳对应的Date
        // 13位的秒级别的时间戳
        double time2 = 1515730332000d;
        String result2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time2);
        System.out.println("13位数的时间戳(毫秒)--->Date:" + result2);
    }

    10位数的时间戳(秒)--->Date:2018-05-31 19:54:25
    13位数的时间戳(毫秒)--->Date:2018-01-12 12:12:12

    尤其要注意上面10位的秒级别的时间戳时,不能用int来定义time1变量,否则会得到错误的结果:

    public static void main(String[] args) {
        // 10位的秒级别的时间戳   
        int time1 = 1527767665;   //错误做法
        String result1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time1 * 1000));
        System.out.println("10位数的时间戳(秒)--->Date:" + result1);
    }
    
    10位数的时间戳(秒)--->Date:1969-12-17 23:21:47

    Date转时间戳

    public static void main(String[] args) {
        //获取指定时间的时间戳,除以1000说明得到的是秒级别的时间戳(10位)
        long time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse("2018-06-30 20:00:00", new ParsePosition(0)).getTime() / 1000;
     
        //获取时间戳
        long now1 = System.currentTimeMillis();
        long now2 = new Date().getTime();
     
        System.out.println("获取指定时间的时间戳:" + time);
        System.out.println("当前时间戳:" +now1);
        System.out.println("当前时间戳:" +now2);
    }
    
    获取指定时间的时间戳:1530360000
    当前时间戳:1527769494340
    当前时间戳:1527769494340

    格式化Date

    public static void main(String[] args) {
        //使用common-lang包下面的DateFormatUtils类
        String format1 = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        //使用最原始的SimpleDateFormat类
        String format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
     
        System.out.println("格式化时间1:" + format1);
        System.out.println("格式化时间2:" + format2);
    }
    
    格式化时间1:2018-05-31 20:26:49
    格式化时间2:2018-05-31 20:26:49

    DateFormatUtils是commons.lang3.time.DateFormatUtils下的,如果你的项目中没有,maven中引入下:

    <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
       <version>3.6</version>
    </dependency>

    给日期加上指定时长

    比如,给现在的时间加上12个小时,这个需求也很常见,例如我做过的某个秒杀接口要返回剩余秒杀时间给前端,那么我就直接计算(比如秒杀持续时间为12小时):秒杀(倒计时)截止时间=(当前时间+12H)即可。

    public static void main(String[] args) {
        //将指定日期加上固定时间,DateUtils还有其它添加分钟、小时、月份之类的方法api
        //使用到的是commons-lang包下面的DateUitls类
        Date date = DateUtils.addDays(new Date(), 10);   //
        System.out.println("当前时间为:"+DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        String format = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
        System.out.println("当前时间加上10天后:" + format);
    }
    
    当前时间为:2018-05-31 20:31:53
    当前时间加上10天后:2018-06-10 20:31:53

    得到指定时间节点的日期时间

    方式一

    public static void main(String[] args) throws ParseException {
            //得到指定日期
            String date = "2018-03-03 15:20:12";
            Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
            System.out.println(parse);
        }

    方式二

       /**
         * 获取当天的YYYY-MM-dd 00:00:01 时间点的毫秒级时间戳
         *
         * @return
         */
        public static Long getCurrentBeginDate() {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0);//控制时
            cal.set(Calendar.MINUTE, 0);//控制分
            cal.set(Calendar.SECOND, 1);//控制秒
            return cal.getTimeInMillis();
        }

    判断两个时间点是否为同一天、同一年

    给定两个日期,快速判断两者是否为同一天或者同一年,比如我做过的一个接口需求是:每人每天有一次抽奖机会,那么当用户当天第二次发送请求时候,我就得判断查询参与记录,并且判断最新一次参与时间和当天是否为同一天。

    /**
         * 判断是否为同一天:使用commons-lang包下的DateUtils类
         *
         * @param day1
         * @param day2
         * @return
         */
        public boolean isSameDay(Date day1, Date day2) {
            return DateUtils.isSameDay(day1, day2);
        }
     
        /**
         * 判断是否为同一天:使用joda依赖包里的时间类,效率从一定程度上优于DateUtils.isSameDay()方法
         *
         * @param date1
         * @param date2
         * @return
         */
        public static boolean isSameDay1(Date date1,Date date2){
            if(date1==null || date2==null){
                throw new IllegalArgumentException("date must be not null");
            }
            LocalDate localDate1 = new LocalDate(new DateTime(date1.getTime()));
            LocalDate localDate2 = new LocalDate(new DateTime(date2.getTime()));
            return localDate1.equals(localDate2);
        }
  • 相关阅读:
    swap文件查看
    内核调试神器SystemTap 转摘
    RPM软件包管理的查询功能 转
    systemtap跟踪C
    mysql 阿里内核人员
    systemtap 调试postgrel
    数据库大会
    solaris 软件包地址
    java实现大文件上传方案
    java实现大文件上传技术
  • 原文地址:https://www.cnblogs.com/jxd283465/p/11763128.html
Copyright © 2020-2023  润新知