0 votes
1.6k views
in Java by
How to convert date to milliseconds in java? Please give an appropriate examle.

2 Answers

0 votes
by
I want to explain with java.util.Calendar. Calendar class provides us to get various tpye of functionalities such as: getTime() and getTimeInMillis(). You will in following example:
 
package com;
 
import java.util.Calendar;
import java.util.Date;
 
 
public class DateToMilliSeconds {
 
/**
* @param args
*/
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
Date currDate = cal.getTime();
long milliSeconds = cal.getTimeInMillis();
System.out.println("Calendar Date: " + currDate + ", MilliSeconds: " + milliSeconds);
}
 
}
 
Output:
Calendar Date: Mon Sep 23 16:42:04 IST 2013, MilliSeconds: 1379934724837
0 votes
by
As we know that, getTime() method gives us time in milliseconds.
package com;
 
import java.util.Date;
 
public class DateToMilliSeconds {
 
/**
* @param args
*/
public static void main(String[] args) {
Date currDate = new Date();
long milliSeconds = currDate.getTime();
System.out.println("Current Date: " + currDate + ", MilliSeconds: " + milliSeconds);
}
 
}
 
Output:
Current Date: Mon Sep 23 16:35:34 IST 2013, MilliSeconds: 1379934334139

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...