Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
62 views
in JAVA by (117k points)
Explain Java Convert Timestamp to Date

Please log in or register to answer this question.

1 Answer

0 votes
by (117k points)

Certainly! In Java, you can convert a timestamp to a date using the java.util.Date and java.sql.Timestamp classes. 

Here's an example code that demonstrates how to convert a timestamp to a date:

import java.util.Date;
import java.sql.Timestamp;

public class TimestampToDateExample {
    public static void main(String[] args) {
        // Example timestamp
        long timestampMillis = 1621300500000L;

        // Convert timestamp to Date
        Date date = new Date(timestampMillis);

        // Display the converted date
        System.out.println("Timestamp: " + timestampMillis);
        System.out.println("Date: " + date);
    }
}
 

In the above code:

  1. We import the necessary classes, java.util.Date and java.sql.Timestamp.

  2. We define a timestamp using the variable timestampMillis. This timestamp represents the number of milliseconds since the Unix epoch (January 1, 1970).

  3. We create a Date object by passing the timestamp to its constructor. The Date class accepts a long value representing the number of milliseconds.

  4. Finally, we display the original timestamp value and the converted date using System.out.println().

When you run this code, it will output something like:

Timestamp: 1621300500000
Date: Sat May 18 12:48:20 GMT 2023
 

Please note that the java.util.Date class is outdated and has been replaced by the java.time package in Java 8 and later versions. It is recommended to use the newer date and time APIs provided by java.time for better functionality and flexibility.

Related questions

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

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

...