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}
1SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
2Date date = new Date();
3System.out.println(formatter.format(date));
4
5// ------------------------
6
7System.out.println(LocalDate.now());
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}
1SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
2Date today = new Date();
3System.out.println(formatter.format(today));
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}