进阶day01-LocalTime类和LocalDateTime类的基本使用(代替Calender)
This commit is contained in:
@@ -0,0 +1,64 @@
|
|||||||
|
package com.inmind.jdk8_time04;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
|
||||||
|
/*
|
||||||
|
LocalDateTime:代表年月日 时分秒
|
||||||
|
*/
|
||||||
|
public class LocalDateTimeDemo14 {
|
||||||
|
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();
|
||||||
|
LocalDateTime ldt10 = LocalDateTime.of(ld, lt);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
66
javaSE-day01/src/com/inmind/jdk8_time04/LocalTimeDemo13.java
Normal file
66
javaSE-day01/src/com/inmind/jdk8_time04/LocalTimeDemo13.java
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package com.inmind.jdk8_time04;
|
||||||
|
|
||||||
|
import java.time.LocalTime;
|
||||||
|
|
||||||
|
/*
|
||||||
|
LocalTime表示的是时分秒
|
||||||
|
*/
|
||||||
|
public class LocalTimeDemo13 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 0、获取本地时间对象
|
||||||
|
LocalTime lt = LocalTime.now(); // 时 分 秒 纳秒 不可变的
|
||||||
|
System.out.println(lt);
|
||||||
|
|
||||||
|
// 1、获取时间中的信息
|
||||||
|
int hour = lt.getHour(); //时
|
||||||
|
int minute = lt.getMinute(); //分
|
||||||
|
int second = lt.getSecond(); //秒
|
||||||
|
int nano = lt.getNano(); //纳秒
|
||||||
|
System.out.println(hour);
|
||||||
|
System.out.println(minute);
|
||||||
|
System.out.println(second);
|
||||||
|
System.out.println(nano);
|
||||||
|
|
||||||
|
// 2、修改时间:withHour、withMinute、withSecond、withNano
|
||||||
|
LocalTime lt3 = lt.withHour(10);
|
||||||
|
LocalTime lt4 = lt.withMinute(10);
|
||||||
|
LocalTime lt5 = lt.withSecond(10);
|
||||||
|
LocalTime lt6 = lt.withNano(10);
|
||||||
|
System.out.println(lt3);
|
||||||
|
System.out.println(lt4);
|
||||||
|
System.out.println(lt5);
|
||||||
|
System.out.println(lt6);
|
||||||
|
|
||||||
|
// 3、加多少:plusHours、plusMinutes、plusSeconds、plusNanos
|
||||||
|
LocalTime lt7 = lt.plusHours(10);
|
||||||
|
LocalTime lt8 = lt.plusMinutes(10);
|
||||||
|
LocalTime lt9 = lt.plusSeconds(10);
|
||||||
|
LocalTime lt10 = lt.plusNanos(10);
|
||||||
|
System.out.println(lt7);
|
||||||
|
System.out.println(lt8);
|
||||||
|
System.out.println(lt9);
|
||||||
|
System.out.println(lt10);
|
||||||
|
|
||||||
|
// 4、减多少:minusHours、minusMinutes、minusSeconds、minusNanos
|
||||||
|
LocalTime lt11 = lt.minusHours(10);
|
||||||
|
LocalTime lt12 = lt.minusMinutes(10);
|
||||||
|
LocalTime lt13 = lt.minusSeconds(10);
|
||||||
|
LocalTime lt14 = lt.minusNanos(10);
|
||||||
|
|
||||||
|
System.out.println(lt11);
|
||||||
|
System.out.println(lt12);
|
||||||
|
System.out.println(lt13);
|
||||||
|
System.out.println(lt14);
|
||||||
|
|
||||||
|
// 5、获取指定时间的LocalTime对象:
|
||||||
|
// public static LocalTime of(int hour, int minute, int second)
|
||||||
|
LocalTime lt15 = LocalTime.of(12, 12, 12);
|
||||||
|
LocalTime lt16 = LocalTime.of(12, 12, 12);
|
||||||
|
|
||||||
|
// 6、判断2个时间对象,是否相等,在前还是在后: equals isBefore isAfter
|
||||||
|
System.out.println(lt15.equals(lt16)); // true
|
||||||
|
System.out.println(lt15.isAfter(lt)); // false
|
||||||
|
System.out.println(lt15.isBefore(lt)); // true
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user