1import java.time.format.DateTimeFormatter;
2import java.time.LocalDateTime;
3public class CurrentDateTimeExample1 {
4 public static void main(String[] args) {
5 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
6 LocalDateTime now = LocalDateTime.now();
7 System.out.println(dtf.format(now));
8 }
9}
1// get current date and timein java using Calendar.getTime() method
2import java.util.Calendar;
3import java.util.Date;
4public class UsingCalendarClass
5{
6 public static void main(String[] args)
7 {
8 Date date = Calendar.getInstance().getTime();
9 System.out.println(date);
10 }
11}
1// Get current date and time in java 8
2import java.time.LocalDate;
3import java.time.LocalDateTime;
4import java.time.LocalTime;
5public class CurrentDateTimeJava8
6{
7 public static void main(String[] args)
8 {
9 LocalDate ld = LocalDate.now();
10 System.out.println("Current date: " + ld);
11 // get current time value
12 LocalTime lt = LocalTime.now();
13 System.out.println("Current time: " + lt);
14 // get current date-time value
15 LocalDateTime ldt = LocalDateTime.now();
16 System.out.println("Current date-time: " + ldt);
17 }
18}
1// get current date and time in java using LocalDate.now() method
2import java.time.LocalDate;
3public class UsingLocalDate
4{
5 public static void main(String[] args)
6 {
7 System.out.println(LocalDate.now());
8 }
9}
1// get current date and time in java using LocalDateTime.now() method
2import java.time.LocalDateTime;
3public class UsingLocalDateTime
4{
5 public static void main(String[] args)
6 {
7 System.out.println(LocalDateTime.now());
8 }
9}
1// how to get current date and time in java
2// using Date class and System.currentTimeMillis() method
3import java.util.Date;
4public class UsingDateClass
5{
6 public static void main(String[] args)
7 {
8 Date dt = new Date();
9 System.out.println(dt);
10 long milliSeconds = System.currentTimeMillis();
11 Date date = new Date(milliSeconds);
12 System.out.println(date);
13 }
14}