如何在 Calendar 数据类型中获取从现在开始的一个月?

how to get one month from now in Calendar data type?

我想像这样在我的日期选择器对话框中设置最大日期

如您所见,输入需要 Calendar 数据类型。

我希望最大日期始终是从当前时间算起的一个月。我试过这样

        val now = Calendar.getInstance()
        val currentYear: Int = now.get(Calendar.YEAR)
        val currentMonth: Int = now.get(Calendar.MONTH)
        val currentDay: Int = now.get(Calendar.DAY_OF_MONTH)

        val oneMonthFromNow = now.add(Calendar.MONTH,1)

oneMonthFromNow 类型属于 unit,而不属于 Calendar 数据类型。那么如何从当前日期时间动态添加一个月?

java 或者 kotlin 都可以

First thing I would say to not use Calendar class, use instead LocalDateTime. You will get many articles about how to use this class and why to use.


即使您想使用 Calendar class,那么我会说,如果有什么不工作或没有返回任何东西,您应该研究 class 实现。一定有办法解决你的问题。这种方式将增加您的知识和调试技能。

所以来到你的解决方案,你需要像这样使用

    val now = Calendar.getInstance()
    // val currentYear: Int = now.get(Calendar.YEAR)
    // val currentMonth: Int = now.get(Calendar.MONTH)
    // val currentDay: Int = now.get(Calendar.DAY_OF_MONTH)

    now.add(Calendar.MONTH,1) // Added one month 
    val oneMonthFromNow = now
    // Or 
    // val oneMonthFromNow = now.clone()
    // oneMonthFromNow.add(Calendar.MONTH,1)

now.add(Calendar.MONTH,1) return 类型是单位,因为它正在更新您的 now 日历

val now = Calendar.getInstance() // it return current calendar
    val currentYear: Int = now.get(Calendar.YEAR)
    val currentMonth: Int = now.get(Calendar.MONTH)
    val currentDay: Int = now.get(Calendar.DAY_OF_MONTH)

    //now calendar updated by 1 month
    now.add(Calendar.MONTH,1)

    //this will return your upcoming dates
     val nextYear: Int = now.get(Calendar.YEAR)
    val nextMonth: Int = now.get(Calendar.MONTH)
    val nextDay: Int = now.get(Calendar.DAY_OF_MONTH)

tl;博士

对所有日期时间处理代码使用现代 java.time 类。如果需要与尚未更新到 java.time 的旧代码进行互操作,请与可怕的旧版 类 相互转换。

Calendar c =
GregorianCalendar 
.from(
    LocalDate
    .now(
        ZoneId.systemDefault()
    )                            // Returns a `LocalDate` object.
    .plusMonths( 1 )
    .atStartOfDay(
        ZoneId.systemDefault()
    )                            // Returns a `ZonedDateTime` object. 
)
;

java.time

现代方法使用 java.time 类 几年前取代了可怕的遗留日期时间 类 例如 Calendar.

获取今天的日期。这需要一个时区。对于任何给定时刻,日期在全球范围内因地区而异。

ZoneId z = ZoneId.of( "Africa/Tunis" ; 

或者获取 JVM 当前的默认时区。

ZoneId z = ZoneId.systemDefault() ;

获取今天的日期。

LocalDate today = LocalDate.now( z ) ;

一个月后获取。

LocalDate monthLater = today.plusMonths( 1 ) ;

如果您要与 java.time 尚未更新的旧代码进行互操作,请在传统代码和现代代码之间来回转换 类。

要转​​换,请通过 ZonedDateTime 获取当天的第一时刻。始终让 java.time 确定第一时刻,因为并非所有区域中的所有日期都从 00:00 开始。

ZonedDateTime zdt = monthLater.atStaetOfDay( z ) ;

然后转换为 GregorianCalendar 也是 Calendar

GregorianCalendar gc = GregorianCalendar.from( zdt ) ;


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

Joda-Time project, now in maintenance mode, advises migration to the java.time 类.

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类.

在哪里获取java.time类?