LocalDate date = LocalDate.of(1998, Month.OCTOBER, 24); // create date from 1998-10-24 LocalDate newdate = date.plusDays(100); // add 100 days to it LocalDate dob = LocalDate.parse("1998-02-28"); // create date from string LocalDate dob2 = LocalDate.parse("09-10-2002", DateTimeFormatter.ofPattern("dd-MM-uuuu")); // create date from string using formatter
LocalTime now = LocalTime.now(); // get current time System.out.println(now); LocalTime singtime = LocalTime.now(ZoneId.of("Asia/Singapore")); // get time in timezone Asia/Singapore System.out.println(singtime); LocalTime starttime = LocalTime.of(9,30,00); // create time from hours, mins, secs LocalTime endtime = starttime.plusMinutes(105); // add 105 minutes and get new time
YearMonth dj = YearMonth.of(2012, Month.FEBRUARY); System.out.println(dj.lengthOfMonth()); // 29 System.out.println(dj.plus(10, ChronoUnit.MONTHS)); // add 10 months to dj
MonthDay today = MonthDay.now(); System.out.println(today.getMonthValue());
Year y = Year.of(2012); System.out.println( y.isLeap());
// display date and time of leaving in India and arriving in Singapore ZoneId leavingZone = ZoneId.of("Asia/Calcutta"); ZonedDateTime departure = ZonedDateTime.now(leavingZone); ZoneId arrivingZone = ZoneId.of("Asia/Singapore"); ZonedDateTime arriving = departure.withZoneSameInstant(arrivingZone).plusHours(3); DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a"); String out = departure.format(format); System.out.printf("LEAVING : %s (%s)%n", out, leavingZone); out = arriving.format(format); System.out.printf("ARRIVING: %s (%s)%n", out, arrivingZone);
LocalDate date = LocalDate.of(2014, Month.MAY, 15); System.out.printf("first day of Month: %s%n", date.with(TemporalAdjusters.firstDayOfMonth())); System.out.printf("first Monday of Month: %s%n",date.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));
import java.time.LocalDate; import java.time.Period; import java.time.temporal.ChronoUnit; import java.util.Scanner; public class AgeCalculator { public static void main(String[] args) { System.out.print("Enter date of birth [yyyy-mm-dd]:"); Scanner s = new Scanner(System.in); String dobstr = s.nextLine(); LocalDate dob = LocalDate.parse(dobstr); LocalDate now = LocalDate.now(); Period p = Period.between(dob,now); System.out.printf("%d years,%d months and %d days old!\n",p.getYears(), p.getMonths(), p.getDays()); } }