一、日期類APIjava
拋出 java8 以前的日期問題:api
下面是java8的:socket
package date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Date; public class DateTestBeforeJava8 { public static void main(String[] args) throws ParseException { Date date = new Date(120, 2, 6); System.out.println(date); SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyyMMdd"); Date parseData = simpleFormatter.parse("20200306"); System.out.println(parseData); // java8之前的問題重現 /*SimpleDateFormat simpleFormatter2 = new SimpleDateFormat("yyyyMMdd"); for (int i = 0; i < 30; i++) { new Thread(() -> { for (int j = 0; j < 1000; j++) { Date parseData2 = null; try { parseData2 = simpleFormatter2.parse("20200306"); } catch (ParseException e) { e.printStackTrace(); } System.out.println(parseData2); } }).start(); }*/ testJava8LocalDate(); System.out.println("========"); testJava8LocalTime(); System.out.println("========"); testJava8LocalDateTime(); System.out.println("========"); testJava8Instant(); System.out.println("========"); testJava8Duration(); System.out.println("========"); testJava8Period(); System.out.println("========"); testJava8Formatter(); System.out.println("========"); testJava8Parse(); System.out.println("========"); } private static void testJava8Parse() { String date = "2020-03-06"; LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd")); System.out.println(localDate); String date2 = "20200306"; LocalDate localDate2 = LocalDate.parse(date2, DateTimeFormatter.BASIC_ISO_DATE); System.out.println(localDate2); } private static void testJava8Formatter() { LocalDate localDate = LocalDate. now(); String format = localDate.format(DateTimeFormatter.BASIC_ISO_DATE); System.out.println(format); LocalDate localDate2 = LocalDate. now(); String format2 = localDate2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); System.out.println(format2); } // Period類使用 private static void testJava8Period() { Period period = Period.of(2020, 3, 6); System.out.println(period.minusDays(3)); System.out.println(period.minusMonths(1)); System.out.println(period.minusYears(1)); System.out.println(period.plusYears(1)); System.out.println(period.plusMonths(1)); System.out.println(period.plusDays(1)); } // Duration類使用 private static void testJava8Duration() { LocalDate localDate = LocalDate.of(2020, 3, 6); LocalTime localTime = LocalTime.now(); LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime); try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } LocalDate localDate2 = LocalDate.of(2019, 3, 6); LocalTime localTime2 = LocalTime.now(); LocalDateTime localDateTime2 = LocalDateTime.of(localDate2, localTime2); Duration duration = Duration.between(localDateTime2, localDateTime); System.out.println(duration.toDays()); System.out.println(duration.toHours()); System.out.println(duration.toMinutes()); System.out.println(duration.toMillis()); } // Instant類使用 private static void testJava8Instant() { Instant startInstant = Instant.now(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Instant endInstant = Instant.now(); Duration duration = Duration.between(startInstant, endInstant); System.out.println(duration.get(ChronoUnit.SECONDS)); } // LocalDateTime類使用 private static void testJava8LocalDateTime() { LocalDate localDate = LocalDate.now(); LocalTime localTime = LocalTime.now(); LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime); System.out.println(localDateTime.getYear()); System.out.println(localDateTime.getMonth()); System.out.println(localDateTime.getDayOfYear()); System.out.println(localDateTime.getDayOfMonth()); System.out.println(localDateTime.getDayOfWeek()); System.out.println(localDateTime.getHour()); System.out.println(localDateTime.getMinute()); System.out.println(localDateTime.getSecond()); } // LocalTime類使用 private static void testJava8LocalTime() { LocalTime localTime = LocalTime.now(); System.out.println(localTime.getHour()); System.out.println(localTime.getMinute()); System.out.println(localTime.getSecond()); System.out.println(localTime.getNano()); } // LocalDate類使用 private static void testJava8LocalDate() { LocalDate localDate = LocalDate.of(2020, 3, 6); System.out.println(localDate.getYear()); System.out.println(localDate.getMonth()); System.out.println(localDate.getMonthValue()); System.out.println(localDate.getDayOfYear()); System.out.println(localDate.getDayOfMonth()); System.out.println(localDate.getDayOfWeek()); } }
執行結果:ide
F:\jdk_1.8\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:65003,suspend=y,server=n -javaagent:C:\Users\Administrator\.IntelliJIdea2019.3\system\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "F:\jdk_1.8\jre\lib\charsets.jar;F:\jdk_1.8\jre\lib\deploy.jar;F:\jdk_1.8\jre\lib\ext\access-bridge-64.jar;F:\jdk_1.8\jre\lib\ext\cldrdata.jar;F:\jdk_1.8\jre\lib\ext\dnsns.jar;F:\jdk_1.8\jre\lib\ext\jaccess.jar;F:\jdk_1.8\jre\lib\ext\jfxrt.jar;F:\jdk_1.8\jre\lib\ext\localedata.jar;F:\jdk_1.8\jre\lib\ext\nashorn.jar;F:\jdk_1.8\jre\lib\ext\sunec.jar;F:\jdk_1.8\jre\lib\ext\sunjce_provider.jar;F:\jdk_1.8\jre\lib\ext\sunmscapi.jar;F:\jdk_1.8\jre\lib\ext\sunpkcs11.jar;F:\jdk_1.8\jre\lib\ext\zipfs.jar;F:\jdk_1.8\jre\lib\javaws.jar;F:\jdk_1.8\jre\lib\jce.jar;F:\jdk_1.8\jre\lib\jfr.jar;F:\jdk_1.8\jre\lib\jfxswt.jar;F:\jdk_1.8\jre\lib\jsse.jar;F:\jdk_1.8\jre\lib\management-agent.jar;F:\jdk_1.8\jre\lib\plugin.jar;F:\jdk_1.8\jre\lib\resources.jar;F:\jdk_1.8\jre\lib\rt.jar;G:\my_project\java8\java8-sharing\target\classes;F:\IntelliJ IDEA 2019.3.1\lib\idea_rt.jar" date.DateTestBeforeJava8 Connected to the target VM, address: '127.0.0.1:65003', transport: 'socket' Fri Mar 06 00:00:00 CST 2020 Fri Mar 06 00:00:00 CST 2020 2020 MARCH 3 66 6 FRIDAY ======== 23 37 24 516000000 ======== 2020 MARCH 66 6 FRIDAY 23 37 24 ======== 1 ======== 365 8783 527039 31622398500 ======== P2020Y3M3D P2020Y2M6D P2019Y3M6D P2021Y3M6D P2020Y4M6D P2020Y3M7D ======== 20200306 2020-03-06 ======== 2020-03-06 2020-03-06 ======== Disconnected from the target VM, address: '127.0.0.1:65003', transport: 'socket' Process finished with exit code 0