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


 
 

Share on Google+Share on Google+

Convert Date to Long

Advertisement
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.

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

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 Long  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
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
DMCA.com