Date-java.util.Date
yyyy-MM-dd HH:mm:ss
2021-10-21 17:31:18
科普
CST可以为如下4个不同的时区的缩写:
美国中部时间:Central Standard Time (USA) UT-6:00
澳大利亚中部时间:Central Standard Time (Australia) UT+9:30
中国标准时间:China Standard Time UT+8:00
古巴标准时间:Cuba Standard Time UT-4:00
// 初始化 : 本质是 CST
// 中国是 : Thu Jan 01 08:00:00 CST 1970
Date date0 = new Date(0);
// Fri Oct 21 17:35:00 CST 2022 : 本质是 UTC + 8:00
Date date1 = new Date();
简单使用
// get 方法 : 时间毫米值 1970-1-1 00:00:00 : 和时区无瓜
Date date0 = new Date(0);
date0.getTime();
// set 方法 : 设置从 1970-1-1 00:00:00 开始 : 和时区无瓜
date0.setTime(0);
SimpleDateFormat
主要功能
1.将 Date 对象 -->> 标准格式的字符串
2.将标准格式的字符串 -->> Date 对象
// a.初始化标准格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// b.2022-10-21 17:56:48 (CST-China)
System.out.println(format.format(new Date()));
// c.Sat Jan 01 00:00:00 CST 2022
Date date = format.parse("2022-1-1 00:00:00");
System.out.println(date);
Calendar 日历 (abstract class)
日历对象
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kRE9yL6J-1666374517887)(E:/500-Local_figure_bed/%E6%97%B6%E9%97%B4%E7%B1%BB/Calendar.png)]
// 获得实例对象
Calendar instance = Calendar.getInstance();
// 增加字段的值
instance.add(Calendar.YEAR, 66);
// 获得某个字段的值
System.out.println(instance.get(Calendar.YEAR));
// 修改某个字段的值
instance.set(Calendar.YEAR, 2022);
// 拿到日期对象 java.until.Date (CST)
System.out.println(instance.getTime());
// 拿到时间毫秒值 1970-1-1 00:00:00 : 和时区无关
System.out.println(instance.getTimeInMillis());
经典时间 api
和日历Api一样喜欢用静态方法产生实例对象
LocalDate
// 通过静态工厂获得实例对象
// 2022-10-21 (CST china standard time)
LocalDate now1 = LocalDate.now();
// 1999-03-13 : 设定年月日
LocalDate now2 = LocalDate.of(1999, 01, 01);
// 2022-10-21 : 1999-03-13
System.out.println(now1 + " : " + now2);
// 2022 : 获得当前年份 (CST)
System.out.println(now1.getYear());
// OCTOBER : 十月 : 当前月份 : (CST)
System.out.println(now1.getMonth());
// 当月第几天 : 21 号 (CST)
System.out.println(now1.getDayOfMonth());
// 星期几 : FRIDAY : 星期五
System.out.println(now1.getDayOfWeek());
// 按天算 : 这是今年的第几天
System.out.println(now1.getDayOfYear());
LocalTime : 注意本质区别和 Date
LocalTime 时分秒毫秒
// 00:04:45.260 : hour : minute : second : millisecond
LocalTime now1 = LocalTime.now();
// 01:01:01.001 : 第 4 个参数是纳秒 10^-9 次方秒 1000000 六个零是一毫秒
LocalTime now2 = LocalTime.of(1, 1, 1, 1000000);
// 获得小时数 : 就是几点钟
System.out.println(now1.getHour());
// 获得分钟数 : 就是几分钟
System.out.println(now1.getMinute());
// 获得秒数 : 就是过几秒
System.out.println(now1.getSecond());
// 或得纳秒数 : 过了几个纳秒
System.out.println(now1.getNano());
LocalDateTime
包含了日期和时间
// year : month : day : hour : minus : second : millisecond
// 2022-10-22 00:14:50.202 : CST
LocalDateTime now1 = LocalDateTime.now();
// 1970-1-1 00:00:00:00
LocalDateTime now2 = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0);
Instant
零时区的时间
// 这个特殊一点 : 本质也是 LocalDateTime
// UTC + 00:00 在零时区
Instant now = Instant.now();
DateTimeFormatter
// 获得格式化对象
DateTimeFormatter format = DateTimeFormatter
.ofPattern("yyyy:MM:dd HH:mm:ss");
// 获得时期对象
LocalDateTime now = LocalDateTime.now();
// 获得格式对象
DateTimeFormatter format = DateTimeFormatter
.ofPattern("yyyy:MM:dd HH:mm:ss");
// 通过格式对象的 .format 方法获得标准字符串
System.out.println(format.format(now));
// 通过时期对象的 .format 方法获得标准字符串
System.out.println(now.format(format));
功能方法
LocalDateTime now1 = LocalDateTime.now();
LocalDateTime now2 = LocalDateTime.of(1970, 1, 1, 00, 00, 00, 00);
// 判断 now2 是否在 now1 之前
System.out.println(now2.isBefore(now1));
// 提取 : month : day
System.out.println(MonthDay.from(now1));
// 减少当前时点一天
System.out.println(now1.minusDays(1));
// 减少当前时点一个月
System.out.println(now1.minusMonths(1));
// 减少当前时点一年
System.out.println(now1.minusYears(1));
// 日期 : LocalDate
LocalDate now1 = LocalDate.now();
LocalDate now2 = LocalDate.of(1970, 1, 1);
// 只能是 LocalDate 之间比较
// now1 - now2
Period between = Period.between(now2, now1);
// P52Y9M21D
System.out.println(between);
// 单独提取相差年份字段
System.out.println(between.getYears());
// 单独提取相差月份字段
System.out.println(between.getMonths());
// 单独提取相差时间对应的天字段
System.out.println(between.getDays());
// 可以比较 LocalDateTime LocalTime LocalDate
LocalDateTime now1 = LocalDateTime.of(1970,1, 1, 00, 00, 00);
LocalDateTime now2 = LocalDateTime.of(1971, 1, 1, 00, 00, 00);
Duration between1 = Duration.between(now1, now2);
// 计算相差的天数 : 365 : 颗粒度 : day
System.out.println(between1.toDays());
// 计算相差的天数 : 8760
System.out.println(between1.toHours());
ChronoUnit :
// LocalDateTime LocalTime LocalDate 都可以
LocalDateTime birthday = LocalDateTime.of(1999, 03, 13, 0, 0 ,0);
LocalDateTime nowTime = LocalDateTime.of(2000, 04, 13, 0, 0 ,0);
// 计算时期的差值 : 颗粒度为 : YEARS
System.out.println(ChronoUnit.YEARS.between(birthday, nowTime));
// 计算时期的差值 : 颗粒度为 : MONTHS
System.out.println(ChronoUnit.MONTHS.between(birthday, nowTime));
// 计算时期的差值 : 颗粒度为 : DAYS
System.out.println(ChronoUnit.DAYS.between(birthday, nowTime));
// 计算时期的差值 : 颗粒度为 : HOURS
System.out.println(ChronoUnit.HOURS.between(birthday, nowTime));
// 计算时期的差值 : 颗粒度为 : MINUTES
System.out.println(ChronoUnit.MINUTES.between(birthday, nowTime));
// 计算时期的差值 : 颗粒度为 : SECONDS
System.out.println(ChronoUnit.SECONDS.between(birthday, nowTime));
// 计算时期的差值 : 颗粒度为 : NANOS
System.out.println(ChronoUnit.NANOS.between(birthday, nowTime));