Meaningless Drivel is fun!
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
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: 9056
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
It's not a secret anymore!