package dates; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month; /** * * @author Lefteris Moussiades */ class DatesTimes { static void currentDate() { LocalDate d = LocalDate.now(); //LocalDate d1 = LocalDate.now(); System.out.println(d); for (int i = 0; i < 1000; i++) { LocalTime t = LocalTime.now(); System.out.println(t); } LocalDateTime dt = LocalDateTime.now(); System.out.println(dt); System.out.println(); } static void specificDate() { LocalDate d = LocalDate.of(1971, Month.DECEMBER, 15); LocalDate d1 = LocalDate.of(1971, 12, 15); System.out.println(d); System.out.println(d.equals(d1)); LocalTime t = LocalTime.of(12, 50, 43); LocalTime t1 = LocalTime.of(12, 50, 43, 625); System.out.println(t); System.out.println(t1); LocalDateTime dt = LocalDateTime.of(1971, Month.DECEMBER, 15, 12, 50, 42, 973); System.out.println(dt); System.out.println(); } static void MonthNums() { for (int i = 1; i <= 12; i++) { System.out.println(LocalDate.of(2000, i, 1)); } System.out.println(); } static void dtCreation() { LocalTime t1 = LocalTime.of(3, 35); LocalDate d1 = LocalDate.of(1920, 1, 9); LocalDateTime dt1 = LocalDateTime.of(d1, t1); System.out.println(dt1); } static void useConstructor() { // LocalDate d=new LocalDate(); } static void wrongDate() { LocalDate dt = LocalDate.of(2010, Month.FEBRUARY, 30); } static void addToDate() { LocalDate today = LocalDate.now(); LocalDate tommorow = today.plusDays(1); LocalDate afterAWeek = today.plusWeeks(1); LocalDate afterAMonth = today.plusMonths(1); LocalDate afterAYear = today.plusYears(1); System.out.println("today :" + today); System.out.println("tommorow "+tommorow); System.out.println("afterAWeek "+afterAWeek); System.out.println("afterAMonth "+afterAMonth); System.out.println("afterAYear :"+afterAYear); } static void addToTime() { LocalTime now = LocalTime.now(); LocalTime after2Hours = now.plusHours(2); LocalTime after2Minutes = now.plusMinutes(2); LocalTime after2Seconds = now.plusSeconds(2); LocalTime after1MillionNanos = now.plusNanos(1_000_000); System.out.println("now "+now); System.out.println("after2Hours "+after2Hours); System.out.println("after2Minutes "+after2Minutes); System.out.println("after2Seconds "+after2Seconds); System.out.println("afterMillionNanos "+after1MillionNanos); } static void subtractDate() { LocalDate today = LocalDate.now(); LocalDate yesterday = today.minusDays(1); LocalDate beforeAWeek = today.minusWeeks(1); LocalDate beforeAMonth = today.minusMonths(1); LocalDate beforeAYear = today.minusYears(1); System.out.println(today); System.out.println(yesterday); System.out.println(beforeAWeek); System.out.println(beforeAMonth); System.out.println(beforeAYear); } static void addDateTime() { LocalDateTime now = LocalDateTime.now(); LocalDateTime tommorow = now.plusDays(1); LocalDateTime afterAWeek = now.plusWeeks(1); LocalDateTime afterAMonth = now.plusMonths(1); LocalDateTime afterAYear = now.plusYears(1); LocalDateTime after2Hours = now.plusHours(2); LocalDateTime after2Minutes = now.plusMinutes(2); LocalDateTime after2Seconds = now.plusSeconds(2); LocalDateTime after1MillionNanos = now.plusNanos(1000000); System.out.println(now); System.out.println(tommorow); System.out.println(afterAWeek); System.out.println(afterAMonth); System.out.println(afterAYear); System.out.println(after2Hours); System.out.println(after2Minutes); System.out.println(after2Seconds); System.out.println(after1MillionNanos); } static void chaining() { LocalDateTime dt1 = LocalDateTime.of(LocalDate.now(), LocalTime.now()); LocalDateTime dt2 = LocalDateTime.now(); System.out.println(dt1); System.out.println(dt2); dt1 = dt1.plusYears(1).plusMonths(1); //.minusWeeks(1).minusHours(5); System.out.println(dt1); } static void immutability() { LocalDate dt = LocalDate.of(2010, Month.MARCH, 1); LocalDate dt1=dt; dt=dt.plusYears(10); System.out.println(dt); System.out.println(dt1); } static void getters() { LocalDateTime dt = LocalDateTime.now(); System.out.println(dt.getDayOfMonth() + "/" + dt.getMonth() + "/" + dt.getYear()); } public static void main(String args[]) { //currentDate(); //specificDate(); //MonthNums(); //dtCreation(); //useConstructor(); //wrongDate(); //addToDate(); //addToTime(); //subtractDate(); //addDateTime(); //chaining(); immutability(); //getters(); } }