LocalDateTime 是 Java8 中提供的 日期+时间的对象,
日期 包含 年、月、日 信息
时间包含 小时、分钟、秒、纳秒 信息
此对象默认使用系统时区,例如中国使用 【东八区】
* 1.创建日期时间对象
* 1.1 获取当前日期时间对象
* LocalTime.now() : 获取默认时区下的系统时钟的时间
* LocalTime.now(Clock clock) : 获取指定时钟的时间
* LocalTime.now(ZoneId zone) : 获取指定时区的、默认系统时钟的时间
* 【补充:获取所有时区信息的方式 : ZoneId.getAvailableZoneIds()】 Asia/Shanghai
* 1.2 获取指定日期时间对象
* LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute) : 指定 年、月(枚举值)、日、小时、分钟
* LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute, int second) : 指定 年、月(枚举值)、日、小时、分钟、秒
* LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) : 指定 年、月(枚举值)、日、小时、分钟、秒、纳秒
* LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute) : 指定 年、月(数值)、日、小时、分钟
* LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second) : 指定 年、月(数值)、日、小时、分钟、秒
* LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) : 指定 年、月(数值)、日、小时、分钟、秒、纳秒
* LocalDateTime.of(LocalDate date, LocalTime time) : 指定 日期对象 和 时间对象 两个对象
* LocalDateTime ofInstant(Instant instant, ZoneId zone) : 指定时间戳对象 Instant 和 时区
* LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) : 指定 距离 1970-01-01 00:00:00 的秒数、纳秒数、偏移数,创建LocalDateTime对象
*
代码
package com.northcastle.K_Date;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class LocalDateTimeTest {
public static void main(String[] args) {
//1.1 获取当前日期
LocalDateTime now01 = LocalDateTime.now();
System.out.println("now01 = " + now01);
LocalDateTime now02 = LocalDateTime.now(Clock.systemUTC());// UTC/GMT 标准时区时间,零时区/中时区
System.out.println("now02 = " + now02);
LocalDateTime now03 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println("now03 = " + now03);
System.out.println("============================");
//1.2 获取指定日期
LocalDateTime localDateTime01 = LocalDateTime.of(2022, Month.MARCH, 20, 20, 46);
System.out.println("localDateTime01 = " + localDateTime01);
LocalDateTime localDateTime02 = LocalDateTime.of(2022, Month.MARCH, 20, 20, 47,20);
System.out.println("localDateTime02 = " + localDateTime02);
LocalDateTime localDateTime03 = LocalDateTime.of(2022, Month.MARCH, 20, 20, 47,30,120);
System.out.println("localDateTime03 = " + localDateTime03);
LocalDateTime localDateTime04 = LocalDateTime.of(2022, 3, 20, 20, 46);
System.out.println("localDateTime04 = " + localDateTime04);
LocalDateTime localDateTime05 = LocalDateTime.of(2022, 3, 20, 20, 47,20);
System.out.println("localDateTime05 = " + localDateTime05);
LocalDateTime localDateTime06 = LocalDateTime.of(2022, 3, 20, 20, 47,30,120);
System.out.println("localDateTime06 = " + localDateTime06);
LocalDate localDate = LocalDate.of(2022, 3, 20);
LocalTime localTime = LocalTime.of(20, 48, 50);
LocalDateTime localDateTime07 = LocalDateTime.of(localDate, localTime);
System.out.println("localDateTime07 = " + localDateTime07);
Instant instant08 = Instant.ofEpochSecond(100); // 1970-01-01 00:01:40
ZoneId zoneId08 = ZoneId.systemDefault(); // 我们中国这儿的 默认时区 是 Asia/Shanghai,东八区,多8个小时
LocalDateTime localDateTime08 = LocalDateTime.ofInstant(instant08, zoneId08); // 默认时区
System.out.println("localDateTime08 = " + localDateTime08); // 所以这里是 1970-01-01 08:01:40
//ZoneOffset zoneOffset09 = ZoneOffset.of("+8"); // +8小时
//ZoneOffset zoneOffset09 = ZoneOffset.UTC; // 标准时区 零时区的时间
//ZoneOffset zoneOffset09 = ZoneOffset.MAX; // +18 最大偏移
//ZoneOffset zoneOffset09 = ZoneOffset.MIN; // -18 最小偏移
//ZoneOffset zoneOffset09 = ZoneOffset.ofHours(2); // 增加两个小时
ZoneOffset zoneOffset09 = ZoneOffset.ofHoursMinutes(0,10); // 增加0小时10分钟
LocalDateTime localDateTime09 = LocalDateTime.ofEpochSecond(100, 0,zoneOffset09);
System.out.println("localDateTime09 = " + localDateTime09);
System.out.println("============================");
// 获取时区的方法 : Asia/Shanghai
// Set asia = ZoneId.getAvailableZoneIds()
// .stream()
// .filter(s -> s.startsWith("Asia"))
// .collect(Collectors.toSet());
// System.out.println(asia);
}
}
结果
* 2.获取日期时间对象的信息
* toLocalDate() : 返回一个LocalDate 对象
* toLocalTime() : 返回一个LocalTime 对象
*
* getYear() : 获取年分信息
* getMonth() : 获取月份信息(枚举类型)
* getMonthValue() : 获取月份的数字(数值类型)
* getDayOfMonth() : 获取日期信息
* getDayOfWeek() : 获取星期几 (枚举类型)
* getDayOfYear() : 获取这一年的第几天
*
* getHour() : 获取小时信息
* getMinute() : 获取分钟信息
* getSecond() : 获取秒
* getNano() : 获取纳秒
*
代码
package com.northcastle.K_Date;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class LocalDateTimeTest {
public static void main(String[] args) {
//2.获取日期对象的年月日、时分秒信息
LocalDateTime today = LocalDateTime.now();
System.out.println("today = " + today);
System.out.println("today.toLocalDate() = " + today.toLocalDate());
System.out.println("today.toLocalTime() = " + today.toLocalTime());
System.out.println("年分 : " + today.getYear());
System.out.println("月份 : " + today.getMonth() + "月份数字 : "+today.getMonthValue());
System.out.println("日期 : " + today.getDayOfMonth());
System.out.println("星期 : " + today.getDayOfWeek());
System.out.println("这一年的第多少天 : " + today.getDayOfYear());
System.out.println("小时 : " + today.getHour());
System.out.println("分钟 : " + today.getMinute());
System.out.println("秒 : " + today.getSecond());
System.out.println("纳秒 : " + today.getNano());
System.out.println("============================");
}
}
结果
* 3.指定日期时间对象的 年、月、日、时、分、秒、纳秒
* withYear(int) : 指定年分
* withMonth(int) : 指定月份
* withDayOfMonth(int) : 指定日期
* withDayOfYear(int) : 指定一年中的多少天
* withHour(int) : 指定小时
* withMinute(int) : 指定分钟
* withSecond(int) : 指定秒
* withNano(int) : 指定纳秒
*
* with(TemporalAdjuster) : 时间矫正器
*
代码
package com.northcastle.K_Date;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class LocalDateTimeTest {
public static void main(String[] args) {
//3.修改指定 年月日时分秒纳秒
LocalDateTime nowWith = LocalDateTime.now();
System.out.println("nowWith = " + nowWith);
System.out.println("指定年分2025 = " + nowWith.withYear(2025));
System.out.println("指定月份6月 = " + nowWith.withMonth(6));
System.out.println("指定日期28号 = " + nowWith.withDayOfMonth(28));
System.out.println("指定一年的第3天 = " + nowWith.withDayOfYear(3));
System.out.println();
System.out.println("指定小时6点 = " + nowWith.withHour(6));
System.out.println("指定分钟20分 = " + nowWith.withMinute(20));
System.out.println("指定秒20秒 = " + nowWith.withSecond(20));
System.out.println("指定纳秒 123000000 = " + nowWith.withNano(123000000));
System.out.println();
//时间矫正器
TemporalAdjuster temporalAdjuster = (temporal) ->{
System.out.println("temporal = " + temporal);
LocalDateTime localDateTime = (LocalDateTime) temporal;
localDateTime = localDateTime.withYear(2023)
.withMonth(6)
.withDayOfMonth(1);
System.out.println("localDateTime = " + localDateTime);
return localDateTime;
};
LocalDateTime withTemporalAdjuster01 = nowWith.with(temporalAdjuster);
System.out.println("withTemporalAdjuster01 = " + withTemporalAdjuster01);
LocalDateTime withTemporalAdjuster02 = nowWith.with(TemporalAdjusters.firstDayOfNextMonth()); // 下个月的第一天
System.out.println("withTemporalAdjuster02 = " + withTemporalAdjuster02);
LocalDateTime withTemporalAdjuster03 = nowWith.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.MONDAY)); // 这个月的第2个星期一
System.out.println("withTemporalAdjuster03 = " + withTemporalAdjuster03);
LocalDateTime withTemporalAdjuster04 = nowWith.with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)); // 这个月的第一个星期天
System.out.println("withTemporalAdjuster04 = " + withTemporalAdjuster04);
LocalDateTime withTemporalAdjuster05 = nowWith.with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY)); // 这个月的最后一个星期一
System.out.println("withTemporalAdjuster05 = " + withTemporalAdjuster05);
LocalDateTime withTemporalAdjuster06 = nowWith.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)); // 下一个星期天
System.out.println("withTemporalAdjuster06 = " + withTemporalAdjuster06);
LocalDateTime withTemporalAdjuster07 = nowWith.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); // 下一个或者当前的星期天
System.out.println("withTemporalAdjuster07 = " + withTemporalAdjuster07);
LocalDateTime withTemporalAdjuster08 = nowWith.with(TemporalAdjusters.previous(DayOfWeek.THURSDAY)); // 上一个星期天
System.out.println("withTemporalAdjuster08 = " + withTemporalAdjuster08);
LocalDateTime withTemporalAdjuster09 = nowWith.with(TemporalAdjusters.previousOrSame(DayOfWeek.THURSDAY)); // 上一个或者当前的星期天
System.out.println("withTemporalAdjuster09 = " + withTemporalAdjuster09);
System.out.println("============================");
}
}
结果
* 4.加上 或者 减去 时、分、秒、纳秒
* plusYears(long) : 加几年
* plusMonths(long) : 加几个月
* plusDays(long) : 加几天
* plusWeeks(long) : 加几个周
* plusHours(long) : 加几个小时
* plusMinutes(long) : 加几分钟
* plusSeconds(long) : 加几秒
* plusNanos(long) : 加几个纳秒
*
* minusYears(long) : 减几年
* minusMonths(long) : 减几个月
* minusDays(long) : 减几天
* minusWeeks(long) : 减几个周
* minusHours(long) : 减几个小时
* minusMinutes(long) : 减几分钟
* minusSeconds(long) : 减几秒
* minusNanos(long) : 减几个纳秒
*
代码
package com.northcastle.K_Date;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class LocalDateTimeTest {
public static void main(String[] args) {
//4.加上 或者 减去 年、月、日、时、分、秒、纳秒
LocalDateTime nowPlusOrMin = LocalDateTime.now();
System.out.println("nowPlusOrMin = " + nowPlusOrMin);
System.out.println("加两年 = " + nowPlusOrMin.plusYears(2));
System.out.println("加2个月 = " + nowPlusOrMin.plusMonths(2));
System.out.println("加12天 = " + nowPlusOrMin.plusDays(12));
System.out.println("加1个周 = " + nowPlusOrMin.plusWeeks(1));
System.out.println("加2个小时 = " + nowPlusOrMin.plusHours(2));
System.out.println("加20分钟 = " + nowPlusOrMin.plusMinutes(20));
System.out.println("加20秒 = " + nowPlusOrMin.plusSeconds(20));
System.out.println("加1纳秒 = " + nowPlusOrMin.plusNanos(1));
System.out.println();
System.out.println("减两年 = " + nowPlusOrMin.minusYears(2));
System.out.println("减2个月 = " + nowPlusOrMin.minusMonths(2));
System.out.println("减12天 = " + nowPlusOrMin.minusDays(12));
System.out.println("减1个星期 = " + nowPlusOrMin.minusWeeks(1));
System.out.println("减2个小时 = " + nowPlusOrMin.minusHours(2));
System.out.println("减20分钟 = " + nowPlusOrMin.minusMinutes(20));
System.out.println("减20秒 = " + nowPlusOrMin.minusSeconds(20));
System.out.println("减1个纳秒 = " + nowPlusOrMin.minusNanos(1));
System.out.println("============================");
}
}
结果
* 5.比较两个日期时间对象的大小
* dateTime01.isEqual(dateTime02) : 两个日期时间对象是否相等
* dateTime01.isAfter(dateTime02) : dateTime01 是否 比 dateTime02 晚
* dateTiime01.isBefore(dateTime02) : dateTime01 是否 比 dateTime02 早
*
代码
package com.northcastle.K_Date;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class LocalDateTimeTest {
public static void main(String[] args) {
//5.比较两个对象的大小
LocalDateTime dateTime01 = LocalDateTime.now();
LocalDateTime dateTime02 = LocalDateTime.of(2022, 3, 22, 21, 37, 59);
System.out.println("dateTime01 = " + dateTime01);
System.out.println("dateTime02 = " + dateTime02);
System.out.println("dateTime01是否与dateTime02相等 : " + dateTime01.isEqual(dateTime02));
System.out.println("dateTime01是否比dateTime02晚 : " + dateTime01.isAfter(dateTime02));
System.out.println("dateTime01是否比dateTime02早 : " + dateTime01.isBefore(dateTime02));
System.out.println("============================")
}
}
结果
* 6. 日期时间对象 与 字符串 的相互转化
* 6.1 日期时间对象 转 字符串 format() + DateTimeFormatter类
* String format01 = localDateTime.format(DateTimeFormatter.ISO_DATE_TIME); //2022-03-23T22:11:36.946
* String format02 = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);//2022-03-23T22:11:36.946
* String format03 = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); //2022-03-23 22:11:36
* 6.2 字符串 转 日期时间对象 parse()
* LocalDateTime localDateTimeParse = LocalDateTime.parse("2022--03--23 22::07::58", DateTimeFormatter.ofPattern("yyyy--MM--dd HH::mm::ss"));
*
代码
package com.northcastle.K_Date;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class LocalDateTimeTest {
public static void main(String[] args) {
//6.1 日期时间对象 转 字符串
LocalDateTime dateTimeFormat01 = LocalDateTime.now();
System.out.println("dateTimeFormat01 = " + dateTimeFormat01);
String format01 = dateTimeFormat01.format(DateTimeFormatter.ISO_DATE_TIME); //2022-03-23T22:11:36.946
System.out.println("format01(ISO_DATE_TIME) = " + format01);
String format02 = dateTimeFormat01.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);//2022-03-23T22:11:36.946
System.out.println("format02(ISO_LOCAL_DATE_TIME) = " + format02);
String format03 = dateTimeFormat01.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); //2022-03-23 22:11:36
System.out.println("format03 = " + format03);
System.out.println();
//6.2 字符串 转 日期时间对象
LocalDateTime localDateTimeParse = LocalDateTime.parse("2022--03--23 22::07::58", DateTimeFormatter.ofPattern("yyyy--MM--dd HH::mm::ss"));
System.out.println("localDateTimeParse = " + localDateTimeParse);
System.out.println("============================");
}
}
结果
Congratulations!
You are one step closer to success!