BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Timestamp to Date Conversion

Last Updated: June 10, 2024 by Chaitanya Singh | Filed Under: java

In this guide, you will learn how to convert Timestamp to Date. Timestamp has higher precision than date and is used when we want to include the fractions seconds in Date & Time. There are two ways you can do this:

  • Using java.util.Date
  • Using java.time

Timestamp to Date Conversion in Java

1. Using java.util.Date

In order to convert the Timestamp to Date, we can pass the milliseconds value while creating an object of Date class: Date date = new Date(long time). Let’s write the complete program:

import java.util.Date;

public class TimestampToDate {
public static void main(String[] args) {
// Example timestamp
// This represents a specific date and time
long timestamp = 1629385200000L;

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

// Print the Date
System.out.println(date);
}
}

How to display the Date in different formats:

import java.sql.Timestamp;
import java.util.Date;
import java.text.SimpleDateFormat;
public class JavaExample {
  public static void main(String args[]){
    Timestamp ts = new Timestamp(System.currentTimeMillis());
    Date date = new Date(ts.getTime());

    //Converted Date in various formats
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    System.out.println(sdf.format(date));

    //Day Name
    SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM YY hh:mm:ss");
    System.out.println(sdf2.format(date));

    //include AM and PM
    SimpleDateFormat sdf3 = new SimpleDateFormat("EEE HH:mm:ss aa");
    System.out.println(sdf3.format(date));
  }
}

Output:

Timestamp to Date conversion Java Example

2. Using java.time (Java 8 and later)

The java.time package is introduced in Java 8. It offers various functionalities for date and time manipulation. Let’s see how can we use this package for Timestamp to Date Conversion.

  • new Date(timestamp): Converts the timestamp to a Date object.
  • Instant.ofEpochMilli(timestamp): Converts the timestamp to an Instant.
  • LocalDateTime.ofInstant(instant, ZoneId.systemDefault()): Converts the Instant to a LocalDateTimeusing the system’s default time zone.
  • DateTimeFormatter: Used to format the LocalDateTime into a human-readable string.
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class TimestampToDate {
public static void main(String[] args) {
// Example timestamp
long timestamp = 1629385200000L;

// Convert timestamp to Instant
Instant instant = Instant.ofEpochMilli(timestamp);

// Convert Instant to LocalDateTime (in system default timezone)
LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

// Print the LocalDateTime
System.out.println(dateTime);

// Format and print the LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(dateTime.format(formatter));
}
}

Both the approaches discussed in this article produce desired result. Choose the approach that fits your needs. The java.time package is recommended as it offers modern approach, which may receive further updates in future.

Recommended Posts

  • Java Date to Timestamp Conversion
  • Java Date Formatting with TimeZone
  • How to Convert String to 24 hour Date-Time Format in Java
  • Java LocalDate to ZonedDateTime Conversion
  • How to get current Timestamp in Java
  • Java String to Date Conversion
  • Calculate Number of Days between Two Dates in Java

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap