[an error occurred while processing this directive]

Convert Date to Long

                         

In this section you will learn to convert the date into a long format. The java.util package provides the functionality to convert a date type into a long type. 

Description of program:

The following program helps you in converting a date into a long type. First of all you need a date that needs to be converted. The DateFormat is a constructor to create a new  date format. The getTime() method is used to convert the given date into a long type. This method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. which is of long type.

 

 Here is the code of program:

import java.util.*;
import java.text.*;

public class DateToLong {
  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)
      long longDate=date.getTime();
      System.out.println("Today is " +longDate );
    }
    catch (ParseException e){
      System.out.println("Exception :"+e);   
    }
  }
}

 Output of this program:

C:\date>javac DateToLong.java
C:\date>java DateToLong
Today is 1181500200000

Download this example.

                         

[an error occurred while processing this directive]