java入门-日期类

日期类

Date类

Date类表示特定的时间,可以精确到毫秒。

获取Date对象

Date date = new Date(); 

构造方法

/**
 * Allocates a <code>Date</code> object and initializes it so that
 * it represents the time at which it was allocated, measured to the
 * nearest millisecond.
 *
 * @see     java.lang.System#currentTimeMillis()
 */
public Date() {
    this(System.currentTimeMillis());
}
  public Date(long date) {
        fastTime = date;
    }

常用api

image-20240502085943443

返回从1970年1月1日0点0分0秒到现在的毫秒数

img

image-20240502090022185

img

Calendar类

Calendar是java util包下的一个工具类,提供了不同日期格式的日期处理。

创建日历对象

public static Calendar getInstance()
{
    return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
}

常用api

 Date getTime()

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

boolean after(Object when)

void add(int field, int amount)

int get(int field)

SimpleDateFormate

创建日期格式对象

img

img

img

(2) 常用api

n Date parse(String source) throws ParseException

n String format(Date date)

1.1.4 java8 日期类

(1) LocalDate类

获取当前日期。

img

Date和LocalDate的转换

img

(2) LocalTime类

获取当前时间

img

(3) DateTimeFormat类: 日期格式化类

img

img

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值