Java 安卓:比较今年和明年

Java 安卓:比较今年和明年,java,android,date,calendar,Java,Android,Date,Calendar,在我的应用程序中,我有两个箭头,显示2017年的月份和一些相关数据。当用户单击>时,应显示文本“二月”,以此类推。njzk的评论指出了你逻辑中的小问题 使用java.time 此外,您正在使用麻烦的旧日期时间类,现在是遗留的,被java.time类取代。大部分java.time被向后移植到Android和Java6和Java7,见下文 本地日期 该类表示一个仅限日期的值,不包含一天中的时间和时区 时区 时区对于确定日期至关重要。在任何一个特定的时刻,世界各地的日期都因地区而异。例如,中午夜后几分

在我的应用程序中,我有两个箭头,显示2017年的月份和一些相关数据。当用户单击>时,应显示文本“二月”,以此类推。njzk的评论指出了你逻辑中的小问题

使用java.time 此外,您正在使用麻烦的旧日期时间类,现在是遗留的,被java.time类取代。大部分java.time被向后移植到Android和Java6和Java7,见下文

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

时区 时区对于确定日期至关重要。在任何一个特定的时刻,世界各地的日期都因地区而异。例如,中午夜后几分钟是新的一天,而中仍然是“昨天”

年 您可以使用类型安全对象而不仅仅是整数来维护日历的状态。调用toString或getValue将值呈现给用户

Year year = Year.from( today );
年月 在类中维护日历的状态可能更有意义

本地化 您可以通过enum请求月份的本地化名称。要本地化,请指定:

确定字符串的长度或缩写。 确定a用于翻译日名、月名等的人类语言,b决定缩写、大写、标点符号、分隔符等问题的文化规范。 检索Month对象,并要求它生成本地化的String对象

数学 下个月或上个月,这些课程可以帮你计算。您可以调用加号…或减号…方法

或者,您可以使用该接口来操作值。查找复数名称类中的实现。请注意,java.time使用。新对象是基于原始对象的值生成的,而不是改变或改变对象的状态

LocalDate firstOfNextMonth = today.with( TemporalAdjusters.firstDayOfNextMonth() );
请注意,与问题中的遗留代码相比,此代码要简单得多,可读性也更高

public class CalendarWidget {
    Locale locale = Locale.getDefault() ;  // Or ask the user, such as Locale.CANADA_FRENCH.
    ZoneId zoneId = ZoneId.systemDefault() ; // Or ask the user for desired/expected zone such as `America/Montreal`.
    YearMonth displayedYearMonth = YearMonth.from( LocalDate.now( zoneId ) );  // By default, initialize to ‘today’ in a particular time zone.
…
    void moveToNextMonth() {
        // Increment the year-month.
        this.displayedYearMonth = this.displayedYearMonth.plusMonths( 1 );
        this.updateUserInterface();
    }

    void moveToPreviousMonth() {
        // Decrement the year-month.
        this.displayedYearMonth = this.displayedYearMonth.minusMonths( 1 );
        this.updateUserInterface();
    }

    void updateUserInterface() {
        // Update UI.
        String textForYearLabel = Integer.toString( this.displayedYearMonth.getYear() ) ;
        String textForMonthLabel = this.displayedYearMonth.getMonth().getDisplayName(
            TextStyle.SHORT ,
            this.locale 
        ) ;
        …
    }
关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

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

从哪里获得java.time类

后来 内置的。 标准JavaAPI的一部分,带有捆绑实现。 Java9添加了一些次要功能和修复。 和 大部分java.time功能都在中向后移植到Java6和Java7。 该项目特别针对Android采用了上述Three-Ten Backport。 看见
调用prevMonth=calendar.getTime;在你检查和更改年份之前…@njzk2你的意思是我必须在检查完年份后再放这行吗?@njzk2谢谢我成功地修复了它!
Year year = Year.from( today );
YearMonth ym = YearMonth.from( today );
Month month = today.getMonth();  // Returns an object from enum rather than a mere number.
String output = month.getDisplayName( 
    TextStyle.NARROW ,
    Locale.CANADA_FRENCH 
);
LocalDate sameDateNextMonth = today.plusMonths( 1 );
LocalDate firstOfNextMonth = today.with( TemporalAdjusters.firstDayOfNextMonth() );
public class CalendarWidget {
    Locale locale = Locale.getDefault() ;  // Or ask the user, such as Locale.CANADA_FRENCH.
    ZoneId zoneId = ZoneId.systemDefault() ; // Or ask the user for desired/expected zone such as `America/Montreal`.
    YearMonth displayedYearMonth = YearMonth.from( LocalDate.now( zoneId ) );  // By default, initialize to ‘today’ in a particular time zone.
…
    void moveToNextMonth() {
        // Increment the year-month.
        this.displayedYearMonth = this.displayedYearMonth.plusMonths( 1 );
        this.updateUserInterface();
    }

    void moveToPreviousMonth() {
        // Decrement the year-month.
        this.displayedYearMonth = this.displayedYearMonth.minusMonths( 1 );
        this.updateUserInterface();
    }

    void updateUserInterface() {
        // Update UI.
        String textForYearLabel = Integer.toString( this.displayedYearMonth.getYear() ) ;
        String textForMonthLabel = this.displayedYearMonth.getMonth().getDisplayName(
            TextStyle.SHORT ,
            this.locale 
        ) ;
        …
    }