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

我如何循环年和月以显示下面的输出?显示期间的限制是当前月份和年份此外,显示 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


ITMISS
浏览 270回答 3
3回答

烙印99

YearMonth&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Represent a specific month as a whole..now(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Determine the current month as seen in the wall-clock time used by the people of a particular region (a time zone).&nbsp; &nbsp; ZoneId.of( "Asia/Tokyo" )&nbsp; &nbsp; // Specify your desired/expected time zone.)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Returns a `YearMonth` object..withMonth( 1 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Move back to January of this year. Returns another `YearMonth` object rather than changing (mutating) the original, per the immutable objects pattern..minusYears( 2 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Jump back two years in the past. Returns yet another `YearMonth` object.java.time您正在使用多年前被现代java.time类取代的可怕的旧日期时间类。另外,您忽略了时区问题。时区对于确定日期至关重要。对于任何给定时刻,日期在全球范围内因地区而异。例如,在法国巴黎午夜过后几分钟是新的一天,而在魁北克蒙特利尔仍然是“昨天” 。以、或 等格式指定适当的时区名称。切勿使用 2-4 字母缩写,例如或因为它们不是真正的时区、未标准化,甚至不是唯一的(!)。Continent/RegionAmerica/MontrealAfrica/CasablancaPacific/AucklandESTISTZoneId&nbsp;z&nbsp;=&nbsp;ZoneId.of(&nbsp;"America/Montreal"&nbsp;)&nbsp;;&nbsp;&nbsp; LocalDate&nbsp;today&nbsp;=&nbsp;LocalDate.now(&nbsp;z&nbsp;)&nbsp;;YearMonth你只关心年份和月份,没有任何日期。所以使用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 ) )&nbsp;{&nbsp; &nbsp; yearMonths.add( ym ) ;&nbsp; &nbsp; // Set up the next loop.&nbsp; &nbsp; ym = ym.plusMonths( 1 ) ;}转储到控制台。System.out.println( yearMonths ) ;在 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 ) )&nbsp;

慕神8447489

试试下面的代码。我正在使用 java 8 和 java.time.LocalDate,LocalDate currentDate = LocalDate.now();int year = currentDate.getYear();int month = currentDate.getMonthValue();for (int i = year - 2; i <= year; i++) {&nbsp; &nbsp; for (int j = 1; j <= 12; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i == year && j == month) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(i + " " + j + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(i + " " + j + " ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出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&nbsp;

喵喔喔

在循环中使用年份和月份for(int i = year-2; i <= year; i++) {&nbsp; &nbsp; //year&nbsp; &nbsp; for(int j= 1; (j <= 12 || (j == month && i == year)); j++) {&nbsp; &nbsp; //month&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Value of year: " + i); //start from 2017 and iterate to 2 years ahead&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Value of month: " + j); //start from January (index val: 1) and iterate to today's month&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java