In this article, you’ll find several examples to get the current date, current time, current date & time, current date & time in a specific timezone in Java.
Get current Date in Java
import java.time.LocalDate;
public class CurrentDateTimeExample {
public static void main(String[] args) {
// Current Date
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date: " + currentDate);
}
}
Get current Time in Java
import java.time.LocalTime;
public class CurrentDateTimeExample {
public static void main(String[] args) {
// Current Time
LocalTime currentTime = LocalTime.now();
System.out.println("Current Time: " + currentTime);
}
}
Get current Date and Time in Java
import java.time.LocalDateTime;
public class CurrentDateTimeExample {
public static void main(String[] args) {
// Current Date and Time
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current Date & time: " + currentDateTime);
}
}
Get current Date and Time in a specific Timezone in Java
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class CurrentDateTimeExample {
public static void main(String[] args) {
// Current Date and Time in a given Timezone
ZonedDateTime currentNewYorkDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println(currentNewYorkDateTime);
}
}