• 时间戳与Date类型转换


    时间戳与Date类型的相互转

    public static void main(String[] args) {
    // 10位的秒级别的时间戳
    long time1 = 1572509722L;
    // 13位的秒级别的时间戳
    double time2 = 1572509722000d;
    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  
    String result2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time2); 
    System.out.println("13位数的时间戳(毫秒)--->Date:" + result2);
     }

    Date转时间戳

    public static void main(String[] args) {
        //获取指定时间的时间戳,除以1000说明得到的是秒级别的时间戳(10位)
        long time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse("2019-09-30 24: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);
    }

    格式化Date

    public static void main(String[] args) {
        //使用common-lang包下面的DateFormatUtils类
      //DateFormatUtils是commons.lang3.time.DateFormatUtils下的,如果你的项目中没有,maven中引入下:
    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); }

    给日期加上指定时长(需求是给现在的时间加上12个小时)

    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);
    }

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

        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);
        }

    判断两个时间点是否为同一天、同一年(需求:给定两个日期,快速判断两者是否为同一天或者同一年)

    借助于commons-lang3这个jar中的DateUtils.isSameDay方法来实现
    public static boolean isSameDay(Date date1, Date date2) {
            if(date1 != null && date2 != null) {
                Calendar cal1 = Calendar.getInstance();
                cal1.setTime(date1);
                Calendar cal2 = Calendar.getInstance();
                cal2.setTime(date2);
                return isSameDay(cal1, cal2);
            } else {
                throw new IllegalArgumentException("The date must not be null");
            }
        }
    
        public static boolean isSameDay(Calendar cal1, Calendar cal2) {
            if(cal1 != null && cal2 != null) {
                return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
            } else {
                throw new IllegalArgumentException("The date must not be null");
            }
        }
    commons-lang包中的isSameDay方法的实现思想为:利用是否是同一ERA(翻译成:世纪)且同一年的第N天来判断的。

    如何得到当前时间的明天零点时间

     public static void main(String[] args) {
                Date currentEndDate = new Date();
                Calendar cal = Calendar.getInstance();
                cal.setTime(currentEndDate);
                cal.add(Calendar.DATE, 1);
                cal.set(Calendar.AM_PM, 0);
                cal.set(Calendar.HOUR, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                Date nextDate = cal.getTime();
                System.out.println(nextDate);
            }
  • 相关阅读:
    leetcode刷题37
    leetcode刷题36
    leetcode刷题38
    leetcode刷题35
    leetcode刷题34
    leetcode刷题33
    记一次Unity使用XNode插件时自动连线问题
    Unity中UGUI图片跟随文本自适应
    Unity中多个物体交换位置
    使用VSCode编译C
  • 原文地址:https://www.cnblogs.com/zyanrong/p/11772079.html
Copyright © 2020-2023  润新知