Java 如何使用for循环填充年份和月份列表?

标签 java for-loop calendar

如何循环年份和月份以显示以下输出? 显示期间的限制为当前月份和年份 此外,还显示 3 年(包括截至日期的当年)。意思是如果现在是 2019 年,请显示 2018 年和 2017 年。

我尝试使用一些代码作为 java 应用程序运行,希望得到下面的预期输出,但这就是我尝试过并得到的结果。

如果有人能在这里阐明一些观点,我将不胜感激。

public class TestClass {
public static void main(String[] args) {
    Calendar today = Calendar.getInstance();
    //month=5, index starts from 0
    int month = today.get(Calendar.MONTH);
    //year=2019
    int year = today.get(Calendar.YEAR);

    for(int i = 0; i < 3; i++) {    //year
        for(int j= 0; j <= month; j++) {    //month
        System.out.println("Value of year: " + (year - 2)); //start from 2017 and iterate to 2 years ahead
        System.out.println("Value of month: " + (month + 1)); //start from January (index val: 1) and iterate to today's month
        }
    }
}

}

预期输出:

2017年 1 2017年 2 2017年 3 2017年 4 2017年 5 2017年 6 2017年 7 2017年 8 2017年 9 2017年 10 2017年 11 2017年 12

2018年 1 2018年 2 2018年 3 2018年 4 2018年 5 2018年 6 2018年 7 2018年 8 2018年 9 2018年 10 2018年 11 2018年 12

2019年 1 2019年 2 2019年 3 2019年 4 2019年 5 2019年 6

最佳答案

tl;博士

YearMonth                        // Represent a specific month as a whole.
.now(                            // Determine the current month as seen in the wall-clock time used by the people of a particular region (a time zone).
    ZoneId.of( "Asia/Tokyo" )    // Specify your desired/expected time zone.
)                                // Returns a `YearMonth` object.
.withMonth( 1 )                  // Move back to January of this year. Returns another `YearMonth` object rather than changing (mutating) the original, per the immutable objects pattern.
.minusYears( 2 )                 // Jump back two years in the past. Returns yet another `YearMonth` object.

java.time

您正在使用可怕的旧日期时间类,这些类在几年前已被现代 java.time 类取代。

此外,您忽略了时区问题。时区对于确定日期至关重要。对于任何特定时刻,全局各地的日期都会因地区而异。例如,Paris France 午夜过后几分钟Montréal Québec 是新的一天,但仍然是“昨天” .

指定proper time zone name格式为Continent/Region,例如America/MontrealAfrica/Cas​​ablancaPacific/Auckland 。切勿使用 2-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,不是标准化的,甚至不是唯一的( !)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

年月

你只关心年份和月份,没有任何日期。所以使用YearMonth类。

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
YearMonth yearMonthNow = YearMonth.now( z ) ;

YearMonth yearMonthJanuaryTwoYearAgo = yearMonthNow.withMonth( 1 ).minusYears( 2 ) ;

每次增加一个月,边走边收集。

ArrayList< YearMonth > yearMonths = new ArrayList<>();
YearMonth ym = yearMonthJanuaryTwoYearAgo ;
while( ! ym.isAfter( yearMonthNow ) ) 
{
    yearMonths.add( ym ) ;
    // Set up the next loop.
    ym = ym.plusMonths( 1 ) ;
}

转储到控制台。

System.out.println( yearMonths ) ;

查看此code run live at IdeOne.com .

[2017-01, 2017-02, 2017-03, 2017-04, 2017-05, 2017-06, 2017-07, 2017-08, 2017-09, 2017-10, 2017-11, 2017-12, 2018-01, 2018-02, 2018-03, 2018-04, 2018-05, 2018-06, 2018-07, 2018-08, 2018-09, 2018-10, 2018-11, 2018-12, 2019-01, 2019-02, 2019-03, 2019-04, 2019-05, 2019-06]

如果您想排除当前月份,请使用:

while( ym.isBefore( yearMonthNow ) ) 

关于Java 如何使用for循环填充年份和月份列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783241/

相关文章:

java - Nginx 配置从 java 代码更改

java - java的循环问题

java - Calendar.getInstance() 性能通过 'warm up' 调用得到改进?

java - 查找 ArrayList 是否至少包含一个不同的字符串

java - 将现有 jaxb 类结构的继承更改为组合,保持当前 xml 结构不变

php - For循环没有给出预期的重复

javascript - 如何在 JavaScript 中将数组中的随机值分配给另一个数组中的项目(将名称数组分配给工作日数组)?

c++ - 在 C++ 效率结构中创建日历应用程序

java - 如何使静态日历线程安全

java - 无法运行Java Spark Hive示例