1. /**
    2. * Display the numnber of days in each month of the specified year.
    3. */
    4. import java.time.Month;
    5. import java.time.Year;
    6. import java.time.YearMonth;
    7. import java.time.DateTimeException;
    8. import java.io.PrintStream;
    9. import java.lang.NumberFormatException;
    10. public class MonthsInYear {
    11. public static void main(String[] args) {
    12. int year = 0;
    13. if (args.length <= 0) {
    14. System.out.printf("Usage: MonthsInYear <year>%n");
    15. throw new IllegalArgumentException();
    16. }
    17. try {
    18. year = Integer.parseInt(args[0]);
    19. } catch (NumberFormatException nexc) {
    20. System.out.printf("%s is not a properly formatted number.%n",
    21. args[0]);
    22. throw nexc; // Rethrow the exception.
    23. }
    24. try {
    25. Year test = Year.of(year);
    26. } catch (DateTimeException exc) {
    27. System.out.printf("%d is not a valid year.%n", year);
    28. throw exc; // Rethrow the exception.
    29. }
    30. System.out.printf("For the year %d:%n", year);
    31. for (Month month : Month.values()) {
    32. YearMonth ym = YearMonth.of(year, month);
    33. System.out.printf("%s: %d days%n", month, ym.lengthOfMonth());
    34. }
    35. }
    36. }