6

今日の日付、曜日、100日後の日付、100日後の曜日、誕生日、曜日など、いくつかのことを印刷しようとしています。 、および私の誕生日とその曜日から10、000日後。さて、GregorianCalendarは1月は0から始まり、12月は11になることを理解しています。私はそれを理解しているので、今日の日付が9/25/12ではなく8/25/12であると表示されている日付を印刷しようとすると意味がありますが、日付を余分に設定せずにこれを修正する方法がわかりません月を作成し、実際にその月を9月ではなく10月に入れます。

これが私が現在扱っていることです。

        GregorianCalendar cal = new GregorianCalendar();
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int weekday = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_MONTH, 100);
        int dayOfMonth2 = cal.get(Calendar.DAY_OF_MONTH);
        int month2 = cal.get(Calendar.MONTH);
        int year2 = cal.get(Calendar.YEAR);
        int weekday2 = cal.get(Calendar.DAY_OF_WEEK);
        GregorianCalendar birthday = new GregorianCalendar(1994, Calendar.JANUARY, 1);
        int dayOfMonth3 = birthday.get(Calendar.DAY_OF_MONTH);
        int month3 = birthday.get(Calendar.MONTH);
        int year3 = birthday.get(Calendar.YEAR);
        int weekday3 = birthday.get(Calendar.DAY_OF_WEEK);
        birthday.add(Calendar.DAY_OF_MONTH, 10000);
        int weekday4 = birthday.get(Calendar.DAY_OF_WEEK);
        int dayOfMonth4 = birthday.get(Calendar.DAY_OF_MONTH);
        int month4 = birthday.get(Calendar.MONTH);
        int year4 = birthday.get(Calendar.YEAR);
        System.out.printf("Todays date is " +month + "/" +dayOfMonth +"/" +year +".");
        System.out.printf(" It is day " +weekday +" of the week");
        System.out.println("");
        System.out.printf("In 100 days it will be " +month2 + "/" +dayOfMonth2 +"/" +year2 +". ");
        System.out.printf("Day " +weekday2 +" of the week");
        System.out.println("");
        System.out.printf("My Birthday is " +month3 + "/" +dayOfMonth3 +"/" +year3 +". "+"Day " +weekday3 +" of the week");
        System.out.println("");
        System.out.printf("10,000 days after my birthday is " +month4 + "/" +dayOfMonth4 +"/" +year4 +". " +"Day " +weekday4 +" of the week");

そのため、今日の日付、100日後の日付、誕生日の日付、および誕生日の1万日後の月を修正するためのサポートが必要です。どんな助けや洞察も大歓迎です。

4

3 に答える 3

9

私はそれを理解しているので、今日の日付が9/25/12ではなく8/25/12であると表示されている日付を印刷しようとすると意味がありますが、日付を余分に設定せずにこれを修正する方法がわかりません月

次のようにして月を印刷する場合

int month = cal.get(Calendar.MONTH);
...
 System.out.printf("Todays date is " + month + ...

month + 1次に、だけでなく、を印刷しますmonth

SimpleDateFormat最終的には、日付を文字列としてフォーマットするために使用するだけで、時間と頭痛の種を大幅に節約できます。

于 2012-09-25T23:38:12.677 に答える
6

はい、Calendar.JANUARYがゼロに等しいことを知っておく必要があります。カレンダーの月はゼロベースです。

あなたはここであまりにも一生懸命働いています。プリミティブを扱いすぎています。

今日の日付を印刷する方法は次のとおりです。

DateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd");
formatter.setLenient(false);
Date today = new Date();
System.out.println(formatter.format(today));  

今から100日後の取得方法は次のとおりです。

Calendar calendar = Calendar.getInstance();
calendar.setTime(today);
calendar.add(Calendar.DAY_OF_YEAR, 100);
Date oneHundredDaysFromToday = calendar.getTime();
System.out.println(formatter.format(oneHundredDaysFromToday));

これらすべてのint値の処理を停止します。

于 2012-09-25T23:38:03.867 に答える
2
于 2018-02-06T02:36:25.493 に答える