java.util.Date

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

A Date object represents a precise moment in time, down to the millisecond. Dates are represented as a long that counts the number of milliseconds since midnight, January 1, 1970, Greenwich Mean Time.

Does this have a year 2000 problem? If so in what year?

To create a Date object for the current date and time use the noargs Date() constructor like this:

Date now = new Date();

To create a Date object for a specific time, pass the number of milliseconds since midnight, January 1, 1970, Greenwich Meantime to the constructor, like this:

Date midnight_jan2_1970 = new Date(24L*60L*60L*1000L);

You can return the number of milliseconds in the Date as a long, using the getTime() method. For example, to time a block of code, you might do this

Date d1 = new Date();

// timed code goes here

Date d2 = new Date();

long elapsed_time = d2.getTime() - d1.getTime();

System.out.println("That took " + elapsed_time + " milliseconds");

You can change a Date by passing the new date as a number of milliseconds since midnight, January 1, 1970, GMT, to the setTime() method, like this:

Date midnight_jan2_1970 = new Date();

midnight_jan2_1970.setTime(24L*60L*60L*1000L);

The before() method returns true if this Date is before the Date argument, false if it's not. For example

if (midnight_jan2_1970.before(new Date())) {

The after() method returns true if this Date is after the Date argument, false if it's not. For example

if (midnight_jan2_1970.after(new Date())) {

The Date class also has the usual hashCode(), equals(), and toString() methods.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航