ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

json字符换转对象,对象转字符串,返回指定日期是星期几,返回指定时间间隔内指定星期的日期列表

2022-02-07 09:34:19  阅读:105  来源: 互联网

标签:星期 endDate String beginDate 指定 日期 date Calendar public


package demo15;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author tuzuoquan
 * @date 2022/2/4 14:19
 */
public class Demo {

    public static void main(String[] args) throws ParseException {

//        List<DateRange> list = new ArrayList<>();
//        DateRange dateRange = new DateRange();
//        dateRange.setBeginDate("2021-06-09");
//        dateRange.setEndDate("2021-06-18");
//        list.add(dateRange);
//
//        DateRange dateRange2 = new DateRange();
//        dateRange2.setBeginDate("2021-07-09");
//        dateRange2.setEndDate("2021-07-18");
//        list.add(dateRange2);
//
//        String jsonObject = JSONObject.toJSONString(list);
//        System.out.println(jsonObject);
//
//        JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(dateRange);
//        System.out.println(jsonObject1.get("beginDate"));
//
//        List<DateRange> list1 = JSON.parseArray(jsonObject, DateRange.class);
//        System.out.println(list1.toString());
//        list1.stream().forEach(item -> {
//            System.out.println(item.toString());
//        });
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date startDate = sdf.parse("2022-01-06");
//        Calendar startCalendar = Calendar.getInstance();
//        startCalendar.setTime(startDate);
//
//        Date endDate = sdf.parse("2022-02-06");
//        Calendar endCalendar = Calendar.getInstance();
//        endCalendar.setTime(endDate);
//
//        String startWeek = "1,2";
//        Calendar calendar = getRealStartDay(startWeek, startCalendar, endCalendar);
//
//        String week = dateToWeek("2022-02-09");
//        System.out.println(week);

//        List<String> days = getDayOfWeekWithInDateInterval("2022-01-06", "2022-02-06", 1);
//        days.stream().forEach(day -> {
//            System.out.println(day);
//        });

        String beginDate = "2022-01-06";
        String endDate = "2022-02-06";
        String startWeek = "1,2";

        String[] weekDays1 = startWeek.split("[^\\d]+");
        List<String> weekDayList = new ArrayList<>();
        for(int i = 0 ; i < weekDays1.length; i++) {
            List<String> weekListTmp = getDayOfWeekWithInDateInterval(beginDate, endDate, Integer.parseInt(weekDays1[i]));
            if (CollectionUtils.isEmpty(weekListTmp)) {
                continue;
            }
            weekDayList.addAll(weekListTmp);
        }
        weekDayList.stream().forEach(week -> {
            System.out.println(week);
        });
    }
    
    /**
     * 返回beginDate,endDate之间指定周weekDays的日期字符串。其中weekDays的内容如下:
     * 
     * 周日 SUNDAY = 1
     * 周一 MONDAY = 2
     * 周二 TUESDAY = 3
     * 周三 WEDNESDAY = 4
     * 周四 THURSDAY = 5
     * 周五 FRIDAY = 6
     * 周六 SATURDAY = 7
     **/
    public static List<String> getDayOfWeekWithInDateInterval(String beginDate, String endDate, int weekDays) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateResult = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        String[] dateInterval = {beginDate, endDate};
        Date[] dates = new Date[dateInterval.length];
        for (int i = 0; i < dateInterval.length; i++) {
            String[] ymd = dateInterval[i].split("[^\\d]+");
            cal.set(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1, Integer.parseInt(ymd[2]));
            dates[i] = cal.getTime();
        }
        for (Date date = dates[0]; date.compareTo(dates[1]) <= 0; ) {
            cal.setTime(date);
            if (cal.get(Calendar.DAY_OF_WEEK) - 1 == weekDays) {
                String format = sdf.format(date);
                dateResult.add(format);

                //return dateResult;
            }
            cal.add(Calendar.DATE, 1);
            date = cal.getTime();
        }
        return dateResult;
    }

    /**
     * 获取指定日期是星期几
     **/
    public static String dateToWeek(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        Date date;
        try {
            date = sdf.parse(datetime);
            cal.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDays[w];
    }
}

DateRange的内容如下:

import java.io.Serializable;

/**
 * @author tuzuoquan
 * @date 2022/2/4 14:19
 */
public class DateRange implements Serializable {

    private String beginDate;
    private String endDate;

    public String getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(String beginDate) {
        this.beginDate = beginDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    @Override
    public String  toString() {
        return "DateRange{" +
                "beginDate='" + beginDate + '\'' +
                ", endDate='" + endDate + '\'' +
                '}';
    }
}

标签:星期,endDate,String,beginDate,指定,日期,date,Calendar,public
来源: https://blog.csdn.net/toto1297488504/article/details/122804202

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有