如何使用 JodaTime 设置固定日期?
How to set a fixed date with JodaTime?
我正在寻找使用 JodaTime 创建固定日期到月份的方法。我有一个 JSpinner 来设置整数值,我想获取这个值并创建日期。例如:JSpinner有15个,我想创建日期2016-09-15
、2016-10-15
、2016-11-15
。
我如何使用 JodaTime 执行此操作?
java.time
Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. The java.time 框架内置于 Java 8 及更高版本中。
许多 java.time 功能被反向移植到 ThreeTen-Backport and further adapted to Android in ThreeTenABP (see 中的 Java 6 & 7)。
java.time.LocalDate
LocalDate
class 表示没有时间和时区的仅日期值。
您可以指定年月日。
LocalDate ld = LocalDate.of( 2016 , 1 , 15 );
您可以切换月份。 java.time classes 使用 Immutable Objects 模式,因此根据原始值生成新的新对象。
LocalDate september = ld.withMonth( 9 ); // 1-12 for January-December.
你可以add/subtract个月。
LocalDate nextMonth = ld.plusMonths( 1 );
LocalDate priorMonth = ld.minusMonths( 1 );
您可以采用任何 LocalDate 并将日期调整为 15 号。
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );
LocalDate fifteenthThisMonth = today.withDayOfMonth( 15 );
陷阱 DateTimeException
在该月的日期数字无效的情况下被抛出,例如二月的 31。
顺便说一下,您也可以找到 YearMonth
or MonthDay
classes handy, and the Month
枚举。
乔达时间
如果您必须使用 Joda-Time,它提供了一个 LocalDate
class 非常类似于 java.time.LocalDate
。
LocalDate ld = new LocalDate( 2016 , 1 , 7 );
LocalDate fifteenthSameMonth = ld.withDayOfMonth( 15 );
我正在寻找使用 JodaTime 创建固定日期到月份的方法。我有一个 JSpinner 来设置整数值,我想获取这个值并创建日期。例如:JSpinner有15个,我想创建日期2016-09-15
、2016-10-15
、2016-11-15
。
我如何使用 JodaTime 执行此操作?
java.time
Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. The java.time 框架内置于 Java 8 及更高版本中。
许多 java.time 功能被反向移植到 ThreeTen-Backport and further adapted to Android in ThreeTenABP (see
java.time.LocalDate
LocalDate
class 表示没有时间和时区的仅日期值。
您可以指定年月日。
LocalDate ld = LocalDate.of( 2016 , 1 , 15 );
您可以切换月份。 java.time classes 使用 Immutable Objects 模式,因此根据原始值生成新的新对象。
LocalDate september = ld.withMonth( 9 ); // 1-12 for January-December.
你可以add/subtract个月。
LocalDate nextMonth = ld.plusMonths( 1 );
LocalDate priorMonth = ld.minusMonths( 1 );
您可以采用任何 LocalDate 并将日期调整为 15 号。
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );
LocalDate fifteenthThisMonth = today.withDayOfMonth( 15 );
陷阱 DateTimeException
在该月的日期数字无效的情况下被抛出,例如二月的 31。
顺便说一下,您也可以找到 YearMonth
or MonthDay
classes handy, and the Month
枚举。
乔达时间
如果您必须使用 Joda-Time,它提供了一个 LocalDate
class 非常类似于 java.time.LocalDate
。
LocalDate ld = new LocalDate( 2016 , 1 , 7 );
LocalDate fifteenthSameMonth = ld.withDayOfMonth( 15 );