获取当天日期范围性能比较

获取当天日期范围

  • 原生。
    static Long ONE_DAY = 24*60*60*1000L;
    static long ONE_DAY_END = 24*60*60*1000L - 1000;
    static int  OFFSET_LEN = TimeZone.getDefault().getRawOffset();
    public static void main(String[] args) {
        long startXw = System.currentTimeMillis();
        long time = System.currentTimeMillis();
        int offsetLen = TimeZone.getDefault().getRawOffset();
        Long currentDayZeroMillisecond = time - time%ONE_DAY - OFFSET_LEN;
        long currentDayEndMillisecond = currentDayZeroMillisecond + ONE_DAY_END;
        Date date1_ = new Date(currentDayZeroMillisecond);
        Date date2_ = new Date(currentDayEndMillisecond);
        System.out.println("简单加减实现: "+ (System.currentTimeMillis() - startXw) + " ms");
        System.out.println(date1_);
        System.out.println(date2_);

        long start = System.currentTimeMillis();
        LocalDateTime ldt1 = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime ldt2 = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        ZoneOffset zoneOffset = OffsetDateTime.now().getOffset();
        Date date1 = Date.from(ldt1.toInstant(zoneOffset));
        Date date2 = Date.from(ldt2.toInstant(zoneOffset));
        System.out.println("LocalDateTIme APi实现:" + (System.currentTimeMillis() - start) + " ms");
        System.out.println(date1);
        System.out.println(date2);

    }
  • 测试结果
Connected to the target VM, address: '127.0.0.1:63478', transport: 'socket'
 cost: 1 ms
Tue Mar 30 00:00:00 CST 2021
Tue Mar 30 23:59:59 CST 2021
LocalDateTIme APi实现: 79 ms
Tue Mar 30 00:00:00 CST 2021
Tue Mar 30 23:59:59 CST 2021
Disconnected from the target VM, address: '127.0.0.1:63478', transport: 'socket'

你可能感兴趣的:(获取当天日期范围性能比较)