java.time.LocalDate;
java.time.LocalDateTime;
java.time.LocalTime;
LocatDate對象獲取:html
@Test void contextLoads() { // 獲取方式 LocalDate localDate = LocalDate.now(); System.out.println(localDate); LocalDate localDate1 = LocalDate.of(2020, 5, 23); System.out.println(localDate1); // 2020-09-28 // 2020-05-23 int year = localDate.getYear(); // 取年 int monthValue = localDate.getMonthValue(); // 取月 Month month = localDate.getMonth(); // 取月的枚舉對象 int monthValue1 = month.getValue(); // 具體值能夠經過該枚舉對象獲取 // 除此以外Month還有一些獲取其餘信息的方法 int maxDaysLength = month.maxLength(); // 該月的最大天數 int minDaysLength = month.minLength(); // 該月的最小天數 int dayOfMonth = localDate.getDayOfMonth(); // 按當月取天數 int dayOfYear = localDate.getDayOfYear(); // 按本年取天數 DayOfWeek dayOfWeek = localDate.getDayOfWeek(); // 按本週取天數? // 和月枚舉對象同樣,這裏也是一個周枚舉對象 int value = dayOfWeek.getValue(); System.out.println(dayOfWeek); // 打印爲 MONDAY .... } private static void cryptTest() { final String PASSWORD = "123456"; PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String encode = passwordEncoder.encode(PASSWORD); System.out.println(encode); boolean matches = passwordEncoder.matches(PASSWORD, encode); System.out.println(matches); }
LocalDateTime & LocalTime:java
@Test void contextLoads() { LocalDateTime localDateTime1 = LocalDateTime.now(); LocalDateTime localDateTime2 = LocalDateTime.of(2020, 2, 2, 13, 24, 52); System.out.println(localDateTime1); // 2020-09-28T16:01:48.248 System.out.println(localDateTime2); // 2020-02-02T13:24:52 LocalTime localTime1 = LocalTime.now(); LocalTime localTime2 = LocalTime.of(20, 2, 2); System.out.println(localTime1); // 16:03:41.330 System.out.println(localTime2); // 20:02:02 int hour = localTime1.getHour(); int minute = localTime1.getMinute(); int second = localTime1.getSecond(); int nano = localTime1.getNano(); System.out.println("時 -> " + hour); // 時 -> 16 System.out.println("分 -> " + minute); // 分 -> 6 System.out.println("秒 -> " + second); // 秒 -> 26 System.out.println("Nano -> " + nano); // Nano -> 206000000 }
GET方法ide
SET方法spa
增長時間3d
減小時間code
public class DateTest { public static void main(String[] args) { LocalDate now1 = LocalDate.now(); LocalTime now2 = LocalTime.now(); LocalDateTime now3 = LocalDateTime.now(); System.out.println("LocalDate.now() -> " + now1); System.out.println("LocalTime.now() -> " + now2); System.out.println("LocalDateTime.now() -> " + now3); /* LocalDate.now() -> 2020-04-19 LocalTime.now() -> 21:16:03.854 LocalDateTime.now() -> 2020-04-19T21:16:03.854 */ LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("localDateTime.getDayOfWeek() -> " + localDateTime.getDayOfWeek() ); // 按周算天 System.out.println("localDateTime.getDayOfMonth() -> " + localDateTime.getDayOfMonth() ); // 按月算天 System.out.println("localDateTime.getDayOfYear() -> " + localDateTime.getDayOfYear() ); // 按年算天 /* localDateTime.getDayOfWeek() -> SUNDAY localDateTime.getDayOfMonth() -> 19 localDateTime.getDayOfYear() -> 110 */ System.out.println( localDateTime.getSecond() );// 取秒 System.out.println( localDateTime.getMinute() );// 取分 System.out.println( localDateTime.getHour() );// 取時 System.out.println( localDateTime.getMonth() );// 取月 英文大寫 System.out.println( localDateTime.getMonthValue() );// 取月 數值表示 System.out.println( localDateTime.getYear() );// 取年 // set × with √ } }
public class DateTest { public static void main(String[] args) { Instant instant = Instant.now(); System.out.println(instant); // 2020-04-19T13:47:58.712Z 本初子午線的標準時間 // 咱們是東八時區,添加8小時的偏移量 OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8)); System.out.println(offsetDateTime); // 從實例獲取毫秒數 時間戳 long epochMilli = instant.toEpochMilli(); System.out.println(epochMilli); // 經過時間戳注入產生instant實例 Instant epochMilli1 = Instant.ofEpochMilli(epochMilli); System.out.println(epochMilli1); } }
public class DateTest { public static void main(String[] args) { // 預約義的標準格式 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; // 事件對象 LocalDateTime now = LocalDateTime.now(); // 轉換格式 日期 -> 字符串格式 String format = dateTimeFormatter.format(now); // 格式 System.out.println( now ); System.out.println( format ); // 日期 轉 字符串格式 TemporalAccessor parse = dateTimeFormatter.parse("2020-03-19T22:09:46.345"); System.out.println(parse); DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL); // ofLocalizedDateTime(); // ofLocalizedDate(); 按Date就支持FULL全格式 // ofPattern("自定義格式"); "yyyy-MM-dd hh:mm:ss" // FormatStyle.FULL 2020年4月19日 星期日 // FormatStyle.LONG 2020年4月19日 下午10時15分01秒 // FormatStyle.MEDIUM 2020-4-19 22:14:28 // FormatStyle.SHORT 20-4-19 下午10:16 String format1 = dateTimeFormatter1.format(now); System.out.println(now); System.out.println(format1); TemporalAccessor parse1 = dateTimeFormatter1.parse(format1); System.out.println(parse1); } }
@Test void contextLoads() { localDateCalendar(); } private static void localDateCalendar() { LocalDate now = LocalDate.now(); int year = now.getYear(); int month = now.getMonth().getValue(); int dayOfMonth = now.getDayOfMonth(); // 設置這個月的第一天 now = now.minusDays(dayOfMonth - 1); // 找到這一天爲周幾 int value = now.getDayOfWeek().getValue(); // 開始渲染控制檯輸出樣式 System.out.println("Mon Tue Wed Thu Fri Sat Sun"); for (int i = 1; i < value; i++) System.out.print(" "); while (now.getMonthValue() == month) { System.out.printf("%3d", now.getDayOfMonth()); if (now.getDayOfMonth() == dayOfMonth) System.out.print("*"); else System.out.print(" "); now = now.plusDays(1); if (now.getDayOfWeek().getValue() == 1) System.out.println(); } if (now.getDayOfWeek().getValue() != 1) System.out.println(); }
打印結果:orm
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28* 29 30
ZoneId ZoneDateTime Clock Duration Period TemporalAdjuster TemporalAdjusters