How do I get the current time in Java?

Asked 2 years ago, Updated 2 years ago, 41 views

What is the best way to get the current time in Java?

java

2022-09-22 22:29

1 Answers

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));

I

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));

If you want to receive the current time as String, you can do it like this.


2022-09-22 22:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.