两个日期之间的开始日期和结束日期 [英] start date and end date between two dates

查看:182
本文介绍了两个日期之间的开始日期和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到每个月的所有开始日期和结束日期之间给定
两年

hi i need to get all the start dates and end date of each and every month in between given two years

pulbic void printStartDateAndEndDate(Date start, Date end){

    for(// start - end){
      Sysout("1 st month starting date: "+ startDateOfMonth+ " End Date"+endDateOfMonth);    
    }

}

如果有人知道如何做请让我知道,需要在Date对象中使用startDateOfMonth和endDateOfMonth。

if somebody knows how to do this please let me know.i need the "startDateOfMonth" and "endDateOfMonth" to be in Date object.

推荐答案

使用 JodaTime ...

LocalDate startDate = new LocalDate(2011, 11, 8);
LocalDate endDate = new LocalDate(2012, 5, 1);

startDate = startDate.withDayOfMonth(1);

while (!startDate.isAfter(endDate)) {
    System.out.println("> " + startDate);
    startDate = startDate.plusMonths(1);
    LocalDate endOfMonth = startDate.minusDays(1);
    System.out.println("< " + endOfMonth);
}

使用Java 8的时间 API

Using Java 8's time API

LocalDate startDate = LocalDate.of(2011, 11, 8);
LocalDate endDate = LocalDate.of(2012, 5, 1);

startDate = startDate.withDayOfMonth(1);

while (!startDate.isAfter(endDate)) {
    System.out.println("> " + startDate);
    startDate = startDate.plusMonths(1);
    LocalDate endOfMonth = startDate.minusDays(1);
    System.out.println("< " + endOfMonth);
}

这篇关于两个日期之间的开始日期和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