java.time.format.DateTimeFormatter class
java.text.SimpleDateFormat class
java.time.LocalDate class
java.time.LocalTime class
java.time.LocalDateTime class
java.time.Clock class
java.util.Date class
java.sql.Date class
java.util.Calendar class
java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class CurrentDateTimeExample1 {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now));
}
}
// output
2021/01/12 07:23:32
java.text.SimpleDateFormat
import java.text.SimpleDateFormat;
import java.util.Date;
public class CurrentDateTimeExample2 {
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
System.out.println(formatter.format(date));
}
}
// output
12/01/2021 07:24:31
java.time.LocalDate
System.out.println(java.time.LocalDate.now());
// output
2021-01-12
java.time.LocalTime
System.out.println(java.time.LocalTime.now());
// output
07:25:18.447
java.time.LocalDateTime
System.out.println(java.time.LocalDateTime.now());
// output
2021-01-12T07:25:49.922
java.time.Clock
System.out.println(java.time.Clock.systemUTC().instant());
// output
2021-01-12T00:32:30.444Z
java.util.Date
java.util.Date date=new java.util.Date();
System.out.println(date);
// output
Tue Jan 12 07:26:49 ICT 2021
Example 2
long millis=System.currentTimeMillis();
java.util.Date date=new java.util.Date(millis);
System.out.println(date);
// output
Tue Jan 12 07:31:25 ICT 2021
java.sql.Date
long millis=System.currentTimeMillis();
java.sql.Date date=new java.sql.Date(millis);
System.out.println(date);
// output
2021-01-12
Date date=java.util.Calendar.getInstance().getTime();
System.out.println(date);
// output
Tue Jan 12 07:34:32 ICT 2021