Also read : Using DATE in java 8 - LocalDate example, 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 and TIME in java 8 - LocalDateTime example
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
/**
* Using DATE and TIME in java 8 - LocalDateTime example
*/
public class LocalDateTimeExample {
public static void main(String[] args) {
//Get current LocalDateTime
LocalDateTime localDateTimeToday = LocalDateTime.now();
//Display LocalDate
System.out.println("localDateTimeToday = "+localDateTimeToday);
System.out.println("DayOfMonth = " + localDateTimeToday.getDayOfMonth());
System.out.println("DayOfMonth = " + localDateTimeToday.getLong(ChronoField.DAY_OF_MONTH));
System.out.println("Month = " + localDateTimeToday.getMonth());
System.out.println("Month = " + localDateTimeToday.getLong(ChronoField.MONTH_OF_YEAR));
System.out.println("Year = " + localDateTimeToday.getYear());
System.out.println("Year = " + localDateTimeToday.getLong(ChronoField.YEAR));
System.out.println("Hour = " + localDateTimeToday.getHour());
System.out.println("Hour = " + localDateTimeToday.getLong(ChronoField.HOUR_OF_DAY));
System.out.println("Minute = " + localDateTimeToday.getMinute());
System.out.println("Minute = " + localDateTimeToday.getLong(ChronoField.MINUTE_OF_HOUR));
System.out.println("Second = " + localDateTimeToday.getSecond());
System.out.println("Second = " + localDateTimeToday.getLong(ChronoField.SECOND_OF_MINUTE));
System.out.println("milliSecond = " + localDateTimeToday.getLong(ChronoField.MILLI_OF_SECOND));
System.out.println("MINUTE_OF_DAY = " + localDateTimeToday.getLong(ChronoField.MINUTE_OF_DAY));
System.out.println("SECOND_OF_DAY = " + localDateTimeToday.getLong(ChronoField.SECOND_OF_DAY));
System.out.println("DAY_OF_WEEK = " + localDateTimeToday.getLong(ChronoField.DAY_OF_WEEK));
System.out.println("DAY_OF_YEAR = " + localDateTimeToday.getLong(ChronoField.DAY_OF_YEAR));
}
}
/* OUTPUT
localDateTimeToday = 2017-05-13T14:12:57.830
DayOfMonth = 13
DayOfMonth = 13
Month = MAY
Month = 5
Year = 2017
Year = 2017
Hour = 14
Hour = 14
Minute = 12
Minute = 12
Second = 57
Second = 57
milliSecond = 830
MINUTE_OF_DAY = 852
SECOND_OF_DAY = 51177
DAY_OF_WEEK = 6
DAY_OF_YEAR = 133
*/
|
2) Display next dates in java 8 - LocalDateTime example
import java.time.LocalDateTime;
/**
* Using DATE and TIME in java 8 - LocalDateTime
*/
public class LocalDateTimeExample_displayNextDates {
public static void main(String[] args) {
//Get current LocalDateTime
LocalDateTime localDateTimeToday = LocalDateTime.now();
System.out.println("--Display next dates--");
LocalDateTime dateTomorrow = localDateTimeToday.plusDays(1);
System.out.println("dateTomorrow = " + dateTomorrow);
LocalDateTime nextWeek = localDateTimeToday.plusWeeks(1);
System.out.println("nextWeek = " + nextWeek);
LocalDateTime nextMonth = localDateTimeToday.plusMonths(1);
System.out.println("nextMonth = " + nextMonth);
LocalDateTime nextYear = localDateTimeToday.plusYears(1);
System.out.println("nextYear = " + nextYear);
LocalDateTime nextHour = localDateTimeToday.plusHours(1);
System.out.println("nextHour = " + nextHour);
LocalDateTime nextMinute = localDateTimeToday.plusMinutes(1);
System.out.println("nextMinute = " + nextMinute);
LocalDateTime nextSecond = localDateTimeToday.plusSeconds(1);
System.out.println("nextSecond = " + nextSecond);
}
}
/* OUTPUT
--Display next dates--
dateTomorrow = 2017-05-14T14:14:47.508
nextWeek = 2017-05-20T14:14:47.508
nextMonth = 2017-06-13T14:14:47.508
nextYear = 2018-05-13T14:14:47.508
nextHour = 2017-05-13T15:14:47.508
nextMinute = 2017-05-13T14:15:47.508
nextSecond = 2017-05-13T14:14:48.508
*/
|
3) Display previous dates in java 8 - LocalDateTime example
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
* Using DATE and TIME in java 8 - LocalDateTime
*/
public class LocalDateTimeExample_displayPreviousDates {
public static void main(String[] args) {
//Get current LocalDateTime
LocalDateTime localDateTimeToday = LocalDateTime.now();
System.out.println("\n--Display previous dates--");
LocalDateTime dateYesterday = localDateTimeToday.minusDays(1);
System.out.println("dateYesterday = " + dateYesterday);
System.out.println("lastWeek = " + localDateTimeToday.minusWeeks(1));
System.out.println("lastMonth = " + localDateTimeToday.minusMonths(1));
System.out.println("lastYear = " + localDateTimeToday.minusYears(1));
System.out.println("\n--Display previous/last dates using java.time.temporal.ChronoUnit--");
System.out.println("lastDay = " + localDateTimeToday.plus(-1, ChronoUnit.DAYS));
System.out.println("lastWeek = " + localDateTimeToday.plus(-1, ChronoUnit.WEEKS));
System.out.println("lastYear = " + localDateTimeToday.plus(-1, ChronoUnit.YEARS));
System.out.println("lastcentury = " + localDateTimeToday.plus(-1, ChronoUnit.CENTURIES));
}
}
/* OUTPUT
--Display previous dates--
dateYesterday = 2017-05-12T14:16:32.048
lastWeek = 2017-05-06T14:16:32.048
lastMonth = 2017-04-13T14:16:32.048
lastYear = 2016-05-13T14:16:32.048
--Display previous/last dates using java.time.temporal.ChronoUnit--
lastDay = 2017-05-12T14:16:32.048
lastWeek = 2017-05-06T14:16:32.048
lastYear = 2016-05-13T14:16:32.048
lastcentury = 1917-05-13T14:16:32.048
*/
|
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.