亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java如何使用for循環來填充年和月列表?

Java如何使用for循環來填充年和月列表?

ITMISS 2023-02-23 15:47:02
我如何循環年和月以顯示下面的輸出?顯示期間的限制是當前月份和年份此外,顯示 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 122018 1 2018 2 2018 3 2018 4 2018 5 2018 6 2018 7 2018 8 2018 9 2018 10 2018 11 2018 122019 1 2019 2 2019 3 2019 4 2019 5 2019 6
查看完整描述

3 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

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類取代的可怕的舊日期時間類。

另外,您忽略了時區問題。時區對于確定日期至關重要。對于任何給定時刻,日期在全球范圍內因地區而異。例如,在法國巴黎午夜過后幾分鐘是新的一天,而在魁北克蒙特利爾仍然是“昨天” 。

以、或 等格式指定適當的時區名稱。切勿使用 2-4 字母縮寫,例如或因為它們不是真正的時區、未標準化,甚至不是唯一的(?。?code>Continent/RegionAmerica/MontrealAfrica/CasablancaPacific/AucklandESTIST

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

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 ) ) 

{

    yearMonths.add( ym ) ;

    // Set up the next loop.

    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 ) ) 




查看完整回答
反對 回復 2023-02-23
?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

試試下面的代碼。我正在使用 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++) {

    for (int j = 1; j <= 12; j++) {

        if (i == year && j == month) {

            System.out.print(i + " " + j + " ");

            break;

        }

            System.out.print(i + " " + j + " ");

        }

    }

}

輸出


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 


查看完整回答
反對 回復 2023-02-23
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

在循環中使用年份和月份


for(int i = year-2; i <= year; i++) {    //year


    for(int j= 1; (j <= 12 || (j == month && i == year)); j++) {    //month

        System.out.println("Value of year: " + i); //start from 2017 and iterate to 2 years ahead

        System.out.println("Value of month: " + j); //start from January (index val: 1) and iterate to today's month

    }

}


查看完整回答
反對 回復 2023-02-23
  • 3 回答
  • 0 關注
  • 268 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號