java

How to Get Current Date and Time in Java

In this tutorial, we are going to see different ways to display the current system date and time in Java. Using:

  • SimpleDateFormat
  • LocalDate
  • LocalTime

 

Method 1: Get Current Date and Time Using SimpleDateFormat
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
	public static void main(String[] args) {
	  SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	  Date date = new Date();
	  System.out.println(s.format(date));
	}
}

Output:

10/03/2020 23:31:23

 

 

Method 2: Get Current Date and Time Using LocalDate
public class Main {
	public static void main(String[] args) {
	  System.out.println(java.time.LocalDate.now());
	}
}

Output:

2020-03-10

 

 

Method 3: Get Current Date and Time Using LocalTime
public class Main {
	public static void main(String[] args) {
	  System.out.println(java.time.LocalTime.now());
	}
}

Output:

23:34:56.477
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *