
Compared to the java.util.date API, the new date API’s are immutable and thread safe. Alos it has a lot of new features and methods compared to it’s previous counterpart.
API’s introduced in Java 8 are:
- LocalDate
- LocalTime
- LocalDateTime
import java.time.*;
public class MyClass {
public static void main(String[] args) {
//current date
LocalDate date = LocalDate.now();
System.out.println("Current date is "+
date);
// current time
LocalTime time = LocalTime.now();
System.out.println("Current time is "+
time);
// current time and date
LocalDateTime current = LocalDateTime.now();
System.out.println("Current date and time : "+
current);
// To format a date into a particular type
DateTimeFormatter format =
DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = current.format(format);
System.out.println("Formatted date is "+
formattedDate);
//Get month,day from current date time
Month month = current.getMonth();
int day = current.getDayOfMonth();
System.out.println("Month : "+month+" day : "+
day);
}
}
