Date日期类
代表日期
构造
Date()
获取当前系统时间(无参构造)
Date(long time)
获取指定时间,传递毫秒值,从时间原点开始算(有参构造)
方法
void setTime(long time)
-> 设置时间,传递毫秒值-> 从时间原点开始算
long getTime()
->获取时间,返回毫秒值
private static void date01() {
//Date() -> 获取当前系统时间
Date date1 = new Date();
System.out.println("date1 = " + date1);
//Date(long time) -> 获取指定时间,传递毫秒值 -> 从时间原点开始算
Date date2 = new Date(1000L);
System.out.println("date2 = " + date2);
}
private static void date02() {
Date date = new Date();
//1.void setTime(long time) -> 设置时间,传递毫秒值-> 从时间原点开始算
date.setTime(1000L);
//2.long getTime()->获取时间,返回毫秒值
System.out.println(date.getTime());
}
Calendar日历类
获取
static Calendar getInstance()
方法
int get(int field)
->返回给定日历字段的值
void set(int field, int value)
:将给定的日历字段设置为指定的值
void add(int field, int amount)
:根据日历的规则,为给定的日历字段添加或者减去指定的时间量
Date getTime()
:将Calendar转成Date对象
field:代表的是日历字段-> 年 月 日 星期等,都是静态的
private static void calendar02() {
Calendar calendar = Calendar.getInstance();//多态
//int get(int field) ->返回给定日历字段的值
int year = calendar.get(Calendar.YEAR);
System.out.println("year = " + year);
//void set(int field, int value) :将给定的日历字段设置为指定的值
//calendar.set(Calendar.YEAR,2028);
//System.out.println(calendar.get(Calendar.YEAR));
//void add(int field, int amount) :根据日历的规则,为给定的日历字段添加或者减去指定的时间量
calendar.add(Calendar.YEAR,-1);
System.out.println(calendar.get(Calendar.YEAR));
//Date getTime():将Calendar转成Date对象
Date date = calendar.getTime();
System.out.println("date = " + date);
}
扩展方法:
void set(int year, int month, int date)
-> 直接设置年月日
需求:键盘录入一个年份,判断这一年是闰年,还是平年
步骤:
1.创建Calendar对象
2.创建Scanner对象,键盘录入一个年份
3.调用set方法,传递年,月,日
set(年,2,1) -> 国外是0-11,所以设置成2月就是代表3月
4.将day减1天(3月1日减1天,就是2月最后一天,知道2月最后一天了,就知道是平年还是闰年了)
5.获取day判断平年还是闰年,输出结果
private static void calendar03() {
//1.创建Calendar对象
Calendar calendar = Calendar.getInstance();
//2.创建Scanner对象,键盘录入一个年份
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
//3.调用set方法,传递年,月,日
//set(年,2,1) -> 国外是0-11,所以设置成2月就是代表3月
calendar.set(year,2,1);
//4.将day减1天(3月1日减1天,就是2月最后一天,知道2月最后一天了,就知道是平年还是闰年了)
calendar.add(Calendar.DATE,-1);
int day = calendar.get(Calendar.DATE);
//5.获取day判断平年还是闰年,输出结果
if (day==29){
System.out.println("闰年");
}else{
System.out.println("平年");
}
}
SimpleDateFormat日期格式化类
将日期按照指定格式格式化
构造
SimpleDateFormat(String pattern)
pattern : yyyy-MM-dd HH:mm:ss
方法
String format(Date date)
-> 将Date对象按照指定的格式转成String
Date parse(String source)
-> 将符合日期格式的字符串转成Date对象
public class Demo03SimpleDateFormat {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//1.String format(Date date) -> 将Date对象按照指定的格式转成String
String time1 = sdf.format(new Date());
System.out.println("time1 = " + time1);
//2.Date parse(String source)-> 将符合日期格式的字符串转成Date对象
String time2 = "2000-10-10 10:10:10";
Date date = sdf.parse(time2);
System.out.println("date = " + date);
}
}
jdk8新日期
1. LocalDate 本地日期
1.1.获取LocalDate对象
1.概述:LocalDate是一个不可变的日期时间对象,表示日期,通常被视为年月日
2.获取:
static LocalDate now() ->
创建LocalDate对象
static LocalDate of(int year, int month, int dayOfMonth)
-> 创建LocalDate对象,设置年月日
public class Demo04LocalDate {
public static void main(String[] args) {
//static LocalDate now() -> 创建LocalDate对象
LocalDate localDate = LocalDate.now();
System.out.println("localDate = " + localDate);
//static LocalDate of(int year, int month, int dayOfMonth) -> 创建LocalDate对象,设置年月日
LocalDate localDate1 = LocalDate.of(2000, 10, 10);
System.out.println("localDate1 = " + localDate1);
}
}
1.2.LocalDateTime对象
1.LocalDateTime概述:LocalDateTime是一个不可变的日期时间对象,代表日期时间,通常被视为年 - 月 - 日 - 时 - 分 - 秒。
2.获取:
static LocalDateTime now()
创建LocalDateTime对象
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second)
创建LocalDateTime对象,设置年月日时分秒
1.3.获取日期字段的方法 : 名字是get开头
int getYear()
->获取年份
int getMonthValue()
->获取月份
int getDayOfMonth()
->获取月中的第几天
private static void get() {
LocalDate localDate = LocalDate.now();
//int getYear()->获取年份
System.out.println(localDate.getYear());
//int getMonthValue()->获取月份
System.out.println(localDate.getMonthValue());
//int getDayOfMonth()->获取月中的第几天
System.out.println(localDate.getDayOfMonth());
}
1.4.设置日期字段的方法 : 名字是with开头
LocalDate withYear(int year)
:设置年份
LocalDate withMonth(int month)
:设置月份
LocalDate withDayOfMonth(int day)
:设置月中的天数
private static void with() {
LocalDate localDate = LocalDate.now();
//LocalDate withYear(int year):设置年份
//LocalDate localDate1 = localDate.withYear(2000);
//System.out.println(localDate1);
//LocalDate withMonth(int month):设置月份
//LocalDate localDate2 = localDate1.withMonth(10);
//System.out.println("localDate2 = " + localDate2);
//LocalDate withDayOfMonth(int day):设置月中的天数
//LocalDate localDate3 = localDate2.withDayOfMonth(10);
//System.out.println("localDate3 = " + localDate3);
LocalDate localDate1 = localDate.withYear(2000).withMonth(10).withDayOfMonth(10);
System.out.println("localDate1 = " + localDate1);
}
1.5.日期字段偏移
设置日期字段的偏移量,方法名plus开头,向后偏移
设置日期字段的偏移量,方法名minus开头,向前偏移
private static void plusAndMinus() {
LocalDate localDate = LocalDate.now();
// LocalDate localDate1 = localDate.plusYears(1L);
// System.out.println("localDate1 = " + localDate1);
LocalDate localDate1 = localDate.minusYears(1L);
System.out.println("localDate1 = " + localDate1);
}
2.Period和Duration类
2.1 Period 计算日期之间的偏差
方法
static Period between(LocalDate d1,LocalDate d2)
:计算两个日期之间的差值
getYears()
->获取相差的年
getMonths()
->获取相差的月
getDays()
->获取相差的天
private static void period() {
LocalDate local1 = LocalDate.of(2022, 12, 12);
LocalDate local2 = LocalDate.of(2021, 11, 11);
Period period = Period.between(local2, local1);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
}
2.2 Duration计算时间之间的偏差
1.static Duration between(Temporal startInclusive, Temporal endExclusive)
-> 计算时间差
2.Temporal : 是一个接口
实现类:LocalDate LocalDateTime
3.参数需要传递 Temporal 的实现类对象, 注意要传递LocalDateTime
因为Duration计算精确时间偏差,所以需要传递能操作精确时间的 LocalDateTime
4.利用Dutation获取相差的时分秒 -> to开头
toDays()
:获取相差天数
toHours()
: 获取相差小时
toMinutes()
:获取相差分钟
toMillis()
:获取相差秒(毫秒)
private static void duration() {
LocalDateTime local1 = LocalDateTime.of(2022, 12, 12,12,12,12);
LocalDateTime local2 = LocalDateTime.of(2021, 11, 11,11,11,11);
Duration duration = Duration.between(local2, local1);
System.out.println(duration.toDays());
System.out.println(duration.toHours());
System.out.println(duration.toMinutes());
System.out.println(duration.toMillis());
}
如果计算年月日 ,就用Period
如果计算时分秒,就用Duration
DateTimeFormatter日期格式化类
1.获取:
static DateTimeFormatter ofPattern(String pattern)
-> 获取对象,指定格式
2.方法:
String format(TemporalAccessor temporal)
-> 将日期对象按照指定的规则转成String
TemporalAccessor
:接口,子接口有Temporal
Temporal
的实现类:LocalDate LocalDateTime
TemporalAccessor parse(CharSequence text)
-> 将符合规则的字符串转成日期对象
如果想将TemporalAccessor转成我们常见的LocalDateTime日期对象,就需要用到LocalDateTime中的静态方法:static LocalDateTime from(TemporalAccessor temporal)
private static void parse() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String time = "2000-10-10 10:10:10";
TemporalAccessor temporalAccessor = dtf.parse(time);
//System.out.println(temporalAccessor);
LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
System.out.println("localDateTime = " + localDateTime);
}