关于Date对象
人的出生年月
用户的注册时间
信息发布时间
这些情况都需要记录日期
构造对象
三种构造方江苏
- 无参构造
- 年月日构造
- 年月日时分秒构造
获取现在时间
Date date = new Date();
System.out.println(date);
获取指定的时间
Date date = new Date(年,月,日)
Date date = new Date(年,月,日,时,分,秒)
获取信息
年份
默认拿到的年份需要加1900才可
int year = date.getYear() + 1900;
System.out.println(year);
月份
int month = date.getMonth() + 1;
System.out.println(month);
int month = date.getMonth() + 1;
System.out.println(month);
日期
int date = now.getDate();
System.out.println(date);
星期
int day = now.getDay();
System.out.println(day);
小时
int hours = now.getHours();
System.out.println(hours);
分钟
int minutes = now.getMinutes();
System.out.println(minutes);
秒
int seconds = now.getSeconds();
System.out.println(seconds);
getTime 获取时间戳
long time = now.getTime();
System.out.println(time);
示例 测试程序性能
Date start = new Date();
for (int i = 0; i < 10000; i++) {
System.out.println("hello");
}
Date end = new Date();
long result = end.getTime() - start.getTime();
System.out.println(result);