简体   繁体   English

Android 日历获取上一年的上一个月

[英]Android calendar get previous to previous month of previous year

I am facing issue with android calendar, I want to get previous to previous month, when i am trying it with current year this will achieve my goal, but when i am trying with previous year it fails.我遇到了 android 日历的问题,我想在上个月之前,当我在今年尝试它时,这将实现我的目标,但是当我尝试上一年时它失败了。 in my case today is 29/apr/2020 and i want feb/2019 return from calendar.就我而言,今天是 2020 年 4 月 29 日,我希望 2019 年 2 月从日历返回。 please help me with this.请帮我解决一下这个。

my code is following我的代码如下

Date dt = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int month = cal.get(Calendar.MONTH) - 2; // beware of month indexing from zero
            int year = cal.get(Calendar.YEAR)-1;
            int day = cal.get(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month);
            String formatMonth = new SimpleDateFormat("MMM").format(cal.getTime());
            String formatYear = new SimpleDateFormat("yy").format(cal.getTime());```  

java.time and ThreeTenABP java.time 和 ThreeTenABP

Allow me to suggest that you use java.time, the modern Java date and time API, for your date work.请允许我建议您使用 java.time,即现代 Java 日期和时间 API 进行日期工作。

    YearMonth currentMonth = YearMonth.of(2020, Month.APRIL);
    YearMonth backThen = currentMonth.minusYears(1).minusMonths(2);

    String formatMonth = backThen.format(DateTimeFormatter.ofPattern("MMM"));
    String formatYear = backThen.format(DateTimeFormatter.ofPattern("yy"));

    System.out.printf("Month %s; year %s%n", formatMonth, formatYear);

Output on my computer is (locale dependent):我的计算机上的 Output 是(取决于区域设置):

Month feb.;二月; year 19 19 年

For the sake of a reproducible example I hardcoded the year and month.为了一个可重现的例子,我硬编码了年份和月份。 To start from the current month in some time zone use something like this:要从某个时区的当前月份开始,请使用以下内容:

    YearMonth currentMonth = YearMonth.now(ZoneId.of("Europe/Minsk"));

What went wrong in your code?你的代码出了什么问题?

It's the poor and confusing design of the Calendar and GregorianCalendar classes.这是CalendarGregorianCalendar类的糟糕和令人困惑的设计。

You said you ran your code on 29/apr/2020.您说您在 2020 年 4 月 29 日运行了代码。 This causes your Calendar to be set back to 29th February 2019. This date does not exist;这会导致您的Calendar被设置回 2019 年 2 月 29 日。这个日期不存在; there were only 28 days in February last year.去年2月只有28天。 Instead Calendar picks the day after February 28, that is, March 1. Causing you to get an incorrect month.相反, Calendar选择 2 月 28 日之后的一天,即 3 月 1 日。导致您获得错误的月份。

Question: Doesn't java.time require Android API level 26?问:java.time不需要Android API 26级吗?

java.time works nicely on both older and newer Android devices. java.time 适用于较旧和较新的 Android 设备。 It just requires at least Java 6 .它只需要至少Java 6

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.在 Java 8 及更高版本以及更新的 Android 设备(来自 API 级别 26)中,现代 ZDB9734A 内置了23871083ACE16。
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).在非 Android Java 6 和 7 中,获得 ThreeTen Backport,现代类的后向端口(JSR 310 的 ThreeTen;参见底部的链接)。
  • On (older) Android use the Android edition of ThreeTen Backport.在(旧版)Android 上,使用 ThreeTen Backport 的 Android 版本。 It's called ThreeTenABP.它被称为 ThreeTenABP。 And make sure you import the date and time classes from org.threeten.bp with subpackages.并确保从带有子包的org.threeten.bp导入日期和时间类。

Links链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM