Dates and time in Java
Class Description
LocalDate Represents a date (year, month, day (yyyy-MM-dd))
LocalTime Represents a time (hour, minute, second and nanoseconds (HH-mm-ss-ns))
LocalDateTime Represents both a date and a time (yyyy-MM-dd-HH-mm-ss-ns)
DateTimeFormatter Formatter for displaying and parsing date-time objects
import java.time.*; // import the classes above to use the methods
Display current date
LocalDate myObj = LocalDate.now(); // Create a date object
System.out.println(myObj); // Display the current date
Display current time
LocalTime myObj = LocalTime.now();
System.out.println(myObj);
Display Date and time
LocalDateTime myObj = LocalDateTime.now();
System.out.println(myObj);
Formatting
LocalDateTime myDateObj = LocalDateTime.now();
System.out.println("Before formatting: " + myDateObj);
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy
HH:mm:ss");
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("After formatting: " + formattedDate);
Isolating parts of the dates and times- when typing this after the full stop wait to see the options. If you want to fetch a
particular part of the date start typing get and see the options.
LocalDateTime dt=LocalDateTime.now();
System.out.println(dt.getDayOfWeek());
System.out.println(dt.getHour());
This also applies when using objects of LocaTime and LocalDate
Finding out is it’s a leap year
System.out.println(LocalDate.of(2022, 3, 12).isLeapYear());
Comparing times
LocalTime a= LocalTime.of(5,20,00);
LocalTime b= LocalTime.of(5,20,00);
System.out.println(a.equals(b));
System.out.println(a.isBefore(b));
System.out.println(a.isAfter(b));
Comparing Dates
LocalDate d1= LocalDate.of(2020,2,5);
LocalDate d2= LocalDate.of(2021,2,15);
System.out.println(d1.equals(d2));
System.out.println(d1.isBefore(d2));
System.out.println(d1.isAfter(d2));
Adjusting Dates and times
LocalDate d1= LocalDate.of(2020,2,5);
System.out.println(d1.plusYears(1)); - this will display the year to 2021
Changing year and month in the object
d1=d1.plusYears(1);
d1=d1.minusMonths(2);
LocalTime a= LocalTime.of(5,20,00);
a=a.minusHours(1);
a=a.plusMinutes(4);
Calculating the difference between dates and times
Using a Period object
LocalDate d1= LocalDate.of(2019,2,5);
LocalDate d2= LocalDate.of(2020,3,15);
Period diff=Period.between(d1, d2); Gives the result in this format - P1Y1M10D - 1 year, 1 month 10 days
Refining the output
System.out.println("years"+diff.getYears()+" months "+diff.getMonths()+ " days "+diff.getDays());
Working with time is similar except you are using the Duration object
LocalTime a= LocalTime.of(7,20,00);
LocalTime b= LocalTime.of(5,35,15);
Duration tDiff= Duration.between(a, b); - gives the result as PT-1H-44M-45S
System.out.println(tDiff);
Changing the format of the date and time
LocalDate df= LocalDate.of(2020,3,15); - converts a String to a LocalDate format
LocalDate ld=LocalDate.parse("2021-05-03");
Setting a specific format
LocalDate ld=LocalDate.parse("2021/05/12",DateTimeFormatter.ofPattern("yyyy/MM/dd"));
System.out.println(ld);
LocalTime lt= LocalTime.parse("12 20 30",DateTimeFormatter.ofPattern("HH mm ss"));
System.out.println(lt);
Reading a Date from a Text File
When you read a date from a text file it is a String. You will have to convert it to a date format
Using LocalDate as the data type
tmp2=scLine.next();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate birthDate= LocalDate.parse(tmp2, dateTimeFormatter);
birthdate can now be used as a variable of LocalDate
Using Date as the data type
String tmp=scLine.next();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
Date DOB=new SimpleDateFormat("dd/MM/yyyy").parse(tmp);