Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JodaTime-两个日期之间每个月的天数_Java_Date_Datetime_Jodatime - Fatal编程技术网

Java JodaTime-两个日期之间每个月的天数

Java JodaTime-两个日期之间每个月的天数,java,date,datetime,jodatime,Java,Date,Datetime,Jodatime,我有两次这样的约会: DateTime startingDate = new DateTime(STARTING_YEAR, STARTING_MONTH, STARTING_DAY, 0, 0); DateTime endingDate = new DateTime(ENDING_YEAR, ENDING_MONTH, ENDING_DAY, 0, 0); TOTAL_DAYS = Days.daysBetween(startingDate, endingDate).getDays();

我有两次这样的约会:

DateTime startingDate = new DateTime(STARTING_YEAR, STARTING_MONTH, STARTING_DAY, 0, 0);
DateTime endingDate = new DateTime(ENDING_YEAR, ENDING_MONTH, ENDING_DAY, 0, 0);

TOTAL_DAYS = Days.daysBetween(startingDate, endingDate).getDays();
知道两个日期之间的总天数很容易,但我对API一点也不熟悉,我想知道是否有更简单的方法可以找到两个日期之间每个月的天数,而不需要循环和ifs

例如:

DateTime startingDate = new DateTime(2000, 1, 1, 0, 0);
DateTime endingDate = new DateTime(2000, 2, 3, 0, 0);
1月31日,2月2日


谢谢

我终于用一个循环完成了

DateTime startingDate = new DateTime(STARTING_YEAR, STARTING_MONTH, STARTING_DAY, 0, 0);
DateTime endingDate = new DateTime(ENDING_YEAR, ENDING_MONTH, ENDING_DAY, 0, 0);
TOTAL_DAYS = Days.daysBetween(startingDate, endingDate).getDays();

DateTime currentDate = startingDate;

System.out.println(currentDate.dayOfMonth().getMaximumValue() - currentDate.dayOfMonth().get() + 1);
currentDate = currentDate.plus(Period.months(1));

while (currentDate.isBefore(endingDate)) { 
     System.out.println(currentDate.dayOfMonth().getMaximumValue());
     currentDate = currentDate.plus(Period.months(1));
 }  
System.out.println(endingDate.dayOfMonth().get());

我最后还是打了个圈

DateTime startingDate = new DateTime(STARTING_YEAR, STARTING_MONTH, STARTING_DAY, 0, 0);
DateTime endingDate = new DateTime(ENDING_YEAR, ENDING_MONTH, ENDING_DAY, 0, 0);
TOTAL_DAYS = Days.daysBetween(startingDate, endingDate).getDays();

DateTime currentDate = startingDate;

System.out.println(currentDate.dayOfMonth().getMaximumValue() - currentDate.dayOfMonth().get() + 1);
currentDate = currentDate.plus(Period.months(1));

while (currentDate.isBefore(endingDate)) { 
     System.out.println(currentDate.dayOfMonth().getMaximumValue());
     currentDate = currentDate.plus(Period.months(1));
 }  
System.out.println(endingDate.dayOfMonth().get());
双日=(endingDate.getMillis()-startingDate.getMillis())/86400000.0

它以浮点数的形式给出天数。如果只需要完整天数,请截断。

双倍天数=(endingDate.getMillis()-startingDate.getMillis())/86400000.0

它以浮点数的形式给出天数。如果只需要完整天数,请截断。

这可能会有帮助:

DateTime startingDate = new DateTime(2000, 1, 1, 0, 0);
DateTime endingDate = new DateTime(2000, 2, 3, 0, 0);

Duration duration = new Duration(startingDate, endingDate);

System.out.println(duration.getStandardDays());//get the difference in number of days
这可能有助于:

DateTime startingDate = new DateTime(2000, 1, 1, 0, 0);
DateTime endingDate = new DateTime(2000, 2, 3, 0, 0);

Duration duration = new Duration(startingDate, endingDate);

System.out.println(duration.getStandardDays());//get the difference in number of days
仅供参考,该项目目前正在进行中,建议迁移到java.time

