Use different libraries for differnt needs
LocalDate
for just the date
LocalTime
for just the time
LocalDateTime
for both time and date
ZoneDateTime
for time, date and time zone
LocalDate
Examples:
The date to day is 13 May 2018
NOTE the return values is just the toString() representation of LocalDate.
Getting current date, in format YYYY-MM-DD
LocalDate dateNow = LocalDate.now();
//return 2018-05-13
Can do some maths with the date
dateNow.plusDays(3);
// return 2018-05-16
dateNow.minus(5, ChronoUnit.MONTHS);
// returns 2017-12-13
There are lots of methods to use
Can do some comparisions
boolean equal = dateNow.isEqual(dateNow);
// returns true
dateNow.isBefore(dateNow.minusDays(1));
// reutrns false
Formatting the date
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/uuuu");
String formattedDate = dateNow.format(formatter);
//returns 13/05/2018
Parsing string date
LocalDate parseDate = LocalDate.parse("2016-08-16");
LocalDate parsedDateFormatted = LocalDate.parse("16-Aug-2016", DateTimeFormatter.ofPattern("d-MMM-yyyy"));
LocalDate parsedDateWordFormatted = LocalDate.parse("Tue, Aug 16 2016", DateTimeFormatter.ofPattern("E, MMM d yyyy"));
Note the pattern of formatter matches the string representation of the date, and thus can turn into LocalDate format
Creating Date
LocalDate someDate = LocalDate.of(2015, Month.JANUARY, 20);
//returns 2015-01-20
LocalDate someOtherDate = LocalDate.of(2016, 11, 19);
// return 2016-11-19
Get parts of the date
dateNow.get(ChronoField.MONTH_OF_YEAR);
// returns 5 (May is 5th month of year)
DayOfWeek dayOfWeek = dateNow.getDayOfWeek();
// return sunday
Alter parts of the date
LocalDate changedDayOfWeek = dateNow.withDayOfMonth(DayOfWeek.FRIDAY.getValue());
// return 2018-05-05
LocalDate dateWithChangedYear = dateNow.with(ChronoField.YEAR, 2024);
// returns 2024-05-13
LocalTime
Very similar methods to those in LocalDate
LocalDateTime
Very similar methods to those in LocalDate
Links
ZoneDateTime
Duration
Time measurement
A great way of measuring how long a method call takes.
long startTime = System.currentTimeMillis();
someMethodToMeasure();
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;