关于Java中的时间类。

今天我们来分享Java中那些表示时间的类~

Date类

Date类的概述

类 Date 表示特定的瞬间,精确到毫秒。 

构造方法

public Date()

获取当前的时间

public Date(long date)

如果参数为零则时间为1970年1月1号

成员方法

public long getTime()

public void setTime(long time)

SimpleDateFormat类

DateFormat类的概述(SimpleDateFormat类的父类)

DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat( SimpleDateFormat 类可以实现日期和字符串的相互转换)

SimpleDateFormat构造方法

public SimpleDateFormat()

public SimpleDateFormat(String pattern)

public Date parse(String source)

把字符串转换成对象~

Calendar类

Calendar类的概述

Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

成员方法

public static Calendar getInstance()

public int get(int field)

直接输出c会打印出很多东西看起来不方便

我们可以用get方法来获取我们想要的内容

public void add(int field,int amount) 

对指定字段进行增加或减少的操作

public final void set(int year,int month,int date) 

修改指定字段

猜你喜欢

转载自blog.csdn.net/DLQFHL/article/details/95179243