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

By Chaitanya Singh | Filed Under: java

In this guide, we 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.

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)

This long type time argument represents the milliseconds value since January 1, 1970, 00:00:00 GMT. To get this milliseconds value from the given Timestamp, we can use the getTime() method of Timestamp class.

public long getTime()

Program to Convert Timestamp to Date

Let’s write the java program based on the above information:

import java.sql.Timestamp;
import java.util.Date;
public class JavaExample {
  public static void main(String args[]) {
    //getting the current Timestamp from system
    Timestamp ts = new Timestamp(System.currentTimeMillis());
    //Timestamp to Date
    Date date = new Date(ts.getTime());
    System.out.println("Timestamp is: "+ts);
    System.out.println("Date is: "+date);
  }
}

Output:

Timestamp to Date Example Output

You can display the converted date in various different formats as shown below:

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

References: Date and Timestamp documentation

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

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 – 2022 BeginnersBook . Privacy Policy . Sitemap