java.util.Date

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值