File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Calendar Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar Class" Watch "Calendar Class" New topic

Calendar Class

gary troknya

Joined: Sep 19, 2001
Posts: 2
Hi; I'm a greenhorn at JAVA. How can you get a date's milliseconds without using the deprecated DATE?
Jamie Young
Ranch Hand

Joined: Jun 19, 2001
Posts: 31
Try using the the class DateFormat in java.text . In this class there is a method:
MILLISECOND_FIELD public static final int MILLISECOND_FIELD Useful constant for MILLISECOND field alignment. Used in FieldPosition of date/time formatting.
Plus, much more. There is also a class SimpleDateFormat, and java.util Class GregorianCalendar.
Good Luck, I hope this help.
Ken Peplow

Joined: Sep 13, 2001
Posts: 2
Calendar includes a method for obtaining the milliseconds part of a date:
int get(int field)
Setting the field to Calendar.MILLISECONDS will return the millisecond part of the Calendar instance.
Calendar also includes a method for setting a Calendare to a specific Date:
void setTime (Date date)
Putting it all together:
import java.util.*;
public class Millisecond
public static void main(String[] args)
// Create a date
// milliseconds since January 1, 1970, 00:00:00 GMT
// 01-01-1970 02:25.312
Date d = new Date( (2*60*1000) + (25*1000) + 312 );
Calendar c = new GregorianCalendar();
System.out.println( "Millisecond="
+ c.get(Calendar.MILLISECOND ) );

Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
The class Date is not deprecated, only some of the methods are.
If you call getTime() on a Date object, you will get the "number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object". (API)

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link:
subject: Calendar Class
jQuery in Action, 3rd edition