Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.*;
- import java.time.format.DateTimeFormatter;
- import java.time.format.FormatStyle;
- import java.time.Month;
- //basic examples of the Java 8 Date and time API
- public class Timing extends MyClass {
- public static void main(String[] args) {
- LocalDate date1 = LocalDate.of(2015, Month.JANUARY, 20);
- LocalDate date2 = LocalDate.of(2015, 1, 20);
- LocalTime time1 = LocalTime.of(22, 00, 13, 1000001);
- LocalDateTime dateTime1 = LocalDateTime.of(2015, Month.JANUARY, 20, 6, 15, 30);
- Period period = Period.ofDays(5);
- date1 = date1.plus(period);
- Duration d = Duration.ofMinutes(100);
- time1 = time1.minus(d);
- LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
- LocalTime time = LocalTime.of(11, 12, 34);
- LocalDateTime fromObjects = LocalDateTime.of(date, time);
- System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
- System.out.println(time.format(DateTimeFormatter.ISO_LOCAL_TIME));
- System.out.println(fromObjects.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
- DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
- System.out.println(shortDateTime.format(fromObjects));
- System.out.println(shortDateTime.format(date1));
- }
- }
Add Comment
Please, Sign In to add comment