【问题标题】:Android: Comparing current year and next yearAndroid:比较今年和明年
【发布时间】:2017-01-29 16:29:24
【问题描述】:

在我的应用程序中,我有 2 个箭头显示 2017 年的月份以及一些与它们相关的数据。当用户单击“>”时,它应该显示文本 February 等。而“

这是我使用的代码:

按钮下一步:

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.MONTH, 1);
Date nextMonth = calendar.getTime();
int yearI = calendar.get(Calendar.YEAR);
if (yearI == 2018)
    calendar.set(Calendar.YEAR,2017);

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM");
String month = simpleDateFormat.format(nextMonth).toUpperCase();

monthName.setText(month);

Log.d("next month", "" + nextMonth);

按钮下一步:

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.MONTH, -1);
Date prevMonth = calendar.getTime();
int yearI = calendar.get(Calendar.YEAR);
if (yearI == 2016)
    calendar.set(Calendar.YEAR,2017);

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM");
String month = simpleDateFormat.format(prevMonth).toUpperCase();

monthName.setText(month);

Log.d("prev month", "" + prevMonth);

它工作正常,但是当我从当前月份的 1 月开始并继续点击下一个直到我通过 12 月时,它会转到明年的 1 月,即 2018 年,因此 2017 年 1 月的数据没有显示它只在第一次显示和当我点击上个月时,它会从 2017 年 1 月返回到 2016 年 12 月,而不是 2017 年 12 月。

如何让它只显示 2017 年的月份?

【问题讨论】:

  • 您在检查和更改年份之前致电prevMonth = calendar.getTime();...
  • @njzk2 你的意思是我必须在我检查年份后输入这条线吗?
  • @njzk2 谢谢我设法修复它!

标签: java android date calendar


【解决方案1】:

njzk 的评论指出了你逻辑中的小问题。

使用 java.time

另外,您正在使用麻烦的旧日期时间类,现在是遗留的,被 java.time 类所取代。大部分 java.time 都向后移植到 Android 和 Java 6 和 Java 7(见下文)。

LocalDate

LocalDate 类表示没有时间和时区的仅日期值。

时区

时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。例如,Paris France 中午夜过后几分钟是新的一天,而 Montréal Québec 中仍然是“昨天”。

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
int yearNumber = today.getYear();
int monthNumber = today.getMonthValue();

Year

您可以使用类型安全的Year 对象而不是单纯的整数来维护日历的状态。调用toStringgetValue 将值呈现给用户。

Year year = Year.from( today );

YearMonth

使用YearMonth 类维护日历状态可能更有意义。

YearMonth ym = YearMonth.from( today );

本地化

您可以通过Month 枚举请求月份的本地化名称。要进行本地化,请指定:

  • TextStyle 确定字符串的长度或缩写。
  • Locale 确定 (a) 用于翻译日期名称、月份名称等的人类语言,以及 (b) 决定缩写、大写、标点符号、分隔符等问题的文化规范。

检索Month 对象,并要求它生成一个本地化的String 对象。

Month month = today.getMonth();  // Returns an object from enum rather than a mere number.
String output = month.getDisplayName( 
    TextStyle.NARROW ,
    Locale.CANADA_FRENCH 
);

数学

这些课程可以为您计算,下个月或上个月。您可以调用plus…minus… 方法。

LocalDate sameDateNextMonth = today.plusMonths( 1 );

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

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

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

从哪里获得 java.time 类?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2011-05-18
    相关资源
    最近更新 更多