{"id":4885,"date":"2024-10-07T21:01:01","date_gmt":"2024-10-07T13:01:01","guid":{"rendered":""},"modified":"2024-10-07T21:01:01","modified_gmt":"2024-10-07T13:01:01","slug":"\u4e0d\u7b80\u5355\u7684\u5973\u4eba\u6709\u54ea\u4e9b\u7279\u5f81_Javajre\u4e0b\u8f7d","status":"publish","type":"post","link":"https:\/\/mushiming.com\/4885.html","title":{"rendered":"\u4e0d\u7b80\u5355\u7684\u5973\u4eba\u6709\u54ea\u4e9b\u7279\u5f81_Javajre\u4e0b\u8f7d"},"content":{"rendered":"
\n

\n<\/p>\n<\/p>\n

\u70b9\u51fb\u5de6\u4e0a\u89d2\u84dd\u5b57\uff0c\u5173\u6ce8\u201c\u9505\u5916\u7684\u5927\u4f6c\u201d<\/strong>\"\u4e0d\u7b80\u5355\u7684\u5973\u4eba\u6709\u54ea\u4e9b\u7279\u5f81_Javajre\u4e0b\u8f7d<\/p>\n

\n

\u4e13\u6ce8\u5206\u4eab\u56fd\u5916\u6700\u65b0\u6280\u672f\u5185\u5bb9<\/strong><\/p>\n

\n

\u4e8b\u5b9e\u8bc1\u660e\uff0cJava \u7684 SimpleDateFormat \u5e76\u6ca1\u6709\u90a3\u4e48\u7b80\u5355\u3002<\/em><\/h2>\n

\u683c\u5f0f\u5316\u548c\u89e3\u6790\u65e5\u671f\u662f\u4e2a\uff08\u75db\u82e6\u7684\uff09\u65e5\u5e38\u4efb\u52a1\u3002\u6bcf\u5929\uff0c\u5b83\u90fd\u8ba9\u6211\u4eec\u5f88\u5934\u75bc\u3002<\/p>\n

\u5728 Java \u4e2d\u683c\u5f0f\u5316\u548c\u89e3\u6790\u65e5\u671f\u7684\u4e00\u79cd\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528 SimpleDateFormat<\/code>\u3002\u4e0b\u9762\u662f\u6211\u4eec\u7528\u5230\u7684\u4e00\u4e2a\u516c\u5171\u7c7b\u3002<\/p>\n

\n

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public final class DateUtils { public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat(\"yyyy-MM-dd\"); private DateUtils() {} public static Date parse(String target) { try { return SIMPLE_DATE_FORMAT.parse(target); } catch (ParseException e) { e.printStackTrace(); } return null; } public static String format(Date target) { return SIMPLE_DATE_FORMAT.format(target); } }<\/code><\/pre>\n

\u4f60\u89c9\u5f97\u5b83\u4f1a\u50cf\u6211\u4eec\u9884\u671f\u7684\u90a3\u6837\u8fdb\u884c\u5de5\u4f5c\u4e48\uff1f\u8ba9\u6211\u4eec\u8bd5\u4e00\u8bd5\u3002<\/p>\n

\n

private static void testSimpleDateFormatInSingleThread() { final String source = \"2019-01-11\"; System.out.println(DateUtils.parse(source)); } \/\/ Fri Jan 11 00:00:00 IST 2019<\/code><\/pre>\n

\u662f\u7684\uff0c\u5b83\u594f\u6548\u4e86\u3002\u63a5\u4e0b\u6765\u518d\u7528\u591a\u7ebf\u7a0b\u518d\u8bd5\u4e00\u8bd5\u3002<\/p>\n

\n

private static void testSimpleDateFormatWithThreads() { ExecutorService executorService = Executors.newFixedThreadPool(10); final String source = \"2019-01-11\"; System.out.println(\":: parsing date string ::\"); IntStream.rangeClosed(0, 20) .forEach((i) -> executorService.submit(() -> System.out.println(DateUtils.parse(source)))); executorService.shutdown(); }<\/code><\/pre>\n

\u8fd9\u662f\u6211\u5f97\u5230\u7684\u7ed3\u679c\uff1a<\/p>\n

\n

:: parsing date string :: ... omitted Fri Jan 11 00:00:00 IST 2019Sat Jul 11 00:00:00 IST 2111Fri Jan 11 00:00:00 IST 2019 ... omitted<\/code><\/pre>\n

\u7ed3\u679c\u5f88\u6709\u610f\u601d\uff0c\u4e0d\u662f\u4e48\uff1f\u8fd9\u662f\u6211\u4eec\u5927\u591a\u6570\u4eba\u5728 Java \u4e2d\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u5e38\u72af\u7684\u9519\u8bef\u3002\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a\u6211\u4eec\u4e0d\u4e86\u89e3\u7ebf\u7a0b\u5b89\u5168\u3002\u4ee5\u4e0b\u662f Java doc \u4e2d\u5173\u4e8e SimpleDateFormat<\/code> \u7684\u5185\u5bb9\uff1a<\/p>\n

\n

\u65e5\u671f\u683c\u5f0f\u662f\u4e0d\u540c\u6b65\u7684\u3002<\/p>\n

\u5efa\u8bae\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u521b\u5efa\u72ec\u7acb\u7684\u683c\u5f0f\u5b9e\u4f8b\u3002<\/p>\n

\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u4e00\u4e2a\u683c\u5f0f\uff0c\u5219\u5b83\u5fc5\u987b\u662f\u5916\u90e8\u540c\u6b65\u7684\u3002<\/p>\n<\/blockquote>\n

\n

Tip\uff1a\u5f53\u6211\u4eec\u4f7f\u7528\u5b9e\u4f8b\u53d8\u91cf\u65f6\uff0c\u5e94\u59cb\u7ec8\u68c0\u67e5\u5176\u662f\u5426\u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7c7b\u3002<\/p>\n<\/blockquote>\n

\u6b63\u5982\u6587\u6863\u6240\u8ff0\uff0c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u6301\u6709\u4e00\u4e2a\u72ec\u7acb\u7684\u53d8\u91cf\u6765\u89e3\u51b3\u8be5\u95ee\u9898\u3002\u5982\u679c\u6211\u4eec\u60f3\u5171\u4eab\u5bf9\u8c61\uff1f\u6709\u4ec0\u4e48\u89e3\u51b3\u65b9\u6848\uff1f<\/p>\n

1. \u65b9\u6848\u4e00\uff1aThreadLocal<\/h3>\n

\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 ThreadLocal<\/code> \u53d8\u91cf\u6765\u89e3\u51b3\u3002ThreadLocal<\/code> \u7684 get()<\/code> \u65b9\u6cd5\u5c06\u4e3a\u6211\u4eec\u63d0\u4f9b\u5f53\u524d\u7ebf\u7a0b\u7684\u6b63\u786e\u503c\u3002<\/p>\n

\n

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public final class DateUtilsThreadLocal { public static final ThreadLocal SIMPLE_DATE_FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat(\"yyyy-MM-dd\")); private DateUtilsThreadLocal() {} public static Date parse(String target) { try { return SIMPLE_DATE_FORMAT.get().parse(target); } catch (ParseException e) { e.printStackTrace(); } return null; } public static String format(Date target) { return SIMPLE_DATE_FORMAT.get().format(target); } }<\/code><\/pre>\n

2. \u65b9\u6848\u4e8c\uff1aJava 8 \u4e2d\u7ebf\u7a0b\u5b89\u5168\u7684\u65e5\u671f\u65f6\u95f4 API<\/h3>\n

Java 8 \u5f15\u5165\u4e86\u4e00\u5957\u65b0\u7684\u65e5\u671f\u65f6\u95f4 API\u3002\u6211\u4eec\u6709\u4e00\u4e2a\u66f4\u597d\u7684\u3001\u9ebb\u70e6\u66f4\u5c11\u7684 SimpleDateFormat<\/code> \u66ff\u4ee3\u54c1\u3002\u5982\u679c\u6211\u4eec\u771f\u7684\u9700\u8981\u575a\u6301\u4f7f\u7528 SimpleDateFormat<\/code>\uff0c\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528 ThreadLocal<\/code>\u3002\u4f46\u662f\u5f53\u6709\u66f4\u597d\u7684\u9009\u62e9\u65f6\uff0c\u6211\u4eec\u5e94\u8003\u8651\u4f7f\u7528\u5b83\u3002<\/p>\n

Java 8 \u5f15\u5165\u4e86\u51e0\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u65e5\u671f\u7c7b\u3002<\/p>\n

\u4ee5\u4e0b\u662f Java doc \u7684\u63cf\u8ff0\uff1a<\/p>\n

\n

\u672c\u7c7b\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4e14\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<\/blockquote>\n

\u8fd9\u4e9b\u7c7b\u662f\u66f4\u52a0\u503c\u5f97\u7814\u7a76\u7684\uff0c\u5305\u62ec DateTimeFormatter\uff0c[OffsetDateTime]\uff0c ZonedDateTime\uff0cLocalDateTime \uff0cLocalDate \u548c LocalTime\u3002<\/p>\n

\u6211\u4eec\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n

\n

import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateUtilsJava8 { public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(\"yyyy-MM-dd\"); private DateUtilsJava8() {} public static LocalDate parse(String target) { return LocalDate.parse(target, DATE_TIME_FORMATTER); } public static String format(LocalDate target) { return target.format(DATE_TIME_FORMATTER); } }<\/code><\/pre>\n

3. \u7ed3\u8bba<\/h3>\n

Java 8 \u7684\u89e3\u51b3\u65b9\u6848\u4f7f\u7528\u4e0d\u53ef\u53d8\u7c7b\uff0c\u8fd9\u662f\u89e3\u51b3\u591a\u7ebf\u7a0b\u95ee\u9898\u7684\u597d\u65b9\u6cd5\u3002\u4e0d\u53ef\u53d8\u7c7b\u672c\u8d28\u4e0a\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u8bf7\u5c3d\u53ef\u80fd\u5730\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n

Happy coding\uff01<\/p>\n

spring for all\u7ffb\u8bd1\u7ec4<\/strong><\/p>\n


\n

8\u6708\u798f\u5229\u6765\u88ad\uff01\u5173\u6ce8\u516c\u4f17\u53f7<\/strong><\/p>\n

\u540e\u53f0\u56de\u590d<\/strong>\uff1a003 <\/strong>\uff0c\u9886\u53d67\u6708\u7ffb\u8bd1\u96c6\u9526~ <\/p>\n

\u5f80\u671f\u798f\u5229\u56de\u590d<\/strong>\uff1a001\uff0c 002<\/strong>\u5373\u53ef\u9886\u53d6  <\/p>\n

\n

\n<\/p>\n

\"\u4e0d\u7b80\u5355\u7684\u5973\u4eba\u6709\u54ea\u4e9b\u7279\u5f81_Javajre\u4e0b\u8f7d<\/p>\n<\/p>\n

\u25cfTop11 \u6784\u5efa\u548c\u6d4b\u8bd5API\u7684\u5de5\u5177<\/p>\n

\u25cfSpring Boot \u9762\u8bd5\u7684\u5341\u4e2a\u95ee\u9898<\/p>\n

\u25cf\u5f00\u8f66\uff01Spring Batch \u5165\u95e8\u7ea7\u793a\u4f8b\u6559\u7a0b\uff01<\/p>\n

\u53f3\u4e0a\u89d2\u6309\u94ae\u5206\u4eab\u7ed9\u66f4\u591a\u4eba\u54e6~<\/strong>\"\u4e0d\u7b80\u5355\u7684\u5973\u4eba\u6709\u54ea\u4e9b\u7279\u5f81_Javajre\u4e0b\u8f7d\"\u4e0d\u7b80\u5355\u7684\u5973\u4eba\u6709\u54ea\u4e9b\u7279\u5f81_Javajre\u4e0b\u8f7d<\/strong><\/p>\n

\"\u4e0d\u7b80\u5355\u7684\u5973\u4eba\u6709\u54ea\u4e9b\u7279\u5f81_Javajre\u4e0b\u8f7d<\/p>\n

\n<\/p>\n<\/p>\n

\u6765\u90fd\u6765\u4e86\uff0c\u70b9\u4e2a\u5728\u770b\u518d\u8d70\u5427<\/strong>~~~<\/strong><\/p>\n<\/p>\n

\"\u4e0d\u7b80\u5355\u7684\u5973\u4eba\u6709\u54ea\u4e9b\u7279\u5f81_Javajre\u4e0b\u8f7d<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\u4e0d\u7b80\u5355\u7684\u5973\u4eba\u6709\u54ea\u4e9b\u7279\u5f81_Javajre\u4e0b\u8f7d\u70b9\u51fb\u5de6\u4e0a\u89d2\u84dd\u5b57\uff0c\u5173\u6ce8\u201c\u9505\u5916\u7684\u5927\u4f6c\u201d\u4e13\u6ce8\u5206\u4eab\u56fd\u5916\u6700\u65b0\u6280\u672f\u5185\u5bb9\u4e8b\u5b9e\u8bc1\u660e\uff0cJava\u7684SimpleDateF...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/4885"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=4885"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/4885\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=4885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=4885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=4885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}