zoukankan      html  css  js  c++  java
  • java求已知时间的本周,本月,本年的起始时间

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

    package com.jdwa.utill;
    
    import java.time.DayOfWeek;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    import java.time.temporal.TemporalAdjusters;
    
    public class Test1 {
        public static void main(String[] args) {
            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 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));
    
    
        }
    }
    
    

    为了方便手机观看,调整一下

    
    package com.jdwa.utill;
    
    import java.time.DayOfWeek;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    import java.time.temporal.TemporalAdjusters;
    
    public class Test1 {
        public static void main(String[] args) {
            String dateTmStr =
                    "2020-09-24 12: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 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));
    
    
        }
    }
    
    

    运行结果如下:

    
    D:javajdk1.8.0_251injava.exe "...com.jdwa.utill.Test1
    当前时间:2020-09-24 12:20:00
    当前日期:2020-09-24
    本周(周一-->周日)==========
    本周一:2020-09-21 00:00:00
    本周日:2020-09-27 23:59:59
    本周(周日---->周六)====
    本周日: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
    
    Process finished with exit code 0
    
    
    欢迎大家留言,以便于后面的人更快解决问题!另外亦欢迎大家可以关注我的微信公众号,方便利用零碎时间互相交流。共勉!

    路漫漫其修远兮,吾将上下而求索。。。

  • 相关阅读:
    RPC的入门
    Https的实现原理
    Celery
    Flask信号
    Redis安装
    python之递归
    python之三元表达式和生成式
    python第十八天作业
    python之生成器
    python之迭代器
  • 原文地址:https://www.cnblogs.com/caozz/p/java_date.html
Copyright © 2011-2022 走看看