Home Java Java-conversion Convert Date to Timestamp
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Convert Date to Timestamp

Advertisement
In this section, you will learn to convert a date into a timestamp format.

Convert Date  to Timestamp

     

In this section, you will learn to convert a date into a timestamp format. 

Description of program:

The following program helps you in converting a date into timestamp format. The java.sql.Timestamp class is a thin wrapper around java.util.Date that allows the JDBC API to identify this as an SQL TIMESTAMP value. It adds the ability to hold the SQL TIMESTAMP value in nanoseconds. It also provides formatting and parsing operations to support the JDBC escape syntax for timestamp values. In this example we are just passing the time into an object of  java.sql.Timestamp class as an argument. 

 

 Here is the code of program:

import java.util.*;
import java.text.*;
import java.sql.Timestamp;
public class DateToTimestamp {
 public static void main(String[] args) {
 try {  String str_date="11-June-07";
  DateFormat formatter ; 
 Date date ; 
  formatter = new SimpleDateFormat("dd-MMM-yy");
  date = (Date)formatter.parse(str_date); 
    java.sql.Timestamp timeStampDate = new 
Timestamp(date.getTime());
 System.out.println("Today is " +timeStampDate);
  catch (ParseException e)
  {System.out.println("Exception :"+e);  }  
 
 }
}   

Output of this program:

C:\date>javac DateToMilliseconds.java
C:\date>java DateToMilliseconds
Today is 1181648109265

Download this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 16, 2007

Ask Questions?    Discuss: Convert Date to Timestamp   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
sreenu
May 29, 2011
Answer please

1) What are the changes occured when we cahnge to 4 nodes in a configuration file whilw it was running on 2 nodes. I need answer please, any 1 help please., Thanks in advance. 2)how to convert dd-mon-yyyy to dd-mm-yyyy in datastage. 3)There are 2 servers in unix.x,y,z files are running on server1. I need to copy only x file on server2.....Pls help Thank u
Tarun
June 29, 2011
different output

Hi, I am getting different output than the one provided here for the same code. Output i am getting is: Today is 0007-06-11 00:00:00.0 Can you please check if you are getting correct output for the same code?
Terry
March 7, 2012
Shorter alternative?

How about: Date dt; Timestamp ts = Timestamp.valueOf(dt.getTstamp().toString()+" 00:00:00");
Vidhya Shankar
August 2, 2012
Change the output

Kindly check before uploading outputs
pravat kumar dash
December 5, 2012
not working

converting a date into timestamp format coding is not working in my case.it is giving that date with time but according to your output my output is nit comming.
DMCA.com