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 Date to Timestamp Conversion

By Chaitanya Singh | Filed Under: java

Timestamp has higher precision as it includes fraction seconds, while a Date is accurate upto seconds as it doesn’t include fraction of seconds. In this guide, we will see java programs to convert a given Date to Timestamp.

Program to Convert Date to Timestamp

Timestamp constructor expects a long argument, it constructs a Timestamp object using the provided milliseconds value. This value must be a long data type.

Timestamp ts = new Timestamp(long time)

To pass the time value as a long data type, we can use the getTime() method of Date class.

public long getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

Let’s write a program using this information:

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

Output:

Java Date to Timestamp example Output

As you can see that the Date and Timestamp values are same except that Timestamp has precision upto fraction seconds. The format of Date and Timestamp is also different, however you can change it using SimpleDateFormat as shown below:

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

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(sdf.format(ts));

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

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

Output:

Date to Timestamp with formatting Example Output

References: Date and Timestamp documentation

Recommended Posts

  • How to get current Timestamp in Java
  • How to get current Date and Time in Java
  • Java Convert LocalDate to Date
  • Java Convert LocalDate to LocalDateTime
  • Java Date Convert 12 hour format to 24 hour format

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