java.time 如果你想单独处理每个月的问题,你需要迭代。但是,这项工作在某种程度上被课堂简化了。此外,您可以使用流来掩盖迭代

半开 time类明智地使用半开放的方法来定义时间跨度。这意味着开始是包含的,而结束是独占的。因此,一系列月份需要在目标月结束后的下一个月结束

TemporalAdjuster
该接口提供对日期时间值的操作。该类(注意复数
s
)提供了几个方便的实现。在这方面,我们需要:

LocalDate
该类表示一个仅限日期的值,不包含一天中的时间和时区

LocalDate startDate = LocalDate.of( 2000 , 1 , 1 );
YearMonth ymStart = YearMonth.from( startDate );

LocalDate stopDate = LocalDate.of( 2000 , 2 , 3 );
LocalDate stopDateNextMonth = stopDate.with( TemporalAdjusters.firstDayOfNextMonth() );
YearMonth ymStop = YearMonth.from( stopDateNextMonth );
在两者之间每月循环一次

顺便说一句,您可以通过enum对象请求月份的本地化名称

YearMonth ym = ymStart;
do {
    int daysInMonth = ym.lengthOfMonth ();
    String monthName = ym.getMonth ().getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH );

    System.out.println ( ym + " : " + daysInMonth + " jours en " + monthName );

    // Prepare for next loop.
    ym = ym.plusMonths ( 1 );
} while ( ym.isBefore ( ymStop ) );
2000-01:31月

2000-02:29焦耳


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到java.time

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

从哪里获得java.time类

  • 后来
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 该项目专门针对Android采用了Three Ten Backport(如上所述)
该项目使用其他类扩展了java.time。这个项目是java.time将来可能添加的一个试验场。您可能会在这里找到一些有用的类,如、、和。

仅供参考,该项目建议迁移到java.time

java.time 如果你想单独处理每个月的问题,你需要迭代。但是,这项工作在某种程度上被课堂简化了。此外,您可以使用流来掩盖迭代

半开 time类明智地使用半开放的方法来定义时间跨度。这意味着开始是包含的,而结束是独占的。因此,一系列月份需要在目标月结束后的下一个月结束

TemporalAdjuster
该接口提供对日期时间值的操作。该类(注意复数
s
)提供了几个方便的实现。在这方面,我们需要:

LocalDate
该类表示一个仅限日期的值,不包含一天中的时间和时区

LocalDate startDate = LocalDate.of( 2000 , 1 , 1 );
YearMonth ymStart = YearMonth.from( startDate );

LocalDate stopDate = LocalDate.of( 2000 , 2 , 3 );
LocalDate stopDateNextMonth = stopDate.with( TemporalAdjusters.firstDayOfNextMonth() );
YearMonth ymStop = YearMonth.from( stopDateNextMonth );
在两者之间每月循环一次

顺便说一句,您可以通过enum对象请求月份的本地化名称

YearMonth ym = ymStart;
do {
    int daysInMonth = ym.lengthOfMonth ();
    String monthName = ym.getMonth ().getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH );

    System.out.println ( ym + " : " + daysInMonth + " jours en " + monthName );

    // Prepare for next loop.
    ym = ym.plusMonths ( 1 );
} while ( ym.isBefore ( ymStop ) );
2000-01:31月

2000-02:29焦耳


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到java.time

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

从哪里获得java.time类

  • 后来
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 该项目专门针对Android采用了Three Ten Backport(如上所述)

该项目使用其他类扩展了java.time。这个项目是java.time将来可能添加的一个试验场。您可以在这里找到一些有用的类,例如、、和。

如果您想在开始和结束之间的每个月进行迭代,以确定每月的天数,那么您可能需要任何类型的循环。难以避免。这是任务/问题的本质。有关使用java.time类和lambda语法的现代解决方案,请参阅类似问题
MapMap=start.datesUntil(stop.collector.groupingBy((LocalDate LocalDate)->YearMonth.from(LocalDate),TreeMap::new,Collectors.counting()                         )                 );如果您想在开始和结束之间每月迭代一次,以确定每月的天数,那么您可能需要任何类型的循环。难以避免。这是您任务/问题的本质。对于任务单