s-day01-LocalDateTime类的基本使用(重点学习)
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package com.inmind.jdk8_date05;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
|
||||
/*
|
||||
LocalDateTime类的基本使用(重点学习)
|
||||
LocalDateTime就是LocalDate和LocalTime的组合,它们是可以互相转换的
|
||||
|
||||
*/
|
||||
public class LocalDateTimeDemo11 {
|
||||
public static void main(String[] args) {
|
||||
// 0、获取本地日期和时间对象。
|
||||
LocalDateTime ldt = LocalDateTime.now(); // 年 月 日 时 分 秒 纳秒
|
||||
System.out.println(ldt);
|
||||
|
||||
// 1、可以获取日期和时间的全部信息
|
||||
int year = ldt.getYear(); // 年
|
||||
int month = ldt.getMonthValue(); // 月
|
||||
int day = ldt.getDayOfMonth(); // 日
|
||||
int dayOfYear = ldt.getDayOfYear(); // 一年中的第几天
|
||||
int dayOfWeek = ldt.getDayOfWeek().getValue(); // 获取是周几
|
||||
int hour = ldt.getHour(); //时
|
||||
int minute = ldt.getMinute(); //分
|
||||
int second = ldt.getSecond(); //秒
|
||||
int nano = ldt.getNano(); //纳秒
|
||||
|
||||
// 2、修改时间信息:
|
||||
// withYear withMonth withDayOfMonth withDayOfYear withHour
|
||||
// withMinute withSecond withNano
|
||||
LocalDateTime ldt2 = ldt.withYear(2029);
|
||||
LocalDateTime ldt3 = ldt.withMinute(59);
|
||||
|
||||
// 3、加多少:
|
||||
// plusYears plusMonths plusDays plusWeeks plusHours plusMinutes plusSeconds plusNanos
|
||||
LocalDateTime ldt4 = ldt.plusYears(2);
|
||||
LocalDateTime ldt5 = ldt.plusMinutes(3);
|
||||
|
||||
// 4、减多少:
|
||||
// minusDays minusYears minusMonths minusWeeks minusHours minusMinutes minusSeconds minusNanos
|
||||
LocalDateTime ldt6 = ldt.minusYears(2);
|
||||
LocalDateTime ldt7 = ldt.minusMinutes(3);
|
||||
|
||||
|
||||
// 5、获取指定日期和时间的LocalDateTime对象:
|
||||
// public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour,
|
||||
// int minute, int second, int nanoOfSecond)
|
||||
LocalDateTime ldt8 = LocalDateTime.of(2029, 12, 12, 12, 12, 12, 1222);
|
||||
LocalDateTime ldt9 = LocalDateTime.of(2029, 12, 12, 12, 12, 12, 1222);
|
||||
|
||||
// 6、 判断2个日期、时间对象,是否相等,在前还是在后: equals、isBefore、isAfter
|
||||
System.out.println(ldt9.equals(ldt8));
|
||||
System.out.println(ldt9.isAfter(ldt));
|
||||
System.out.println(ldt9.isBefore(ldt));
|
||||
|
||||
// 7、可以把LocalDateTime转换成LocalDate和LocalTime
|
||||
// public LocalDate toLocalDate()
|
||||
// public LocalTime toLocalTime()
|
||||
// public static LocalDateTime of(LocalDate date, LocalTime time)
|
||||
LocalDate ld = ldt.toLocalDate();
|
||||
LocalTime lt = ldt.toLocalTime();
|
||||
System.out.println(ld);
|
||||
System.out.println(lt);
|
||||
LocalDateTime ldt10 = LocalDateTime.of(ld, lt);
|
||||
System.out.println(ldt10);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user