Java 8 LocalDateTime example.
LocalDateTime is a date-time without a time-zone in the ISO-8601 calendar system, such as 2016-12-03T10:15:30.
LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second.
Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond
precision.
This class does not store or represent a time-zone. Instead, it is a description of the date, as used for birthdays,
combined with the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional
information such as an offset or time-zone.
LocalDateTime class is immutable and thread-safe.
DateTimeIntroEx |
package com.java2novice.datetime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
public class DateTimeIntroEx {
public static void main(String[] args) {
// get current date and time
LocalDateTime currTime = LocalDateTime.now();
System.out.println("current date time: "+currTime);
// get date alone
LocalDate d1 = currTime.toLocalDate();
System.out.println("current date: " + d1);
// extract date properties
Month mnt = currTime.getMonth();
System.out.println("Month: "+mnt.getValue());
int dayOfMonth = currTime.getDayOfMonth();
System.out.println("Day of the month: "+dayOfMonth);
int year = currTime.getYear();
System.out.println("Year: "+year);
// extract time properties
int hour = currTime.getHour();
System.out.println("current hour: "+hour);
int minutes = currTime.getMinute();
System.out.println("current minutes: "+minutes);
int seconds = currTime.getSecond();
System.out.println("current seconds: "+seconds);
}
}
|
|