Date:时间点
LocalDate:日期
Date :
Date today =new Date();
System.out.println(today); //输出的是当前时间点(年月日具体时刻)
{略:Date(年月日):年是从1900开始多少年,月份减一,日期直接插入}
LocalDate:
创建对象不用构造器,用静态工厂方法(factory method)
import java.util.*;
import java.time.*;
public class DateDemo {
public static void main(String[] args){
//Date
Date today=new Date();//Date类创建对象
System.out.println("today is "+today);
//
//LocalDate
//LocalDate 用静态工厂方法(factory method)创建对象
LocalDate date=LocalDate.now();
System.out.println("date is "+date);
//自定义数据
LocalDate today1=LocalDate.now();
today1=LocalDate.of(2001,12,5);//构建一个特定日期
System.out.println("today1 is "+today1);
//LocalDate获取年月日
int year=date.getYear();
int month=date.getMonthValue();
int day=date.getDayOfMonth();
System.out.println(year+month+day);
//打印一个距离现在日期多少天的新日期
LocalDate NewDay=date.plusDays(1000);
System.out.println("After 1000 days later is "+NewDay);
}
}
总结:
1.LocalDate 类封装了实例域来维护所设置的日期,不查源码就不知道其内部的代码和日期表示,。及封装的意义在于类对外提供的方法。(例子的重点说明)
2.关于LocalDate :创建对象—用静态工厂的方法:LocalDate.now();
LocalDate类的相关方法:取相应的年月日;getYear(); getMonthValue(); getDayOfMonth();
创建一个距离已知对象(日期)n天的新对象(日期):plusDays()方法——————LocalDate NewDay=data.plusDays(n);//date为已存在的对象(日期)