Also read : Create own LocalDate, Create own LocalDateTime, Clock , time Zone in java 8, Display All TimeZone SORTED By name, TIME (or offset) in java 8, ISO_DATE, Difference two Different TimeZones, Convert LocalDateTime to Time Zone ISO8061
1) Using DATE in java 8 - LocalDate example
import java.time.LocalDate;
/**
* Using DATE in java 8 - LocalDate example
*/
public class LocalDateExample {
public static void main(String[] args) {
//Get current LocalDate
LocalDate localDateToday = LocalDate.now();
//Display LocalDate
System.out.println("localDateToday = "+localDateToday);
System.out.println("DayOfMonth = " + localDateToday.getDayOfMonth());
System.out.println("Month = " + localDateToday.getMonth());
System.out.println("Year = " + localDateToday.getYear());
System.out.println("Era = " + localDateToday.getEra());
System.out.println("DayOfWeek = " + localDateToday.getDayOfWeek());
System.out.println("DayOfYear = " + localDateToday.getDayOfYear());
}
}
/* OUTPUT
localDateToday = 2017-05-12
DayOfMonth = 12
Month = MAY
Year = 2017
Era = CE
DayOfWeek = FRIDAY
DayOfYear = 132
*/
|
2) Display next dates in java 8 - LocalDate example
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class LocalDateExample_displayNextDates {
public static void main(String[] args) {
//Get current LocalDate
LocalDate localDateToday = LocalDate.now();
System.out.println("--Display next dates--");
LocalDate dateTomorrow = localDateToday.plusDays(1);
System.out.println("dateTomorrow = " + dateTomorrow);
System.out.println("nextWeek = " + localDateToday.plusWeeks(1));
System.out.println("nextMonth = " + localDateToday.plusMonths(1));
System.out.println("nextYear = " + localDateToday.plusYears(1));
System.out.println("\n--Display next dates using java.time.temporal.ChronoUnit--");
System.out.println("nextDay = " + localDateToday.plus(1, ChronoUnit.DAYS));
System.out.println("nextWeek = " + localDateToday.plus(1, ChronoUnit.WEEKS));
System.out.println("nextYear = " + localDateToday.plus(1, ChronoUnit.YEARS));
System.out.println("nextcentury = " + localDateToday.plus(1, ChronoUnit.CENTURIES));
}
}
/* OUTPUT
--Display next dates--
dateTomorrow = 2017-05-13
nextWeek = 2017-05-19
nextMonth = 2017-06-12
nextYear = 2018-05-12
--Display next dates using java.time.temporal.ChronoUnit--
nextDay = 2017-05-13
nextWeek = 2017-05-19
nextYear = 2018-05-12
nextcentury = 2117-05-12
*/
|
3) Display previous dates in java 8 - LocalDate example
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class LocalDateExample_displayPreviousDates2 {
public static void main(String[] args) {
//Get current LocalDate
LocalDate localDateToday = LocalDate.now();
System.out.println("\n--Display previous dates--");
LocalDate dateYesterday = localDateToday.minusDays(1);
System.out.println("dateYesterday = " + dateYesterday);
System.out.println("lastWeek = " + localDateToday.minusWeeks(1));
System.out.println("lastMonth = " + localDateToday.minusMonths(1));
System.out.println("lastYear = " + localDateToday.minusYears(1));
System.out.println("\n--Display previous/last dates using java.time.temporal.ChronoUnit--");
System.out.println("lastDay = " + localDateToday.plus(-1, ChronoUnit.DAYS));
System.out.println("lastWeek = " + localDateToday.plus(-1, ChronoUnit.WEEKS));
System.out.println("lastYear = " + localDateToday.plus(-1, ChronoUnit.YEARS));
System.out.println("lastcentury = " + localDateToday.plus(-1, ChronoUnit.CENTURIES));
}
}
/* OUTPUT
--Display previous dates--
dateYesterday = 2017-05-11
lastWeek = 2017-05-05
lastMonth = 2017-04-12
lastYear = 2016-05-12
--Display previous/last dates using java.time.temporal.ChronoUnit--
lastDay = 2017-05-11
lastWeek = 2017-05-05
lastYear = 2016-05-12
lastcentury = 1917-05-12
*/
|
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.