目标:Date类的有参构造器的使用。
构造器:
----- public Date(): 创建当前系统的此刻日期时间对象。
----- public Date(long time): 把时间毫秒值转换成日期对象。
流程:
Date日期对象 -> getTime() -> 时间毫秒值
时间毫秒值 -> new Date(时间毫秒值) -> Date日期对象
小结public Date(long time): 把时间毫秒值转换成日期对象。
案例:
package com.ithei.DateTimeDemo; import java.util.Date; /** * @program: javaDemo01->Demo02Time * @description: Date有参构造器 * @author: 安生 * @create: 2021-01-22 13:48 **/ public class Demo02Time { public static void main(String[] args) { //需求 求121秒后的此刻时间 //第一步创建当前日期对象 Date d = new Date(); System.out.println("now: " + d); //第二步 通过getTime()把当前对象转换成当前毫秒值 1s = 1000毫秒 long d1 = d.getTime()+121*1000; //第三步 通过Date(时间毫秒值)有参构造器 再转换成日期对象 Date d2 = new Date(d1); System.out.println("future: " + d2); } }
结果: 121秒 等于 2分钟+1秒
now : Fri Jan 22 13:56:10 CST 2021
future : Fri Jan 22 13:58:11 CST 2021