メモ。
日付
package lab; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.YearMonth; import java.util.ArrayList; import java.util.List; public class HelloDate { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.printf("現在日時> %s%n", now); LocalDate today = now.toLocalDate(); System.out.printf("今日> %s%n", today); YearMonth thisMonth = YearMonth.from(today); System.out.printf("今月> %s%n", thisMonth); LocalDate firstDay = thisMonth.atDay(1); System.out.printf("今月1日> %s%n", firstDay); LocalDate lastDay = thisMonth.atEndOfMonth(); System.out.printf("今月末日> %s%n", lastDay); LocalDate firstMonday = firstDay.with(DayOfWeek.MONDAY); if (firstMonday.isBefore(firstDay)) { firstMonday = firstMonday.plusWeeks(1); } System.out.printf("今月第1月曜日> %s%n", firstMonday); List<LocalDate> mondays = new ArrayList<>(); for (LocalDate monday = firstMonday; !monday.isAfter(lastDay); monday = monday.plusWeeks(1)) { mondays.add(monday); } System.out.println("今月の月曜日>"); mondays.forEach(System.out::println); } }
時刻
package lab; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.ArrayList; import java.util.List; public class HelloTime { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.printf("現在日時> %s%n", now); LocalDate today = now.toLocalDate(); System.out.printf("現在日付> %s%n", today); LocalTime currentTime = now.toLocalTime(); System.out.printf("現在時刻> %s%n", currentTime); LocalDateTime todayStart = today.atStartOfDay(); System.out.printf("今日の0時> %s%n", todayStart); LocalDateTime tomorrowStart = today.plusDays(1).atStartOfDay(); System.out.printf("明日の0時> %s%n", tomorrowStart); LocalDateTime today0623 = today.atTime(LocalTime.of(6, 23)); System.out.printf("今日の06:23> %s%n", today0623); List<LocalDateTime> times = new ArrayList<>(); for (LocalDateTime t = today0623; t.isBefore(tomorrowStart); t = t.plusMinutes(20)) { times.add(t); } System.out.println("今日の06:23から20分おきで深夜まで>"); times.forEach(System.out::println); } }