java8 新特性 DateTimeFormatter

2019-07-04 00:00:00 datetimeformatter 新特性 Java8
/**
         * 1 新日期处理函数 DateTimeFormatter 是线程安全的
         * SimpleTimeFormatter 线程是不安全的,可以使用枷锁或者使用ThradLocal来实现  比较复杂
         */
        LocalDateTime nowTime = LocalDateTime.now();
        nowTime.plusDays(1);
        System.out.println(nowTime.plusDays(-1).format(formatter));
        System.out.println(11);
        System.out.println(format(new Date()));
    }

    private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    /**
     *    @Description: 返回本线程值
     *    @author wangyuanyuan@zto.com
     *    @date  @DATE@TIME
     *      @return ${return_type}
     */
    public static String format(Date date) {
        return threadLocal.get().format(date);
    }

结果

"C:\Program Files\Java\jdk1.8.0_144\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=53710:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;E:\project\webtest\target\classes;E:\repository\com\zto\titans\titans-web\1.7.5\titans-web-1.7.5.jar;E:\repository\org\springframework\boot\spring-boot-starter-web\1.5.3.RELEASE\spring-boot-starter-web-1.5.3.RELEASE.jar;E:\repository\org\springframework\boot\spring-boot-starter\1.5.3.RELEASE\spring-boot-starter-1.5.3.RELEASE.jar;E:\repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;E:\repository\org\springframework\boot\spring-boot-starter-tomcat\1.5.3.RELEASE\spring-boot-starter-tomcat-1.5.3.RELEASE.jar;E:\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.14\tomcat-embed-core-8.5.14.jar;E:\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.14\tomcat-embed-el-8.5.14.jar;E:\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.14\tomcat-embed-websocket-8.5.14.jar;E:\repository\org\hibernate\hibernate-validator\5.3.5.Final\hibernate-validator-5.3.5.Final.jar;E:\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;E:\repository\org\jboss\logging\jboss-logging\3.3.1.Final\jboss-logging-3.3.1.Final.jar;E:\repository\com\fasterxml\classmate\1.3.3\classmate-1.3.3.jar;E:\repository\com\fasterxml\jackson\core\jackson-databind\2.8.8\jackson-databind-2.8.8.jar;E:\repository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar;E:\repository\com\fasterxml\jackson\core\jackson-core\2.8.8\jackson-core-2.8.8.jar;E:\repository\org\springframework\spring-web\4.3.8.RELEASE\spring-web-4.3.8.RELEASE.jar;E:\repository\org\springframework\spring-beans\4.3.8.RELEASE\spring-beans-4.3.8.RELEASE.jar;E:\repository\org\springframework\spring-context\4.3.8.RELEASE\spring-context-4.3.8.RELEASE.jar;E:\repository\org\springframework\spring-webmvc\4.3.8.RELEASE\spring-webmvc-4.3.8.RELEASE.jar;E:\repository\org\springframework\spring-expression\4.3.8.RELEASE\spring-expression-4.3.8.RELEASE.jar;E:\repository\org\springframework\boot\spring-boot-starter-security\1.5.3.RELEASE\spring-boot-starter-security-1.5.3.RELEASE.jar;E:\repository\org\springframework\spring-aop\4.3.8.RELEASE\spring-aop-4.3.8.RELEASE.jar;E:\repository\org\springframework\security\spring-security-config\4.2.2.RELEASE\spring-security-config-4.2.2.RELEASE.jar;E:\repository\org\springframework\security\spring-security-core\4.2.2.RELEASE\spring-security-core-4.2.2.RELEASE.jar;E:\repository\org\springframework\security\spring-security-web\4.2.2.RELEASE\spring-security-web-4.2.2.RELEASE.jar;E:\repository\com\zto\titans\titans-common\1.7.5\titans-common-1.7.5.jar;E:\repository\org\springframework\boot\spring-boot-autoconfigure\1.5.3.RELEASE\spring-boot-autoconfigure-1.5.3.RELEASE.jar;E:\repository\commons-beanutils\commons-beanutils\1.9.3\commons-beanutils-1.9.3.jar;E:\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;E:\repository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;E:\repository\commons-io\commons-io\2.3\commons-io-2.3.jar;E:\repository\org\apache\commons\commons-lang3\3.5\commons-lang3-3.5.jar;E:\repository\net\sourceforge\jexcelapi\jxl\2.6.12\jxl-2.6.12.jar;E:\repository\com\alibaba\fastjson\1.2.31\fastjson-1.2.31.jar;E:\repository\com\thoughtworks\xstream\xstream\1.4.9\xstream-1.4.9.jar;E:\repository\xmlpull\xmlpull\1.1.3.1\xmlpull-1.1.3.1.jar;E:\repository\xpp3\xpp3_min\1.1.4c\xpp3_min-1.1.4c.jar;E:\repository\com\dangdang\sharding-jdbc-self-id-generator\1.4.1\sharding-jdbc-self-id-generator-1.4.1.jar;E:\repository\org\projectlombok\lombok\1.16.16\lombok-1.16.16.jar;E:\repository\com\zto\titans\titans-dubbo\1.7.5\titans-dubbo-1.7.5.jar;E:\repository\com\alibaba\dubbo\2.5.3\dubbo-2.5.3.jar;E:\repository\org\javassist\javassist\3.21.0-GA\javassist-3.21.0-GA.jar;E:\repository\com\github\sgroschupf\zkclient\0.1\zkclient-0.1.jar;E:\repository\org\apache\zookeeper\zookeeper\3.4.6\zookeeper-3.4.6.jar;E:\repository\jline\jline\0.9.94\jline-0.9.94.jar;E:\repository\io\netty\netty\3.10.6.Final\netty-3.10.6.Final.jar;E:\repository\com\zto\titans\titans-http\1.7.5\titans-http-1.7.5.jar;E:\repository\org\apache\httpcomponents\httpclient\4.5.2\httpclient-4.5.2.jar;E:\repository\org\apache\httpcomponents\httpcore\4.4.6\httpcore-4.4.6.jar;E:\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;E:\repository\org\apache\httpcomponents\httpmime\4.5.2\httpmime-4.5.2.jar;E:\repository\com\zto\titans\titans-logging\1.7.5\titans-logging-1.7.5.jar;E:\repository\org\springframework\boot\spring-boot-starter-logging\1.5.3.RELEASE\spring-boot-starter-logging-1.5.3.RELEASE.jar;E:\repository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;E:\repository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;E:\repository\org\slf4j\jcl-over-slf4j\1.7.25\jcl-over-slf4j-1.7.25.jar;E:\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;E:\repository\org\slf4j\log4j-over-slf4j\1.7.25\log4j-over-slf4j-1.7.25.jar;E:\repository\com\dianping\cat\cat-client\1.4.0-ZTO\cat-client-1.4.0-ZTO.jar;E:\repository\org\unidal\framework\foundation-service\2.5.0\foundation-service-2.5.0.jar;E:\repository\org\codehaus\plexus\plexus-container-default\1.6\plexus-container-default-1.6.jar;E:\repository\org\codehaus\plexus\plexus-classworlds\2.5.1\plexus-classworlds-2.5.1.jar;E:\repository\org\apache\xbean\xbean-reflect\3.7\xbean-reflect-3.7.jar;E:\repository\org\codehaus\plexus\plexus-utils\3.0.24\plexus-utils-3.0.24.jar;E:\repository\com\google\code\gson\gson\2.8.0\gson-2.8.0.jar;E:\repository\com\zto\titans\titans-config\1.7.5\titans-config-1.7.5.jar;E:\repository\org\springframework\boot\spring-boot-starter-aop\1.5.3.RELEASE\spring-boot-starter-aop-1.5.3.RELEASE.jar;E:\repository\org\aspectj\aspectjweaver\1.8.10\aspectjweaver-1.8.10.jar;E:\repository\com\ctrip\framework\apollo\apollo-client\0.8.0\apollo-client-0.8.0.jar;E:\repository\com\ctrip\framework\apollo\apollo-core\0.8.0\apollo-core-0.8.0.jar;E:\repository\com\google\inject\guice\4.1.0\guice-4.1.0.jar;E:\repository\javax\inject\javax.inject\1\javax.inject-1.jar;E:\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;E:\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\repository\com\netflix\hystrix\hystrix-javanica\1.5.12\hystrix-javanica-1.5.12.jar;E:\repository\org\aspectj\aspectjrt\1.8.10\aspectjrt-1.8.10.jar;E:\repository\org\ow2\asm\asm\5.0.3\asm-5.0.3.jar;E:\repository\com\netflix\hystrix\hystrix-core\1.5.12\hystrix-core-1.5.12.jar;E:\repository\com\netflix\archaius\archaius-core\0.4.1\archaius-core-0.4.1.jar;E:\repository\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;E:\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;E:\repository\io\reactivex\rxjava\1.2.0\rxjava-1.2.0.jar;E:\repository\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;E:\repository\com\google\guava\guava\19.0\guava-19.0.jar;E:\repository\com\google\code\findbugs\jsr305\2.0.0\jsr305-2.0.0.jar;E:\repository\com\zto\titans\titans-orm\1.7.5\titans-orm-1.7.5.jar;E:\repository\com\zaxxer\HikariCP\2.6.1\HikariCP-2.6.1.jar;E:\repository\com\oracle\ojdbc6\11.2.0.3\ojdbc6-11.2.0.3.jar;E:\repository\mysql\mysql-connector-java\6.0.6\mysql-connector-java-6.0.6.jar;E:\repository\org\springframework\boot\spring-boot-starter-jdbc\1.5.3.RELEASE\spring-boot-starter-jdbc-1.5.3.RELEASE.jar;E:\repository\org\apache\tomcat\tomcat-jdbc\8.5.14\tomcat-jdbc-8.5.14.jar;E:\repository\org\apache\tomcat\tomcat-juli\8.5.14\tomcat-juli-8.5.14.jar;E:\repository\org\springframework\spring-jdbc\4.3.8.RELEASE\spring-jdbc-4.3.8.RELEASE.jar;E:\repository\org\springframework\spring-tx\4.3.8.RELEASE\spring-tx-4.3.8.RELEASE.jar;E:\repository\org\mybatis\mybatis\3.4.0\mybatis-3.4.0.jar;E:\repository\org\mybatis\mybatis-spring\1.3.0\mybatis-spring-1.3.0.jar;E:\repository\tk\mybatis\mapper\3.4.4\mapper-3.4.4.jar;E:\repository\javax\persistence\persistence-api\1.0\persistence-api-1.0.jar;E:\repository\com\github\pagehelper\pagehelper\5.0.1\pagehelper-5.0.1.jar;E:\repository\com\github\jsqlparser\jsqlparser\0.9.5\jsqlparser-0.9.5.jar;E:\repository\com\dangdang\sharding-jdbc-core\1.4.1\sharding-jdbc-core-1.4.1.jar;E:\repository\org\apache\commons\commons-collections4\4.1\commons-collections4-4.1.jar;E:\repository\com\alibaba\druid\1.0.12\druid-1.0.12.jar;C:\Program Files\Java\jdk1.8.0_144\lib\jconsole.jar;C:\Program Files\Java\jdk1.8.0_144\lib\tools.jar;E:\repository\cglib\cglib\3.1\cglib-3.1.jar;E:\repository\io\dropwizard\metrics\metrics-core\3.1.4\metrics-core-3.1.4.jar;E:\repository\com\zto\titans\titans-endpoint\1.7.5\titans-endpoint-1.7.5.jar;E:\repository\io\netty\netty-all\4.1.15.Final\netty-all-4.1.15.Final.jar;E:\repository\org\springframework\boot\spring-boot\1.5.3.RELEASE\spring-boot-1.5.3.RELEASE.jar;E:\repository\junit\junit\4.12\junit-4.12.jar;E:\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;E:\repository\org\springframework\spring-core\4.3.8.RELEASE\spring-core-4.3.8.RELEASE.jar" com.zto.test.web.controller.TestController
2018-03-28 21:46:12
11
2018-03-29 21:46:12

Process finished with exit code 0

    原文作者:三好大师
    原文地址: https://zhuanlan.zhihu.com/p/35096121
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章