美文网首页
java获取月份日期列表

java获取月份日期列表

作者: 飞不起的小鸟 | 来源:发表于2021-05-21 15:51 被阅读0次

    效果图


    image.png
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    public class text {
    
        public static void main(String[] args) {
    
            System.out.println(monthDays("2021-05"));
    
        }
    
        private static List<DateInfo> monthDays(String date){
            SimpleDateFormat yymm = new SimpleDateFormat("yyyy-MM");
            SimpleDateFormat yymmdd = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            List<DateInfo> dateInfoList = new ArrayList<>();
            try {
                //年月转成Date 例:2021-05 ,目的获取本月的第一次
                Date dt =yymm.parse(date);
                String ym = date;
                do{
                    //当天日期
                    date = yymmdd.format(dt);
                    if (!date.startsWith(ym)){ //如果不是本月 退出
                        //判断本月1号是否星期天,如果不是星期天,取上个星期天的天数
                        if (!dateInfoList.isEmpty()){
                            DateInfo oneDate = dateInfoList.get(0);
                            if (oneDate.getWeek() != 0){
                                calendar.setTime(yymmdd.parse(oneDate.getDate()));
                                for (int i = oneDate.getWeek() - 1; i >= 0; i--) {
                                    calendar.add(calendar.DATE,-1);
                                    dt = calendar.getTime();
                                    date = yymmdd.format(dt);
                                    DateInfo dateInfo = new DateInfo();
                                    dateInfo.setDate(date);
                                    dateInfo.setWeek(calendar.get(Calendar.DAY_OF_WEEK)-1);
                                    dateInfo.setDay(calendar.get(Calendar.DATE));
                                    dateInfoList.add(0,dateInfo);
                                }
                            }
                        }
                        return dateInfoList;
                    }
                    calendar.setTime(dt);
    
                    //获取星期 和 日
                    DateInfo dateInfo = new DateInfo();
                    dateInfo.setDate(date);
                    dateInfo.setWeek(calendar.get(Calendar.DAY_OF_WEEK)-1);
                    dateInfo.setDay(calendar.get(Calendar.DATE));
                    dateInfoList.add(dateInfo);
    
                    //设置明天日期
                    calendar.add(calendar.DATE,1);
                    dt = calendar.getTime();
                } while (true);
    
    
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    
    }
    
    class DateInfo{
        private int week;
        private int day;
        private String date;
    
        public int getWeek() {
            return week;
        }
    
        public void setWeek(int week) {
            this.week = week;
        }
    
        public int getDay() {
            return day;
        }
    
        public void setDay(int day) {
            this.day = day;
        }
    
        public String getDate() {
            return date;
        }
    
        public void setDate(String date) {
            this.date = date;
        }
    
        @Override
        public String toString() {
            return "\nDateInfo{" +
                    "week=" + week +
                    ", day=" + day +
                    ", date='" + date + '\'' +
                    '}';
        }
    }
    

    打印数据

    [
    DateInfo{week=0, day=25, date='2021-04-25'}, 
    DateInfo{week=1, day=26, date='2021-04-26'}, 
    DateInfo{week=2, day=27, date='2021-04-27'}, 
    DateInfo{week=3, day=28, date='2021-04-28'}, 
    DateInfo{week=4, day=29, date='2021-04-29'}, 
    DateInfo{week=5, day=30, date='2021-04-30'}, 
    DateInfo{week=6, day=1, date='2021-05-01'}, 
    DateInfo{week=0, day=2, date='2021-05-02'}, 
    DateInfo{week=1, day=3, date='2021-05-03'}, 
    DateInfo{week=2, day=4, date='2021-05-04'}, 
    DateInfo{week=3, day=5, date='2021-05-05'}, 
    DateInfo{week=4, day=6, date='2021-05-06'}, 
    DateInfo{week=5, day=7, date='2021-05-07'}, 
    DateInfo{week=6, day=8, date='2021-05-08'}, 
    DateInfo{week=0, day=9, date='2021-05-09'}, 
    DateInfo{week=1, day=10, date='2021-05-10'}, 
    DateInfo{week=2, day=11, date='2021-05-11'}, 
    DateInfo{week=3, day=12, date='2021-05-12'}, 
    DateInfo{week=4, day=13, date='2021-05-13'}, 
    DateInfo{week=5, day=14, date='2021-05-14'}, 
    DateInfo{week=6, day=15, date='2021-05-15'}, 
    DateInfo{week=0, day=16, date='2021-05-16'}, 
    DateInfo{week=1, day=17, date='2021-05-17'}, 
    DateInfo{week=2, day=18, date='2021-05-18'}, 
    DateInfo{week=3, day=19, date='2021-05-19'}, 
    DateInfo{week=4, day=20, date='2021-05-20'}, 
    DateInfo{week=5, day=21, date='2021-05-21'}, 
    DateInfo{week=6, day=22, date='2021-05-22'}, 
    DateInfo{week=0, day=23, date='2021-05-23'}, 
    DateInfo{week=1, day=24, date='2021-05-24'}, 
    DateInfo{week=2, day=25, date='2021-05-25'}, 
    DateInfo{week=3, day=26, date='2021-05-26'}, 
    DateInfo{week=4, day=27, date='2021-05-27'}, 
    DateInfo{week=5, day=28, date='2021-05-28'}, 
    DateInfo{week=6, day=29, date='2021-05-29'}, 
    DateInfo{week=0, day=30, date='2021-05-30'}, 
    DateInfo{week=1, day=31, date='2021-05-31'}]
    

    相关文章

      网友评论

          本文标题:java获取月份日期列表

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