In this example, we will demonstrate how to get the current date time using LocalDateTime now() method.
Read more about LocalDateTime class with an example at https://www.javaguides.net/2018/08/java-8-localdatetime-class-api-guide.html.
Read more about LocalDateTime class with an example at https://www.javaguides.net/2018/08/java-8-localdatetime-class-api-guide.html.
Java LocalDateTime now() Method Example
LocalDateTime class provides below APIs to create the current date-time and specific date-time object respectively.
- static LocalDateTime now() - Obtains the current date-time from the system clock in the default time-zone.
- static LocalDateTime now(Clock clock) - Obtains the current date-time from the specified clock.
- static LocalDateTime now(ZoneId zone) - Obtains the current date-time from the system clock in the specified time-zone.
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
/**
* Program to demonstrate LocalDateTime Class APIs.
* @author javaguides.net
*
*/
public class LocalDateTimeExample {
public static void main(String[] args) {
createLocalDateTime();
}
private static void createLocalDateTime() {
// Current date time
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime);
// Current date time from specified time-zone
LocalDateTime dateTime2 = LocalDateTime.now(ZoneId.of("UTC"));
System.out.println(dateTime2);
// Current date time from specified clock
LocalDateTime dateTime3 = LocalDateTime.now(Clock.systemUTC());
System.out.println(dateTime3);
// Specific date time
LocalDateTime dateTime4 = LocalDateTime.of(2017, Month.JULY, 12, 10, 35, 55);
System.out.println(dateTime4);
}
}
Output:
2018-08-10T18:08:43.787
2018-08-10T12:38:43.789
2018-08-10T12:38:43.789
2017-07-12T10:35:55
Comments
Post a Comment