package com.example.demo;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
public class Demo {
public static void main(String[] args) {
/**
* Java8新增的所有日期类都在包java.time下,而且都是final class和线程安全。
* PS:Java8以前的日期类Date、Calendar、SimpleDateFormat等都存在线程安全问题。
*
* LocalDateTime:日期+时间 yyyy-MM-dd HH:mm:ss,替代Calendar
* LocalDate:日期,yyyy-MM-dd
* LocalTime:时间,HH:mm:ss
*
* YearMonth:年月,比如信用卡的日期类型
* MonthDay:月日,比如生日
* DateTimeFormatter:日期格式化类,替代SimpleDateFormat
* Instant:替代Date
* java8中提供三个类(LocalDate,LocalTime,LocalDateTime),
* 它们分别对年月日、时分秒和年月日时分秒进行单独的处理,
* 使用时可以根据自己的实际情况进行选择,它们的API类似
*/
/* LocalDateTime localDateTime=LocalDateTime.now();
System.out.println(localDateTime);//2020-09-28T14:51:14.231
LocalTime localTime=LocalTime.now();
System.out.println(localTime);//14:51:14.232
LocalDate localDate=LocalDate.now();
System.out.println(localDate);//2020-09-28*/
//还有很多枚举类,月份枚举Month、星期枚举DayOfWeek、Year类(可以判断闰年)等等。
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//todo LocalDateTime类 ,,,LocalDateTime:日期+时间 yyyy-MM-dd HH:mm:ss,替代Calendar
// LocalDateTime localDateTime2 = LocalDateTime.parse("2018-01-01 17:30:30", pattern);
// System.out.println("当天是周几: " + localDateTime2.getDayOfWeek());
// System.out.println("ISO_LOCAL_DATE_TIME: " + localDateTime2.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
// System.out.println("判断日期大小: " + localDateTime2.isBefore(LocalDateTime.now()));
// System.out.println("判断日期大小: " + localDateTime2.isEqual(LocalDateTime.now()));
//todo LocalDate类 ,,,,,,,,,LocalDate:日期,yyyy-MM-dd
// LocalDate localDate = LocalDate.parse("2018-06-07 17:30:30", pattern);
/* LocalDate localDate = LocalDate.now();
System.out.println("localDate: " + localDate);
System.out.println("ISO_LOCAL_DATE: " + localDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
//当月第一天:2020-09-01
System.out.println("当月第一天:" + LocalDate.now().withDayOfMonth(1));
//当前年2月最后一天: 2020-02-29
System.out.println("当前年2月最后一天: " + LocalDate.now().withMonth(2).with(TemporalAdjusters.lastDayOfMonth()));
//下周的日期两个方式,日、月、年类似
//下周开始日期: 2020-10-05
System.out.println("下周日期: " + localDate.plus(1, ChronoUnit.WEEKS));
//下周开始日期: 2020-10-05
System.out.println("下周日期: " + localDate.plusWeeks(1));
//下下周开始日期: 2020-10-12
System.out.println("下下周日期: " + localDate.plusWeeks(2));
//2020年10月第一个星期天: 2020-10-04
System.out.println("2020年10月第一个星期天: " + LocalDate.of(2020, 10, 1)
.with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)));*/
//TODO LocalTime类
//01:01:01
// System.out.println("LocalTime: " + LocalTime.of(1, 1, 1));
//TODO **之差只要改变后面的枚举类型就行
/* LocalDate startDate = LocalDate.parse("2020-09-01");
LocalDate endDate = LocalDate.parse("2020-09-30");
//周数差: 4
System.out.println("周数差: " + startDate.until(endDate, ChronoUnit.WEEKS));
//天数差: 29
System.out.println("天数差: " + startDate.until(endDate, ChronoUnit.DAYS));
*/
//TODO 本年第几周
//WeekFields.ISO代表每周从周一开始算。
//WeekFields.SUNDAY_START代表每周从周日开始算。
TemporalField weekFields = WeekFields.ISO.weekOfMonth();
// TemporalField weekFields = WeekFields.SUNDAY_START.weekOfMonth();
//注意
//if the 1st day of the year is a Monday, week one starts on the 1st and there is no week zero
//if the 2nd day of the year is a Monday, week one starts on the 2nd and the 1st is in week zero
//if the 4th day of the year is a Monday, week one starts on the 4th and the 1st to 3rd is in week zero
//if the 5th day of the year is a Monday, week two starts on the 5th and the 1st to 4th is in week one
//就是1月第一周天数<=3(周一是星期起始天),那么这些天都是第0周。
//第几周:5
// System.out.println("第几周:" + LocalDate.of(2020, 9, 28).get(weekFields));
//TODO 指定时间之后或之前XXX的时间
/* LocalDateTime now = LocalDateTime.now();
System.out.println("当前时间" + now);
LocalDateTime localDateTime = now.minusDays(2);
//当前时间之前2天:2020-09-26 15:34:48
//当前时间之前2天:2020-09-26
//当前时间之前2天:15:35:33
System.out.println("当前时间之前2天:" + localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println("当前时间之后2天:" + now.plusDays(2));
System.out.println("当前时间之前2周:" + now.minusWeeks(2));
System.out.println("当前时间之后2周:" + now.plusWeeks(2));
System.out.println("当前时间之前2月:" + now.minusMonths(2));
System.out.println("当前时间之后2月:" + now.plusMonths(2));
System.out.println("当前时间之前2年:" + now.minusYears(2));
System.out.println("当前时间之后2年:" + now.plusYears(2));
System.out.println("当前时间之前2小时:" + now.minusHours(2));
System.out.println("当前时间之后2小时:" + now.plusHours(2));
System.out.println("当前时间之前2分钟:" + now.minusMinutes(2));
System.out.println("当前时间之后2分钟:" + now.plusMinutes(2));
System.out.println("当前时间之前2秒:" + now.minusSeconds(2));
System.out.println("当前时间之后2秒:" + now.plusSeconds(2));*/
//TODO 比较时间先后
/* LocalDateTime now = LocalDateTime.now();
System.out.println("当前时间:"+now);
LocalDateTime appointTime = LocalDateTime.of(2019, 1, 17, 9, 32, 12);
System.out.println("指定时间:"+appointTime);
System.out.println(now.isAfter(appointTime));
System.out.println(now.isBefore(appointTime));*/
//TODO 格式化时间格式及按照一定格式解析
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("HH:mm:ss");
//格式化
String format = now.format(formatter);
String format2 = now.format(formatter2);
String format3 = now.format(formatter3);
System.out.println("格式化1:" + format);
System.out.println("格式化2:" + format2);
System.out.println("格式化3:" + format3);
//解析
LocalDateTime localDateTime = LocalDateTime.parse(format, formatter);
LocalDate localDate = LocalDate.parse(format2, formatter2);
LocalTime localTime = LocalTime.parse(format3, formatter3);
System.out.println("解析1:" + localDateTime);
System.out.println("解析2:" + localDate);
System.out.println("解析3:" + localTime);
}
}
JAVA8时间类
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...