JavaSE|Date、DateFormat
Date
类 Date 表示特定的瞬间,精确到毫秒。
构造方法
1. public Date()
根据当前的默认毫秒值创建日期对象。
2. public Date(long date)
根据给定的毫秒值创建日期对象。
成员方法
1. public long getTime()
获取时间,以毫秒为单位。
类似System.currentTimeMillis()。
2. public void setTime(long time)
设置时间。
Date ↔ 毫秒值
- 从Date得到一个毫秒值
public long getTime() - 把一个毫秒值转换为Date
public void setTime(long time)
public Date(long date)
案例
你来到这个世界多少天了?
DateFormat
DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。
DateFormat 是抽象类,所以使用其子类SimpleDateFormat。
SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
SimpleDateFormat构造方法
1. public SimpleDateFormat()
用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
2. public SimpleDateFormat(String pattern)
用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
日期或时间元素 | 字母 |
---|---|
年 | y |
月 | M |
日 | d |
时 | H |
分 | m |
秒 | s |
SimpleDateFormat成员方法
1. public final String format(Date date)
将一个 Date 格式化为日期/时间字符串。
2. public Date parse(String source)
从给定字符串的开始解析文本,以生成一个日期。
// Date -- String
// 创建日期对象
Date d = new Date();
// 创建格式化对象
// SimpleDateFormat sdf = new SimpleDateFormat();
// 给定模式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
// public final String format(Date date)
String s = sdf.format(d);
System.out.println(s);
//String -- Date
String str = "2008-08-08 12:12:12";
//在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(str);
System.out.println(dd);
案例
制作了一个针对日期操作的工具类。
Calendar
Calendar 类是一个**抽象类,**它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
成员方法
1. public static Calendar getInstance()
Calendar 提供了一个类方法 getInstance,获得此类型的一个通用的对象。Calendar 的 getInstance 方法返回一个 Calendar 对象,其日历字段已由当前日期和时间初始化:
Calendar rightNow = Calendar.getInstance(); // 查看API可以知道,其返回子类对象
2. public int get(int field)
返回给定日历字段的值,日历中的每个日历字段都是静态变量,并且是int类型。
// 其日历字段已由当前日期和时间初始化:
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 + "日");
4. public void add(int field,int amount)
根据给定的日历字段和对应的时间,对当前日历进行操作。
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "-" + (month+1) + "-" + day); // 2018-10-24
c.add(Calendar.YEAR, 5);
c.add(Calendar.DAY_OF_MONTH, -10);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "-" + (month+1) + "-" + day); // 2023-10-14
5. public final void set(int year,int month,int date)
设置当前日历的年月日。
PS: month - 用来设置 MONTH 日历字段的值。Month 值是基于 0 的。例如,0 表示 January。取值范围0-11,超过11会对12取余。
c.set(2011, 11, 11);
// 获取年
year = c.get(Calendar.YEAR);
// 获取月
month = c.get(Calendar.MONTH);
// 获取日
date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
6. public long getTimeInMillis()
返回此 Calendar 的时间值,以毫秒为单位。
案例:获取任意一年的2月有多少天
System.out.println("请输入年份:");
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
Calendar c = Calendar.getInstance();
c.set(year, 2, 1);
c.add(Calendar.DATE, -1);
System.out.println(c.get(Calendar.DAY_OF_MONTH));