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.

Ads

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.

Advertisements

Share on Google+Share on Google+

Convert Date to Long

Posted on: June 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Convert Date to Long  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Ralph
October 23, 2013
11815 ms after 1/1/1970

Did you run this sample 11815 ms after 1/1/1970 or did you miss to divide longdate by /24/60/60/1000? Or did I miss anything? Regards

Ads

 

Ads