{"id":1142,"date":"2017-09-23T17:07:10","date_gmt":"2017-09-23T08:07:10","guid":{"rendered":"http:\/\/esoro.jp\/?p=1142"},"modified":"2021-04-20T17:01:40","modified_gmt":"2021-04-20T08:01:40","slug":"apache-commons%e3%81%aedateutils%e3%81%ab%e6%a9%9f%e8%83%bd%e3%82%92%e6%8b%a1%e5%bc%b5%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/esoro.jp\/?p=1142","title":{"rendered":"Apache Commons\u306eDateUtils\u306b\u6a5f\u80fd\u3092\u62e1\u5f35\u3059\u308b"},"content":{"rendered":"<p>\u3000\u3088\u304f\u3042\u308b\u30b1\u30fc\u30b9\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u30e1\u30b8\u30e3\u30fc\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u3082\u3046\u3061\u3087\u3063\u3068\u6a5f\u80fd\u304c\u6b32\u3057\u3044\u6642\u3001\u305d\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u62e1\u5f35\u3057\u3066\u5171\u901a\u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u3057\u3066\u4f7f\u3044\u307e\u308f\u3057\u305f\u308a\u3057\u307e\u3059\u3088\u306d\uff1f<br \/>\n\u7b46\u8005\u304cJava\u3092\u4f7f\u3046\u5834\u5408\u306f\u3001\u3060\u3044\u3076\u524d\u304b\u3089Apache Commons\u306eDateUtils\u3092\u62e1\u5f35\u3057\u3001String\u30fbDate\u306e\u76f8\u4e92\u5909\u63db\u30e1\u30bd\u30c3\u30c9\u3068\u304b\u3092\u8ffd\u52a0\u3057\u3066\u3044\u308d\u3093\u306a\u958b\u767a\u6848\u4ef6\u3067\u4f7f\u3044\u307e\u308f\u3057\u3066\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u3001\u6587\u5b57\u5217\u304b\u3089\u65e5\u6642\u578b\u306b\u5909\u63db\u3059\u308b\u3051\u3069\u3001\u3069\u3093\u306a\u6587\u5b57\u5217\u30d1\u30bf\u30fc\u30f3\u306b\u306a\u308b\u304b\u304c\u4e0d\u660e\u78ba\u3001\u3001\u3068\u3044\u3046\u8981\u4ef6\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u62e1\u5f35\u3057\u3066\u3044\u305f\u30af\u30e9\u30b9\u306b\u6a5f\u80fd\u3092\u8ffd\u52a0\u3057\u3001\u4e00\u822c\u7684\u306b\u65e5\u6642\u3068\u3057\u3066\u4f7f\u308f\u308c\u308b\u6587\u5b57\u5217\u304b\u3089\u306eDate\u5909\u63db\u6a5f\u80fd\u3092\u8ffd\u52a0\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u203b2021.04 \u9577\u6587\u304b\u3089\u306e\u65e5\u6642\u6587\u5b57\u5217\u53d6\u5f97\u3067\u554f\u984c\u304c\u3042\u3063\u305f\u306e\u3067\u4fee\u6b63\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true \" title=\"DateUtils\" >package jp.esoro.common.utils;\r\n\r\nimport java.text.Normalizer;\r\nimport java.text.Normalizer.Form;\r\nimport java.text.ParseException;\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Calendar;\r\nimport java.util.Date;\r\nimport java.util.regex.Matcher;\r\nimport java.util.regex.Pattern;\r\n\r\nimport org.apache.commons.lang3.time.DateFormatUtils;\r\n\r\n\/**\r\n * \u65e5\u4ed8\u51e6\u7406\u30af\u30e9\u30b9\r\n * *\/\r\npublic class DateUtils extends org.apache.commons.lang3.time.DateUtils {\r\n\r\n\t\/**\r\n\t * \u65e5\u4ed8\u8868\u793a\u30d1\u30bf\u30fc\u30f3(YYYY\/MM\/DD hh:mm)\r\n\t *\/\r\n\tpublic static final String FORMAT_YYYY_MM_DD_HH_MM = \"yyyy\/MM\/dd HH:mm\";\r\n\r\n\tpublic static final String FORMAT_YYYY_MM_DD = \"yyyy\/MM\/dd\";\r\n\tpublic static final String FORMAT_YYYYMMDD = \"yyyyMMdd\";\r\n\tpublic static final String FORMAT_YYMMDD = \"yyMMdd\";\r\n\tpublic static final String FORMAT_JPN_YYYY_MM_DD = \"yyyy\u5e74MM\u6708dd\u65e5\";\r\n\r\n\tpublic static final String FORMAT_YYYY_MM_DD_HH_MM_SS = \"yyyy\/MM\/dd HH:mm:ss\";\r\n\r\n\tpublic static final String FORMAT_MYSQL_DATE = \"yyyy-MM-dd\";\r\n\tpublic static final String FORMAT_MYSQL_DATETIME = \"yyyy-MM-dd HH:mm:ss\";\r\n\tpublic static final String FORMAT_YYYYMMDDHHMMSS = \"yyyyMMddHHmmss\";\r\n\tpublic static final String FORMAT_YYYYMMDDHHMMSSmi = \"yyyyMMddHHmmssSSS\";\r\n\tpublic static final String FORMAT_YYMMDDHHMMSS = \"yyMMddHHmmss\";\r\n\tpublic static final String FORMAT_YYMMDDHHMMSSmi = \"yyMMddHHmmssSSS\";\r\n\r\n\t\/**\r\n\t * \u65e5\u4ed8(Calendar)\u3092\u6587\u5b57\u5217\u306b\u7de8\u96c6\u3059\u308b\r\n\t * @param date\r\n\t * @param pattern\r\n\t * @return\r\n\t *\/\r\n\tpublic static String format(Calendar date, String pattern) {\r\n\t\tif (date == null) {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t\treturn DateFormatUtils.format(date, pattern);\r\n\t}\r\n\r\n\t\/**\r\n\t * \u65e5\u4ed8(Date)\u3092\u6587\u5b57\u5217\u306b\u7de8\u96c6\u3059\u308b\r\n\t * @param date\r\n\t * @param pattern\r\n\t * @return\r\n\t *\/\r\n\tpublic static String format(Date date, String pattern) {\r\n\t\tif (date == null) {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t\tCalendar cal = Calendar.getInstance();\r\n\t\tcal.setTime(date);\r\n\t\treturn format(cal, pattern);\r\n\t}\r\n\r\n\t\/**\r\n\t * \u30b7\u30b9\u30c6\u30e0\u65e5\u4ed8\u3092\u53d6\u5f97\u3059\u308b\r\n\t * @return Date\r\n\t *\/\r\n\tpublic static Date getDateTime() {\r\n\t\treturn Calendar.getInstance().getTime();\r\n\t}\r\n\r\n\t\/**\r\n\t * \u6587\u5b57\u5217\u3092Date\u578b\u3078\u5909\u63db\u3059\u308b\r\n\t * @param value(yyyy_mm_dd)\r\n\t * @return\r\n\t *\/\r\n\tpublic static Date stringToDate(String value) {\r\n\t    return stringToDate(value , FORMAT_YYYY_MM_DD);\r\n\t}\r\n\r\n\t\/**\r\n\t * \u6587\u5b57\u5217\u3092Date\u578b\u3078\u5909\u63db\u3059\u308b\r\n\t * @param value\r\n\t * @param pattern \r\n\t * @return\r\n\t *\/\r\n\tpublic static Date stringToDate(String value ,String pattern) {\r\n\t    SimpleDateFormat sdf = new SimpleDateFormat(pattern);\r\n\t    if (value == null) {\r\n\t    \treturn null;\r\n\t    }\r\n\t    try {\r\n\t        return sdf.parse(value);\r\n\t    } catch (ParseException e) {\r\n\t        return null;\r\n\t    }\r\n\t}\r\n\/\/\u3053\u3053\u307e\u3067\u4ee5\u524d\u304b\u3089\u3001\u4ee5\u964d\u3092\u8ffd\u52a0\r\n\t\/**\r\n\t * \u65e5\u6642\u6587\u5b57\u5217\u3092Date\u5909\u63db\u3059\u308b\r\n\t * @param str Date\u5909\u63db\u3057\u305f\u3044\u6587\u5b57\u5217\r\n\t * @return Date \u53d6\u5f97\u51fa\u6765\u306a\u3044\u5834\u5408\u306fnull\r\n\t * @see \u3042\u3089\u304b\u3058\u3081\u6587\u5b57\u5217\u30d1\u30bf\u30fc\u30f3\u304c\u5224\u660e\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u3001stringToDate\u3092\u4f7f\u7528\u3057\u3066\u304f\u3060\u3055\u3044\uff09\r\n\t *\/\r\n\tpublic static Date extractDate(String str) {\r\n\t\tfinal String[] regexs = {\"([0-9]{2,4})(\/|-|\u00a5\u00a5.|\u5e74)([01]?[0-9])(\/|-|\u00a5\u00a5.|\u6708)([0123]?[0-9])(\u65e5|\u65e5\u00a5\u00a5s+|\u00a5\u00a5s|_)([0-9]{1,2})(:|-|\u00a5\u00a5.|\u6642)([0-9]{1,2})(:|-|\u00a5\u00a5.|\u5206)([0-9]{1,2})\",\r\n\t\t\t\t\"([0-9]{2,4})(\/|-|\u00a5\u00a5.|\u5e74)([01]?[0-9])(\/|-|\u00a5\u00a5.|\u6708)([0123]?[0-9])(\u65e5|\u65e5\u00a5\u00a5s+|\u00a5\u00a5s|_)([0-9]{1,2})(:|-|\u00a5\u00a5.|\u6642)([0-9]{1,2})\",\r\n\t\t\t\t\"([0-9]{2,4})(\/|-|\u00a5\u00a5.|\u5e74)([01]?[0-9])(\/|-|\u00a5\u00a5.|\u6708)([0123]?[0-9])(\u65e5|\u65e5\u00a5\u00a5s+|\u00a5\u00a5s|_)([0-9]{1,2})(:|-|\u00a5\u00a5.|\u6642)\",\r\n\t\t\t\t\"([0-9]{2,4})(\/|-|\u00a5\u00a5.|\u5e74)([01]?[0-9])(\/|-|\u00a5\u00a5.|\u6708)([0123]?[0-9])\",\r\n\t\t\t\t};\r\n\t\tDate ret = null;\r\n\t\tString normStr = Normalizer.normalize(str, Form.NFKC);\r\n\t\ttry{\r\n\t\t\tString chkStr = StringUtils.removePattern(normStr, \"am|AM|Am|pm|PM|Pm|\u5348\u524d|\u5348\u5f8c\");\r\n\t\t\t\r\n\t\t\tfor(String regex : regexs){\r\n\t\t\t\tPattern pattern = Pattern.compile(regex, Pattern.DOTALL|Pattern.CASE_INSENSITIVE);\r\n\t\t\t\tMatcher matcher = pattern.matcher(chkStr);\r\n\t\t\t\t\r\n\t\t\t\tif(matcher.find()){\r\n\t\t\t\t\tif(matcher.groupCount() >= 5){\r\n\t\t\t\t\t\tboolean isPm = false;\r\n\t\t\t\t\t\tif(normStr.indexOf(matcher.group()) < 0 &#038;&#038; matcher.groupCount() >= 7){\r\n\t\t\t\t\t\t\tif( normStr.matches(\".*\"+matcher.group(1)+matcher.group(2)+matcher.group(3)+matcher.group(4)+matcher.group(5)+matcher.group(6)+\"(pm|PM|pm|\u5348\u5f8c).*\")){\r\n\t\t\t\t\t\t\t\tisPm = true;\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\tif(matcher.group(1).length() == 2){\r\n\t\t\t\t\t\t\tret = DateUtils.setYears(getDateTime(), Integer.valueOf(\"20\" + matcher.group(1)));\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\telse{\r\n\t\t\t\t\t\t\tret = DateUtils.setYears(getDateTime(), Integer.valueOf(matcher.group(1)));\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tret = DateUtils.setDays(ret, 1);\r\n\t\t\t\t\t\t\/\/Month is 0 -11\r\n\t\t\t\t\t\tret = DateUtils.setMonths(ret, Integer.valueOf(matcher.group(3))-1);\r\n\t\t\t\t\t\tret = DateUtils.setDays(ret, Integer.valueOf(matcher.group(5)));\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\tif(matcher.groupCount() >= 7){\r\n\t\t\t\t\t\t\tint hour = Integer.valueOf(matcher.group(7));\r\n\t\t\t\t\t\t\tret = DateUtils.setHours(ret, hour + (hour<12 &#038;&#038; isPm?12:0));\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\telse{\r\n\t\t\t\t\t\t\tret = DateUtils.setHours(ret, 0);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tif(matcher.groupCount() >= 9){\r\n\t\t\t\t\t\t\tret = DateUtils.setMinutes(ret, Integer.valueOf(matcher.group(9)));\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\telse{\r\n\t\t\t\t\t\t\tret = DateUtils.setMinutes(ret, 0);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tif(matcher.groupCount() == 11){\r\n\t\t\t\t\t\t\tret = DateUtils.setSeconds(ret, Integer.valueOf(matcher.group(11)));\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\telse{\r\n\t\t\t\t\t\t\tret = DateUtils.setSeconds(ret, 0);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\treturn ret;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif(ret == null){\r\n\t\t\t\tSimpleDateFormat sdf1 = new SimpleDateFormat(\"EEE MMM dd HH:mm:ss z yyyy\",Locale.ENGLISH);\r\n\t\t        ret = sdf1.parse(chkStr);\r\n\t\t\t}\r\n\t\t}catch(Exception e){\r\n\t        return null;\r\n\t\t}\r\n\t\treturn ret;\r\n\t}\r\n}\r\n<\/pre>\n<p> \u3067\u3001\u30c6\u30b9\u30c8\u3067\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true \" title=\"Datetest.java\" >package test;\r\n\r\nimport jp.esoro.common.utils.DateUtils;\r\n\r\npublic class Datetest {\r\n\tstatic String[] dateStrs = {\r\n\t\t\t\"20170131\",\r\n\t\t\t\"2017-9-2 15:00:00\",\r\n\t\t\t\"2017\/9\/2 15:25:00\",\r\n\t\t\t\"2017\u5e749\u67082\u65e58\u664234\u520651\u79d2\",\r\n\t\t\t\"2017\u5e741\u67082\u65e58\u664232\u5206\",\r\n\t\t\t\"\uff12\uff10\uff11\uff17\u5e74\uff11\uff12\u6708\uff13\uff11\u65e5\u5348\u5f8c\uff11\uff11\u6642\u96c6\u5408\",\r\n\t\t\t\"2017\u5e7412\u67082\u65e58\u6642\",\r\n\t\t\t\"2017.12.2 PM8:00\",\r\n\t\t\t\"2017.12.3 AM8:25\",\r\n\t\t\t\"2017.01.31\",\r\n\t\t\t\"2017.1.32\"\t};\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tfor(String str : dateStrs){\r\n\t\t\tSystem.out.println(str + \" is \" + DateUtils.format(DateUtils.extractDate(str),DateUtils.FORMAT_YYYY_MM_DD_HH_MM_SS));\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>\u30c6\u30b9\u30c8\u7d50\u679c<br \/>\n20170131 is null<br \/>\n2017-9-2 15:00:00 is 2017\/09\/02 15:00:00<br \/>\n2017\/9\/2 15:25:00 is 2017\/09\/02 15:25:00<br \/>\n2017\u5e749\u67082\u65e58\u664234\u520651\u79d2 is 2017\/09\/02 08:34:51<br \/>\n2017\u5e741\u67082\u65e58\u664232\u5206 is 2017\/01\/02 08:32:00<br \/>\n\uff12\uff10\uff11\uff17\u5e74\uff11\uff12\u6708\uff13\uff11\u65e5\u5348\u5f8c\uff11\uff11\u6642\u96c6\u5408 is 2017\/12\/31 23:00:00<br \/>\n2017\u5e7412\u67082\u65e58\u6642 is 2017\/12\/02 08:00:00<br \/>\n2017.12.2 PM8:00 is 2017\/12\/02 20:00:00<br \/>\n2017.12.3 AM8:25 is 2017\/12\/03 08:25:00<br \/>\n2017.01.31 is 2017\/01\/31 00:00:00<br \/>\n2017.1.32 is null<\/p>\n<p>YYYYMMDD\u3063\u3066\u306e\u306f\u3001\u3053\u3053\u3067\u306f\u5bfe\u8c61\u5916\u3067\u3059\u3002\u3060\u3063\u3066\u3001\u305f\u3060\u306e\u6570\u5b57\u306e\u7f85\u5217\u306f\u6587\u5b57\u3068\u3057\u3066\u65e5\u4ed8\u3068\u306f\u8a00\u3048\u306a\u3044\u3057\u3002<\/p>\n<p>\uff12\u5e74\u305f\u3063\u3066\u30d0\u30b0\u3063\u3066\u305f\u4e8b\u304c\u5224\u660e\u3002\uff11\u65e5\u306b\u623b\u3059\u306e\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f\u3002\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u3088\u304f\u3042\u308b\u30b1\u30fc\u30b9\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u30e1\u30b8\u30e3\u30fc\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u3082\u3046\u3061\u3087\u3063\u3068\u6a5f\u80fd\u304c\u6b32\u3057\u3044\u6642\u3001 &hellip; <a href=\"https:\/\/esoro.jp\/?p=1142\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1142"}],"collection":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1142"}],"version-history":[{"count":2,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1142\/revisions"}],"predecessor-version":[{"id":1886,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1142\/revisions\/1886"}],"wp:attachment":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}