1. import java.time.Month;
    2. import java.time.Year;
    3. import java.time.LocalDate;
    4. import java.time.DateTimeException;
    5. import java.time.format.DateTimeFormatter;
    6. import java.io.PrintStream;
    7. /**
    8. * This example takes a month and day from the command line and,
    9. * assuming the current year, prints the next payday.
    10. * This example uses the PaydayAdjuster class.
    11. */
    12. public class NextPayday {
    13. public static void main(String[] args) {
    14. Month month = null;
    15. LocalDate date = null;
    16. DateTimeFormatter format;
    17. String out = null;
    18. if (args.length < 2)
    19. {
    20. System.out.printf("Usage: NextPayday <month> <day>%n");
    21. throw new IllegalArgumentException();
    22. }
    23. try {
    24. month = Month.valueOf(args[0].toUpperCase());
    25. } catch (IllegalArgumentException exc) {
    26. System.out.printf("%s is not a valid month.%n", args[0]);
    27. throw exc; // Rethrow the exception.
    28. }
    29. int day = Integer.parseInt(args[1]);
    30. try {
    31. date = Year.now().atMonth(month).atDay(day);
    32. } catch (DateTimeException exc) {
    33. System.out.printf("%s %s is not a valid date.%n", month, day);
    34. throw exc; // Rethrow the exception.
    35. }
    36. LocalDate nextPayday = date.with(new PaydayAdjuster());
    37. try {
    38. format = DateTimeFormatter.ofPattern("yyyy MMM d");
    39. out = date.format(format);
    40. System.out.printf("Given the date: %s%n", out);
    41. out = nextPayday.format(format);
    42. System.out.printf("the next payday: %s%n", out);
    43. } catch (DateTimeException exc) {
    44. System.out.printf("%s can't be formatted!%n", out);
    45. throw exc;
    46. }
    47. }
    48. }
    1. import java.time.LocalDate;
    2. import java.time.DayOfWeek;
    3. import java.time.DateTimeException;
    4. import java.time.temporal.Temporal;
    5. import java.time.temporal.TemporalAdjuster;
    6. import java.time.temporal.TemporalAdjusters;
    7. import java.io.PrintStream;
    8. /**
    9. * This temporal adjuster assumes that payday occurs on the 15th
    10. * and the last day of each month. However, if either of those
    11. * days lands on a weekend, then the previous Friday is used.
    12. */
    13. public class PaydayAdjuster implements TemporalAdjuster {
    14. /**
    15. * The adjustInto method accepts a Temporal instance
    16. * and returns an adjusted LocalDate. If the passed in
    17. * parameter is not a LocalDate, then a DateTimeException is thrown.
    18. */
    19. public Temporal adjustInto(Temporal input) {
    20. LocalDate date = LocalDate.from(input);
    21. int day;
    22. if (date.getDayOfMonth() < 15) {
    23. day = 15;
    24. } else {
    25. day = date.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
    26. }
    27. date = date.withDayOfMonth(day);
    28. if (date.getDayOfWeek() == DayOfWeek.SATURDAY ||
    29. date.getDayOfWeek() == DayOfWeek.SUNDAY) {
    30. date = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
    31. }
    32. return input.with(date);
    33. }
    34. }