Java8新特新 - 日期类API

Java8新特新 - 日期类API

    抛出 java8 之前的日期问题:

    下面是java8的:

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());
    }
}

    执行结果:

F:jdk_1.8injava.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:65003,suspend=y,server=n -javaagent:C:UsersAdministrator.IntelliJIdea2019.3systemcaptureAgentdebugger-agent.jar -Dfile.encoding=UTF-8 -classpath "F:jdk_1.8jrelibcharsets.jar;F:jdk_1.8jrelibdeploy.jar;F:jdk_1.8jrelibextaccess-bridge-64.jar;F:jdk_1.8jrelibextcldrdata.jar;F:jdk_1.8jrelibextdnsns.jar;F:jdk_1.8jrelibextjaccess.jar;F:jdk_1.8jrelibextjfxrt.jar;F:jdk_1.8jrelibextlocaledata.jar;F:jdk_1.8jrelibext
ashorn.jar;F:jdk_1.8jrelibextsunec.jar;F:jdk_1.8jrelibextsunjce_provider.jar;F:jdk_1.8jrelibextsunmscapi.jar;F:jdk_1.8jrelibextsunpkcs11.jar;F:jdk_1.8jrelibextzipfs.jar;F:jdk_1.8jrelibjavaws.jar;F:jdk_1.8jrelibjce.jar;F:jdk_1.8jrelibjfr.jar;F:jdk_1.8jrelibjfxswt.jar;F:jdk_1.8jrelibjsse.jar;F:jdk_1.8jrelibmanagement-agent.jar;F:jdk_1.8jrelibplugin.jar;F:jdk_1.8jrelib
esources.jar;F:jdk_1.8jrelib
t.jar;G:my_projectjava8java8-sharing	argetclasses;F:IntelliJ IDEA 2019.3.1libidea_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