2017-11-02 22:29:34
Date类:类 Date 表示特定的瞬间,精确到毫秒。
在 JDK 1.1 之前,类 Date 有两个其他的函数。它允许把日期解释为年、月、日、小时、分钟和秒值。它也允许格式化和解析日期字符串。不过,这些函数的 API 不易于实现国际化。从 JDK 1.1 开始,应该使用 Calendar 类实现日期和时间字段之间转换,使用 DateFormat 类来格式化和解析日期字符串。Date 中的相应方法已废弃。
Calender类:Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。
与其他语言环境敏感类一样,Calendar 提供了一个类方法 getInstance,以获得此类型的一个通用的对象。Calendar 的 getInstance 方法返回一个 Calendar 对象,其日历字段已由当前日期和时间初始化:
Calendar rightNow = Calendar.getInstance();
*常用属性




public class Main {
public static void main(String[] args) {
Calendar rightnow= Calendar.getInstance();
int year=rightnow.get(Calendar.YEAR);
int month=rightnow.get(Calendar.MONTH);
int date=rightnow.get(Calendar.DATE);
System.out.println(year+"年"+(month+1)+"月"+date+"日");
}
}
根据日历的规则,为给定的日历字段添加或减去指定的时间量。例如,要从当前日历时间减去 5 天,可以通过调用以下方法做到这一点:add(Calendar.DAY_OF_MONTH, -5)。
/**
* 获取任意一年的二月份的天数
*
* 分析:
* A:键盘输入任意的年份
* B:设置日历的年月日
* C:设置月:2
* 日:1(其实是3月1号)
* D:把时间往前推一天
* E:输出日期字段
*/
public class Main {
public static void main(String[] args) {
Calendar rightnow= Calendar.getInstance();
int year=1990;
rightnow.set(year,2,1);
rightnow.add(Calendar.DATE,-1);
System.out.println(rightnow.get(Calendar.DATE));
}
}
原文:http://www.cnblogs.com/TIMHY/p/7775067.html