Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
84 views
in JAVA by (178k points)
Explain how to get the Current Date and Time in Java

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

To obtain the current date and time in Java, you can use the java.util.Date and java.util.Calendar classes or the newer java.time API introduced in Java 8. Here's an example code snippet demonstrating both approaches:

Using java.util.Date and java.util.Calendar:

import java.util.Date;
import java.util.Calendar;

public class GetCurrentDateTimeExample {
    public static void main(String[] args) {
        // Get the current date and time using java.util.Date
        Date currentDate = new Date();
        System.out.println("Current Date and Time using java.util.Date: " + currentDate);

        // Get the current date and time using java.util.Calendar
        Calendar calendar = Calendar.getInstance();
        Date currentDateTime = calendar.getTime();
        System.out.println("Current Date and Time using java.util.Calendar: " + currentDateTime);
    }
}
 

Using java.time API:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class GetCurrentDateTimeExample {
    public static void main(String[] args) {
        // Get the current date and time using java.time.LocalDateTime
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("Current Date and Time using java.time.LocalDateTime: " + currentDateTime);

        // Format the current date and time using a specific pattern
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentDateTime.format(formatter);
        System.out.println("Formatted Current Date and Time: " + formattedDateTime);
    }
}
 

Both approaches will provide you with the current date and time. However, the java.util.Date and java.util.Calendar classes are considered legacy and have been replaced by the more modern and flexible java.time API. It is recommended to use the java.time API for new projects whenever possible.

Related questions

0 votes
1 answer
asked May 17, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
asked May 17, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
asked May 17, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
asked May 17, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
asked May 17, 2023 in JAVA by kvdevika (178k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...