java获取指定时间区间的所有日期,获取最近一周,最近一个月,最近一年的所有日期

1、补全给定起止时间区间内的所有日期

/**
 * 补全给定起止时间区间内的所有日期
 * @param startTime
 * @param endTime
 * @param isIncludeStartTime
 * @return
 */
public static List<String> getBetweenDates(String startTime, String endTime,boolean isIncludeStartTime){
    List<String> result = new ArrayList<>();
    try {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(startTime);//定义起始日期
        Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(endTime);//定义结束日期  可以去当前月也可以手动写日期。
        Calendar dd = Calendar.getInstance();//定义日期实例
        dd.setTime(d1);//设置日期起始时间
        if(isIncludeStartTime) {
            result.add(format.format(d1));
        }
        while (dd.getTime().before(d2)) {//判断是否到结束日期
            dd.add(Calendar.DATE, 1);//进行当前日期加1
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String str = sdf.format(dd.getTime());
            result.add(str);
            System.out.println(str);
        }
    }catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

2、 补全给定起止时间区间内的所有周(年份-周)

/**
 * 补全给定时间内的所有周,包含最开始的
 * @param startTime
 * @param endTime
 * @return
 */
public static ArrayList<String> getWeeksBetweenDates(String startTime,String endTime) {
    ArrayList<String> result = new ArrayList<>();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date start = null;
    Date end = null;
    try {
        start = format.parse(startTime);
        end = format.parse(endTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(start);
        result.add(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.WEEK_OF_YEAR));
        while(calendar.getTime().before(end)) {
            calendar.add(Calendar.WEEK_OF_YEAR, 1);
            String weekStr = calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.WEEK_OF_YEAR);
            result.add(weekStr);
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return result;
}

3、补全给定起止时间区间内的所有月份

/**
 * 补全给定时间区内的所有月份
 * @param startTime
 * @param endTime
 * @return
 */
public static List<String> getMonthsBetweenDates(String startTime,String endTime) {
    List<String> result = new ArrayList<>();
    try{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date d1 = new SimpleDateFormat("yyyy-MM").parse(startTime);//定义起始日期
        Date d2 = new SimpleDateFormat("yyyy-MM").parse(endTime);//定义结束日期  可以去当前月也可以手动写日期。
        Calendar dd = Calendar.getInstance();//定义日期实例
        dd.setTime(d1);//设置日期起始时间
        result.add(format.format(d1));
        while (dd.getTime().before(d2)) {//判断是否到结束日期
            dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String str = sdf.format(dd.getTime());
            result.add(str);
        }
    }catch (Exception e){
        e.printStackTrace();
    }

    return result;
}

4、最近一周的所有日期

/**
 * 最近一周的所有日期
 * @return
 */
public static List<String> getNearlyWeekDates() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    //过去七天
    c.setTime(new Date());
    String today = format.format(new Date());
    c.add(Calendar.DATE, - 7);
    Date d = c.getTime();
    String day = format.format(d);
    List<String> result = getBetweenDates(day,today,false);

    return result;
}

5、最近一个月的所有日期

/**
 * 最近一个月的所有日期
 * @return
 */
public static List<String> getNearlyMonthDates() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    //过去一月
    c.setTime(new Date());
    String today = format.format(new Date());
    c.add(Calendar.MONTH, -1);
    Date m = c.getTime();
    String mon = format.format(m);
    List<String> result = getBetweenDates(mon,today,false);

    return result;
}

6、最近一年的所有月份

/**
 * 最近一年的所有月份
 * @return
 */
public static List<String> getNearlyYearDates() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    //过去一年
    c.setTime(new Date());
    String today = format.format(new Date());
    c.add(Calendar.MONTH, -11);
    Date y = c.getTime();
    String year = format.format(y);
    //如果要获取近一年内的所有日期则,调用getBetweenDates()方法
    List<String> result = getMonthsBetweenDates(year,today);

    return result;
}

7、运行

 

public static void main(String[] args) {
    List<String> result1 = getBetweenDates("2019-11-01","2019-11-12",true);
    System.out.println("年-月-日:"+result1);
    List<String> result2 = getMonthsBetweenDates("2019-09-01","2019-11-12");
    System.out.println("年-月:"+result2);
    ArrayList<String> result = getWeeksBetweenDates("2019-1-1","2019-11-12");
    System.out.println("周:"+result.toString());
    List<String> result3 = getNearlyWeekDates();
    System.out.println("最近一周:"+result3.toString());
    List<String> result4 = getNearlyMonthDates();
    System.out.println("最近一月:"+result4.toString());
    List<String> result5 = getNearlyYearDates();
    System.out.println("最近一年:"+result5.toString());
}

运行结果:

年-月-日:
[2019-11-01, 2019-11-02, 2019-11-03, 2019-11-04, 2019-11-05, 2019-11-06, 
2019-11-07, 2019-11-08, 2019-11-09, 2019-11-10, 2019-11-11, 2019-11-12]

年-月:
[2019-09, 2019-10, 2019-11]

周:指的是一年内的第多少周
[2019-1, 2019-2, 2019-3, 2019-4, 2019-5, 2019-6, 2019-7, 2019-8, 2019-9, 2019-10, 
2019-11, 2019-12, 2019-13, 2019-14, 2019-15, 2019-16, 2019-17, 2019-18, 2019-19, 2019-20,
2019-21, 2019-22, 2019-23, 2019-24, 2019-25, 2019-26, 2019-27, 2019-28, 2019-29, 2019-30,
2019-31, 2019-32, 2019-33, 2019-34, 2019-35, 2019-36, 2019-37, 2019-38, 2019-39, 2019-40,
2019-41, 2019-42, 2019-43, 2019-44, 2019-45, 2019-46]

最近一周:
[2019-11-14, 2019-11-15, 2019-11-16, 2019-11-17, 2019-11-18, 2019-11-19, 2019-11-20]

最近一月:
[2019-10-21, 2019-10-22, 2019-10-23, 2019-10-24, 2019-10-25, 2019-10-26, 2019-10-27, 
2019-10-28, 2019-10-29, 2019-10-30, 2019-10-31, 2019-11-01, 2019-11-02, 2019-11-03, 
2019-11-04, 2019-11-05, 2019-11-06, 2019-11-07, 2019-11-08, 2019-11-09, 2019-11-10, 
2019-11-11, 2019-11-12, 2019-11-13, 2019-11-14, 2019-11-15, 2019-11-16, 2019-11-17, 
2019-11-18, 2019-11-19, 2019-11-20]

最近一年:
[2018-12, 2019-01, 2019-02, 2019-03, 2019-04, 2019-05, 2019-06, 2019-07, 2019-08, 
2019-09, 2019-10, 2019-11]

 

  • 10
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Java 中的 Calendar 类来获取两个时间点之间的所有日期,具体步骤如下: 1. 首先,需要将两个时间点转换为 Calendar 对象,并获取它们的年、月、日等信息。 2. 然后,通过循环遍历两个时间点之间的所有日期,判断每个日期是否为一,如果是一则输出该日期的年、月、日信息。 下面是一个示例代码: ```java import java.util.Calendar; public class Main { public static void main(String[] args) { Calendar start = Calendar.getInstance(); start.set(2022, Calendar.JANUARY, 1); // 起始日期 Calendar end = Calendar.getInstance(); end.set(2022, Calendar.DECEMBER, 31); // 结束日期 Calendar current = start; while (current.compareTo(end) <= 0) { if (current.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) { int year = current.get(Calendar.YEAR); int month = current.get(Calendar.MONTH) + 1; int day = current.get(Calendar.DAY_OF_MONTH); System.out.println(year + "-" + month + "-" + day); } current.add(Calendar.DATE, 1); } } } ``` 在这个示例中,我们首先定义了起始日期和结束日期,然后使用 Calendar.getInstance() 方法获取 Calendar 实例,并将起始日期和结束日期设置到 Calendar 实例中。接着,我们定义一个 current 变量,用于循环遍历起始日期和结束日期之间的所有日期,判断每个日期是否为一,如果是一则输出该日期的年、月、日信息。需要注意的是,在循环中,我们每次将 current 变量加一天,直到到达结束日期为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值