Convert Date to Timestamp

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.

 Convert  Date   to  Timestamp

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.