Java — How to get current Date and Time | Code Factory

Get current Date in Java

package com.example.java.programming.datetime;import java.time.LocalDate;/**
* @author code.factory
*
*/
public class CurrentDateExample {
public static void main(String... args) {
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date : " + currentDate);
}
}
Current Date : 2020-06-29

Get current Time in Java

package com.example.java.programming.datetime;import java.time.LocalTime;/**
* @author code.factory
*
*/
public class CurrentTimeExample {
public static void main(String... args) {
LocalTime currentTime = LocalTime.now();
System.out.println("Current Time : " + currentTime);
}
}
Current Time : 14:00:59.165

Get current Date and Time in Java

package com.example.java.programming.datetime;import java.time.LocalDateTime;/**
* @author code.factory
*
*/
public class CurrentDateTimeExample {
public static void main(String... args) {
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current Date & Time : " + currentDateTime);
}
}
Current Date & Time : 2020-06-29T14:03:56.960

Get current Date and Time in a specific Timezone in Java

package com.example.java.programming.datetime;import java.time.ZoneId;
import java.time.ZonedDateTime;
/**
* @author code.factory
*
*/
public class CurrentDateTimeExample {
public static void main(String... args) {
ZonedDateTime currentKolkataDateTime = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
System.out.println("Current Date & Time : " + currentKolkataDateTime);
}
}
Current Date & Time : 2020-06-29T14:11:49.193+05:30[Asia/Kolkata]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store