LocalDateTime is an immutable date-time object that represents a date-time, with default format as yyyy-MM-dd-HH-mm-ss.zzz. It provides a factory method that takes LocalDate and LocalTime input arguments to create LocalDateTime instance. Lets look its usage with a simple example.
In Java 8, some Classes can be added for Date and Time API. The LocalDateTime Class can be Introduced in Java 8. This is an immutable date-time object which can represent the date and time with format yyyy-MM-dd HH-mm-ss. To create the instance of LocalDateTime Class just provide two arguments LocalDate and LocalTime which are the factory methods.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
public class JAVA8 {
public static void main(String[] args) {
//Current Date
LocalDateTime today = LocalDateTime.now();
System.out.println("Current Date And Time="+today);
//Current Date And Time using LocalDate and LocalTime
System.out.println("Current Date And Time"+LocalDateTime.of(LocalDate.now(), LocalTime.now()));
//Creating LocalDateTime by providing input arguments
LocalDateTime specificDate = LocalDateTime.of(2014, Month.JANUARY, 1, 10, 10, 30);
System.out.println("Specific Date="+specificDate);
//Current date in "Asia/Kolkata", you can get it from ZoneId
LocalDateTime todayKolkata = LocalDateTime.now(ZoneId.of("Asia/Kolkata"));
System.out.println("Current Date in IST="+todayKolkata);
}
}
Hope this will help you :)
0 Comment(s)