• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar Class

 
gary troknya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi; I'm a greenhorn at JAVA. How can you get a date's milliseconds without using the deprecated DATE?
 
Jamie Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
c.setTime(d);
System.out.println( "Millisecond="
+ c.get(Calendar.MILLISECOND ) );
}
}

------------------
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic