美文网首页
java已知时间的本周,本月,本年的起始时间

java已知时间的本周,本月,本年的起始时间

作者: 东方欲晓_莫道君行早 | 来源:发表于2020-09-24 12:28 被阅读0次

    String --->LocalDateTime /LocalDate --->String/Long
    有一些项目需要展示本日,本周,本月,本年的相关统计信息,这时就需要根据当前日期(或者所在系统的时间),来确定本周本年本月的起始时间。(java8 以上版本)
    代码如下:

    package com.jdwa.utill;
    
    import java.text.SimpleDateFormat;
    import java.time.DayOfWeek;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.ZoneOffset;
    import java.time.format.DateTimeFormatter;
    import java.time.temporal.TemporalAdjusters;
    
    public class Test1 {
        public static void main(String[] args) throws Exception{
            String dateTmStr = "2020-09-24 11:20:00";
            String dateStr = "2020-09-24";
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter fmtDay = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime dateTm = LocalDateTime.parse(dateTmStr, fmt);
            System.out.println("当前时间:"+fmt.format(dateTm));
            LocalDate date = LocalDate.parse(dateStr,fmtDay);
            System.out.println("当前日期:"+fmtDay.format(date));
    
            //获取本周起始日期
            System.out.println("本周(周一---->周日)=====================================================");
            LocalDateTime dateWeekFirst = date.with(DayOfWeek.MONDAY).atStartOfDay();
            LocalDateTime dateWeekLast = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY)).atStartOfDay().minusSeconds(1L);
            System.out.println("本周一:"+fmt.format(dateWeekFirst));
            System.out.println("本周日:"+fmt.format(dateWeekLast));
            System.out.println("====================将LocalDateTime转化为Long时间戳的方法==================================");
            Long startTm13 = dateWeekFirst.toInstant(ZoneOffset.of("+8")).toEpochMilli();
            Long endTm13 = dateWeekLast.toInstant(ZoneOffset.of("+8")).toEpochMilli();
            System.out.println(startTm13);
            System.out.println(endTm13);
            System.out.println("=========上面是13位,下面是10位===============");
            Long startTm10 = dateWeekFirst.toEpochSecond(ZoneOffset.of("+8"));
            Long endTm10 = dateWeekLast.toEpochSecond(ZoneOffset.of("+8"));
            System.out.println(startTm10);
            System.out.println(endTm10);
            System.out.println("====================将LocalDateTime转化为Long时间戳的方法==================================");
            //如果周日是第一天
            System.out.println("本周(周日---->周六)=====================================================");
            LocalDateTime dateSun = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)).atStartOfDay();
            LocalDateTime dateSat = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).atStartOfDay().minusSeconds(1L);
            System.out.println("本周日:"+fmt.format(dateSun));
            System.out.println("本周六:"+fmt.format(dateSat));
    
    
    
    
            //获取本月起始日期
            System.out.println("本月==============================================================");
            LocalDateTime dateMonthFirst = date.with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
            LocalDateTime datMonthLast = date.with(TemporalAdjusters.firstDayOfNextMonth()).atStartOfDay().minusSeconds(1L);
            System.out.println("本月的第一天:"+fmt.format(dateMonthFirst));
            System.out.println("本月最后一天:"+fmt.format(datMonthLast));
    
            //获取本年起始日期
            System.out.println("本年==============================================================");
            LocalDateTime dateYearFirst = date.with(TemporalAdjusters.firstDayOfYear()).atStartOfDay();
            LocalDateTime datYearLast = date.with(TemporalAdjusters.firstDayOfNextYear()).atStartOfDay().minusSeconds(1L);
            System.out.println("本年的第一天:"+fmt.format(dateYearFirst));
            System.out.println("本年最后一天:"+fmt.format(datYearLast));
    
            System.out.println("=============字符串转换为时间戳=========================");
            Long startTm = new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse("2020-09-26 23:23:23").getTime();
            System.out.println(startTm);
        }
    }
    
    

    运行结果如下:

    
    D:\java\jdk1.8.0_251\bin\java.exe "...com.jdwa.utill.Test1
    当前时间:2020-09-24 11:20:00
    当前日期:2020-09-24
    本周(周一---->周日)=====================================================
    本周一:2020-09-21 00:00:00
    本周日:2020-09-27 23:59:59
    ====================将LocalDateTime转化为Long时间戳的方法==================================
    1600617600000
    1601222399000
    =========上面是13位,下面是10位===============
    1600617600
    1601222399
    ====================将LocalDateTime转化为Long时间戳的方法==================================
    本周(周日---->周六)=====================================================
    本周日:2020-09-20 00:00:00
    本周六:2020-09-26 23:59:59
    本月==============================================================
    本月的第一天:2020-09-01 00:00:00
    本月最后一天:2020-09-30 23:59:59
    本年==============================================================
    本年的第一天:2020-01-01 00:00:00
    本年最后一天:2020-12-31 23:59:59
    =============字符串转换为时间戳=========================
    1601133803000
    
    Process finished with exit code 0
    
    
    

    公 众 号 已同步更新,欢迎查阅
    更有早行人

    相关文章

      网友评论

          本文标题:java已知时间的本周,本月,本年的起始时间

          本文链接:https://www.haomeiwen.com/subject/rjpzyktx.html