したがって、このプログラムはユーザー入力を「m/d/yyyy」または「mm/dd/yyyy」の形式の文字列として受け取ります。次に、月を単語として表示します (たとえば、月 1 は "January" になります)。最後に、GregorianCalendar クラスを使用して、年における日付の序数を決定する必要があります。これを行う方法がよくわかりません。どんな助けでも大歓迎です。
import javax.swing.*;
import java.text.*;
import java.util.*;
public class ConvertDate {
public static void main(String[] args) {
String date = "";
String[] monthName =
{"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
Integer[] daysInMonth = {29, 30, 31};
int mm = 0;
int dd = 0;
int yy = 0;
String month = "";
String day = "";
String year = "";
int maxDay = 0;
while(mm < 1 || mm > 12 || dd < 1 || dd > maxDay) {
date = JOptionPane.showInputDialog(null, "Please, enter a date in the format MM/DD/YYYY.");
String[] partsOfTheDate = date.split("/");
mm = Integer.parseInt(partsOfTheDate[0]);
dd = Integer.parseInt(partsOfTheDate[1]);
yy = Integer.parseInt(partsOfTheDate[2]);
switch (mm) {
case 1: maxDay = daysInMonth[2]; // Jan
break;
case 2: maxDay = daysInMonth[0]; // Feb
break;
case 3: maxDay = daysInMonth[2]; // Mar
break;
case 4: maxDay = daysInMonth[1]; // Apr
break;
case 5: maxDay = daysInMonth[2]; // May
break;
case 6: maxDay = daysInMonth[1]; // Jun
break;
case 7: maxDay = daysInMonth[2]; // Jul
break;
case 8: maxDay = daysInMonth[2]; // Aug
break;
case 9: maxDay = daysInMonth[1]; // Sep
break;
case 10: maxDay = daysInMonth[2]; // Oct
break;
case 11: maxDay = daysInMonth[1]; // Nov
break;
case 12: maxDay = daysInMonth[2]; // Dec
break;
default: maxDay = 0;
break;
}
if((mm < 1 || mm > 12) || (dd < 1 || dd > maxDay)) {
System.out.println("You entered " + date + ";\nyou did not include a valid month, day, or both.");
}
else if((mm > 0 && mm < 13) && (dd > 0 && dd <= maxDay)) {
System.out.println("You entered " + date + ";\nthat can also be expressed as " + monthName[mm - 1] + " " + dd + ", " + yy + ".");
}
GregorianCalendar greg = new GregorianCalendar();
greg.setTime(date);
greg.get(greg.DAY_OF_YEAR);
}
}
}