⧠ChatGPTã¯ãã²ã¼ã ã»ãã§ã³ã¸ã£ã¼ã«ãªããã§ãããã
æ¤ç´¢ããå´ããããããæ±ããæ å ±ã«è¾¿ãçããä»çµã¿ãæä¾ãã¦ãããã®ã§ããã°æå¥ã¯ããã¾ããã...
Javaã®æ¥ä»ã®ãã¿ã¼ã³ã§å¹´ã®é¨åã«yyyyã¨uuuuã®ã©ã¡ãã使ãã¹ãã
æ¥åã§ãã¨ããWebã·ã¹ãã ã®åä½ãã¹ãããã¦ãã¦æ°ä»ããã®ã§ãããSimpleDateFormatã«ç½ ãå¤ãããã«æãã
setLenient
public void setLenient(boolean lenient)
- æ¥ä»/æå»è§£æãå³å¯ã«è¡ããã©ãããè¨å®ãã¾ããå³å¯ã§ãªã解æã§ã¯ã解ææ©è½ã¯ããã¥ã¼ãªã¹ãã£ãã¯ãªæ¹æ³ã使ã£ã¦ããã®ãªãã¸ã§ã¯ãã®ãã©ã¼ãããã¨å®å ¨ã«ã¯ä¸è´ããªãå ¥åã解éãããã¨ãããã¾ããå³å¯ãªè§£æã§ã¯ãå ¥åã¯ãã®ãªãã¸ã§ã¯ãã®ãã©ã¼ãããã¨ä¸è´ããå¿ è¦ãããã¾ãã
-
- ãã©ã¡ã¼ã¿:
lenient
 - true ã®å ´åã¯å³å¯ã§ã¯ãªã解æ- é¢é£é ç®:
Calendar.setLenient(boolean)
https://docs.oracle.com/javase/jp/6/api/java/text/DateFormat.html#setLenient(boolean)
⧠ããã¥ã¡ã³ããç´ããããã®ã ãã©ã
⧠setLenient(false)ãå®è£ ãã¦ãã¦ãæ¥ä»/æå»è§£æãå³å¯ã«è¡ã£ã¦ãããªãã¨ãã...
試ãã«ãEclipseã§Javaããã¸ã§ã¯ããä½æãã¦ã
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; public class CheckDateFormat { public static void main(String[] args) { // String[] testDateStrList = { "2023/1/1", "2023/1/11", "2023/11/1", "2023/11/11", "2023/01/1", "2023/1/01", "2023/01/01", "2023/02/30", "2023/1/1a", "2023/111/111", "20230/1/1", "2023/1/1/" }; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList[index] + ", "); System.out.println(checkDate(testDateStrList[index])); } } private static boolean checkDate(String strDate) { DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); df.setLenient(false); try { df.parse(strDate); } catch (ParseException e) { // ä¸æ£ãªæ¥ä» return false; } return true; } }
⧠ä¸è¨ãå®è¡ãã¦ã¿ãã¨ã
⧠ãåããããã ããã ãããï¼
ä½ã¨ã20230å¹´ãæ£å¸¸ãªæ¥ä»ã¨ããã¦ãã¾ãã®ã§ããï¼
ã¾ãã2ä¸å¹´å¾ã¨èããã°æ£ããæ¥ä»ã¨è¨ããã®ã§ãããã°ã©ã çã«ã¯æ£å¸¸ãªåä½ãªã®ããããããã©ããã¾ãã¡ãyyyyã®æ£ç¢ºãªå¤å®ãåããããã...
stackoverflowã«ããã¨ã
18888 is a valid year. It's just really far in the future.
From the SimpleDateFormat
 documentation:
For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits.
(Emphasis mine)
You'll just have to range check the date to make sure it's within whatever bounds you want.
https://stackoverflow.com/questions/8762034/simpledateformat-error-with-year
⧠ããã¥ã¡ã³ãã«è¨è¼ãããã¨ã
å¹´:ãã©ã¼ããã¿ã®Calendar
ãã°ã¬ã´ãªãªæ¦ã®å ´åã¯ã次ã«ç¤ºãã«ã¼ã«ãé©ç¨ããã¾ãã
- ãã¿ã¼ã³æåã®æ°ã2ã®å ´åããã©ã¼ãããã«ã¯å¹´ã2æ¡ã«ç縮ããã¾ããããã§ãªãå ´åã¯ãæ°å¤ã¨ãã¦è§£éããã¾ãã
- 解æã«ã¯ããã¿ã¼ã³æåã®æ°ã2以ä¸ã®å ´åãå¹´ã¯æ¡æ°ã«ãããããæåã©ããã«è§£éããã¾ãããMM/dd/yyyyãã®ãã¿ã¼ã³ãç¨ããã¨ãã01/11/12ãã¯A.D. 12å¹´1æ11æ¥ã«è§£éããã¾ãã
https://docs.oracle.com/javase/jp/8/docs/api/java/text/SimpleDateFormat.html
⧠ãªãã»ã©ãå¹´ã®ãã¿ã¼ã³æåã®æ¡æ°ã¨è§£æ対象ã®æ¥ä»ã®å¹´ã®æ¡æ°ã¯ä¸è´ãã¦ãå¿ è¦ã¯ãªãã¨ã
ã¤ã¾ãã5æ¡ã®å¹´æ°ã許容ããããªãã®ãªããç¬èªã«ããªãã¼ã·ã§ã³ãå®è£ ãã¦ããã§ãã¯å¦çãè¡ã£ã¦ãã ãããã¨ãããã¨ã§ãããã
ãã¨ã2023/1/1aã¨ããæ£å¸¸ãªæ¥ä»ã¨ããã¦ãã®ãåé¡ãªã®ã§ãããyyyy/MM/ddã®æ¥ä»ãã¿ã¼ã³ã§å¤å®ãã¦ãã«ãé¢ãããã2023/1/1ã¨ãææ¥ãä¸æ¡ã®æ¥ä»ã«ã¤ãã¦ãæ£å¸¸ã«ãªã£ã¦ãã¾ã£ã¦ãã®ãé§ç®ãªã®ã§ã¯ãªããã¨ã
ä¸å¿ãJavaã®æ¨æºAPIã®SimpleDataFormatã®ããã¥ã¡ã³ãã«ã¯ã
⧠ã¨èª¬æããããyyyy-MM-ddã¯ãæã¨æ¥ã¯ããããäºæ¡ã«ãªã£ã¦ãã®ã§ãï¼ä¸æ¡ã®ææ¥ã®å ´åã¯zero paddingããã¦äºæ¡ã«ãªã£ã¦ããã®ã§ãï¼
ãªã®ã§ãæ®å¿µãªãããJava8ã使ããªãç°å¢ã®å ´åã¯ãæ¥ä»ã®è§£æãå®æ½ãããåã«ä¸æ£ãªæ¥ä»ãå¼¾ãå¦çãå¿ è¦ã«ãªã£ã¦ãããã
ã§ãJava8ã使ããç°å¢ã§ããã°ãJava8ã§å°å ¥ãããDate and Time API ï¼JSR-310ï¼ã使ã£ã¦ããã®ãè¯ããããã®ã§ããã
⧠åæ¦ãèæ ®ããªãã¦ãè¯ãã®ã§ããã°ãå¹´ææ¥ã®å¹´ã®é¨åã®ãã¿ã¼ã³ã¨ãã¦ã¯ããuuuuãã使ã£ã¦ããã®ãç¡é£ã¨ãããã¨ã¿ããã
åæ¦ãªã©ã®å¯¾å¿ã¯ã
⧠ä¸è¨ãµã¤ãæ§ã詳ããã§ãã
ã¨ãããã西æ¦ã§å³å¯ã«æ¥ä»è§£æããã«ã¯ããuuuuãæå®ãå¿ é ã«ãªãã£ã½ãã
ã¨ãããã§æ¼¸ããè¬ã®ãuuuuããåºã¦ããã®ã§ããããã®ãuuuuãã¯ä½è ãªã®ãï¼
ã§ãJavaã®æ¨æºã®APIã®ããã¥ã¡ã³ãã§ã
⧠Java8ã§å°å ¥ãããDate and Time API ï¼JSR-310ï¼ã®ä¸ã§ãjava.time.format.DateTimeFormatterã¨ãããã®ãããã®ã§ããããã®ããã¥ã¡ã³ãã§ãuãã®è¨è¼ãããã¾ãã¨ã
ã¨ããã®ãã java.time.format.DateTimeFormatterã®ããã¥ã¡ã³ãã¨java.text.SimpleDateFormatã®ããã¥ã¡ã³ããè¦æ¯ã¹ã¦ãã®ã ãã©ã
â https://docs.oracle.com/javase/jp/8/docs/api/java/time/format/DateTimeFormatter.html
Symbol Meaning Presentation Examples ------ ------- ------------ ------- G era text AD; Anno Domini; A u year year 2004; 04 y year-of-era year 2004; 04 D day-of-year number 189 M/L month-of-year number/text 7; 07; Jul; July; J d day-of-month number 10 Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter Y week-based-year year 1996; 96 w week-of-week-based-year number 27 W week-of-month number 4 E day-of-week text Tue; Tuesday; T e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T F week-of-month number 3 a am-pm-of-day text PM h clock-hour-of-am-pm (1-12) number 12 K hour-of-am-pm (0-11) number 0 k clock-hour-of-am-pm (1-24) number 0 H hour-of-day (0-23) number 0 m minute-of-hour number 30 s second-of-minute number 55 S fraction-of-second fraction 978 A milli-of-day number 1234 n nano-of-second number 987654321 N nano-of-day number 1234000000 V time-zone ID zone-id America/Los_Angeles; Z; -08:30 z time-zone name zone-name Pacific Standard Time; PST O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00; X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15; x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15; Z zone-offset offset-Z +0000; -0800; -08:00; p pad next pad modifier 1 ' escape for text delimiter '' single quote literal ' [ optional section start ] optional section end # reserved for future use { reserved for future use } reserved for future use
â https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
Letter Date or Time Component Presentation Examples G Era designator Text AD y Year Year 1996; 96 Y Week year Year 2009; 09 M Month in year (context sensitive) Month July; Jul; 07 L Month in year (standalone form) Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day name in week Text Tuesday; Tue u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1 a Am/pm marker Text PM H Hour in day (0-23) Number 0 k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 h Hour in am/pm (1-12) Number 12 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time zone Pacific Standard Time; PST; GMT-08:00 Z Time zone RFC 822 time zone -0800 X Time zone ISO 8601 time zone -08; -0800; -08:00
⧠java.time.format.DateTimeFormatterã®ããã¥ã¡ã³ãã§ã¯ãå¹´ã表ç¾ãããuããåå¨ãããã ãã©ãjava.text.SimpleDateFormatã®ããã¥ã¡ã³ãã«ã¯å¹´ã表ç¾ãããuããåå¨ããªãã£ã½ãã®ããã
話ãè±ç·ãã¾ãããã試ãã«ãjava.time.format.DateTimeFormatterã§å¹´ã®é¨åã®ãã¿ã¼ã³ã«ãyyyyããæå®ãã¦ã¿ãã¨ããã
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.ResolverStyle; public class CheckDateFormat { public static void main(String[] args) { // String[] testDateStrList = { "2023/1/1", "2023/1/11", "2023/11/1", "2023/11/11", "2023/01/1", "2023/1/01", "2023/01/01", "2023/02/30", "2023/1/1a", "2023/111/111", "20230/1/1", "2023/1/1/" }; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 // for (int index = 0; index < testDateStrList.length; index++) { // System.out.print(index + ", "); // System.out.print(testDateStrList[index] + ", "); // System.out.println(checkDate(testDateStrList[index])); // } System.out.println("â Date and Time API"); for (int index = 0; index < testDateStrList.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList[index] + ", "); System.out.println(isCorrectDate(testDateStrList[index])); } } private static boolean checkDate(String strDate) { DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); df.setLenient(false); try { df.parse(strDate); } catch (ParseException e) { // ä¸æ£ãªæ¥ä» return false; } return true; } private static boolean isCorrectDate(String strDate) { try { DateTimeFormatter.ofPattern("yyyy/MM/dd") .withResolverStyle(ResolverStyle.STRICT) .parse(strDate, LocalDate::from); } catch (Exception e) { // TODO èªåçæããã catch ããã㯠return false; } return true; } }
⧠ä¸è¨ãå®è¡ããã¨ããã
è»ä¸¦ã¿æ¥ä»ã®è§£æã§ãã¼ã¹ã¨ã©ã¼ã«ãªãã£ã½ã...
æ¥ä»ã®ãã¿ã¼ã³ã®å¹´ã®é¨åããuuuuãã«ããã¨ããã
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.ResolverStyle; public class CheckDateFormat { public static void main(String[] args) { // String[] testDateStrList = { "2023/1/1", "2023/1/11", "2023/11/1", "2023/11/11", "2023/01/1", "2023/1/01", "2023/01/01", "2023/02/30", "2023/1/1a", "2023/111/111", "20230/1/1", "2023/1/1/" }; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 // for (int index = 0; index < testDateStrList.length; index++) { // System.out.print(index + ", "); // System.out.print(testDateStrList[index] + ", "); // System.out.println(checkDate(testDateStrList[index])); // } System.out.println("â Date and Time API"); for (int index = 0; index < testDateStrList.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList[index] + ", "); System.out.println(isCorrectDate(testDateStrList[index])); } } private static boolean checkDate(String strDate) { DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); df.setLenient(false); try { df.parse(strDate); } catch (ParseException e) { // ä¸æ£ãªæ¥ä» return false; } return true; } private static boolean isCorrectDate(String strDate) { try { DateTimeFormatter.ofPattern("uuuu/MM/dd") .withResolverStyle(ResolverStyle.STRICT) .parse(strDate, LocalDate::from); } catch (Exception e) { // TODO èªåçæããã catch ããã㯠return false; } return true; } }
⧠æ£ç¢ºã«è§£æã§ãã¦ã模æ§ã
Java8ã§å°å ¥ãããDate and Time API ï¼JSR-310ï¼ãå©ç¨ãã¦ãå³å¯ãªæ¥ä»ã®è§£æãããã«ã¯ãå¹´ã®ãã¿ã¼ã³ã«ã¯ãuuuuãã使ããããå¾ãªãããã§ããã¤ã¾ããé¸æã®ä½å°ãç¡ãã£ãã¨ããã...
ã¨ãããã¨ã¯ãæ¢åã®ã·ã¹ãã ã§ãyyyyããå©ç¨ãã¦ãåæãå¤ãã¨ãªãã¡ã¯ã¿ãªã³ã°ãé£ããæ°ããã...
ããããã¨ãã¦ãSimpleDataFormatã§ã¯ãå¹´ã®ãã¿ã¼ã³ã«ãuuuuããå©ç¨ã§ããªãã£ã½ãã...
ãªã®ã§ãããªãç¡çããæãããããããã¾ããããSimpleDataFormatã使ããããå¾ãªãå ´åã¯ã
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CheckDateFormat { public static void main(String[] args) { // String[] testDateStrList = { "2023/1/1", "2023/1/11", "2023/11/1", "2023/11/11", "2023/01/1", "2023/1/01", "2023/01/01", "2023/02/30", "2023/1/1a", "2023/111/111", "20230/1/1", "2023/1/1/", "2023/1/001", "2023/001/1", "23/1/01" }; String dateFormat = "yyyy/MM/dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList[index] + ", "); System.out.println(checkDate(testDateStrList[index], dateFormat)); } String[] testDateStrList02 = { "2023-1-1", "2023-1-11", "2023-11-1", "2023-11-11", "2023-01-1", "2023-1-01", "2023-01-01", "2023-02-30", "2023-1-1a", "2023-111-111", "20230-1-1", "2023-1-1-", "2023-1-001", "2023-001-1", "23-1-01" }; System.out.println("â ãã¤ãã³åºåã"); String dateFormat02 = "yyyy-MM-dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList02.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList02[index] + ", "); System.out.println(checkDate(testDateStrList02[index], dateFormat02)); } // System.out.println("â Date and Time API"); // for (int index = 0; index < testDateStrList.length; index++) { // System.out.print(index + ", "); // System.out.print(testDateStrList[index] + ", "); // System.out.println(isCorrectDate(testDateStrList[index])); // } } private static boolean checkDate(String strDate, String dateFormat) { // ã¤ã³ããããã§ã㯠if (Objects.isNull(strDate) || strDate.isEmpty() || Objects.isNull(dateFormat) || dateFormat.isEmpty()) { return false; } // ã¤ã³ãããã®æ¥ä»ããåè§æ°åã®ã¿æ½åºããé·ã int dateLength = strDate.replaceAll("[^0-9]", "").length(); // æ¥ä»ãã©ã¼ãããããã¢ã«ãã¡ãããé¨åã®ã¿æ½åºããé·ã int dateFormatLength = dateFormat.replaceAll("[^a-zA-Z]", "").length(); // ã¤ã³ãããã®æ¥ä»ãæ¥ä»ã®ãã©ã¼ãããã¨åãæ¡æ°ãã¨ãå¹´ææ¥ã®å¤ãæ°åã®ã¿ããã§ã㯠if (strDate.length() != dateFormat.length() || dateLength != dateFormatLength) { return false; } // å¹´ã®æ¡æ°ãã§ã㯠String year = strDate.substring(0, 5); int correctYearLength = 4; // å¹´ææ¥ã®åºåãã¯ã¹ã©ãã·ã¥ããã¤ãã³ Pattern pattern = Pattern.compile("[\\-\\/]+"); Matcher matcher = pattern.matcher(year); while (matcher.find()) { try { if (matcher.start() != correctYearLength) { return false; } } catch (Exception e) { return false; } } // æ¥ä»ã®è§£æãè¡ã DateFormat df = new SimpleDateFormat(dateFormat); df.setLenient(false); try { df.parse(strDate); } catch (ParseException e) { // ä¸æ£ãªæ¥ä» return false; } return true; } // private static boolean isCorrectDate(String strDate) { // if (Objects.isNull(strDate) || strDate.isEmpty()) { // return false; // } // try { // DateTimeFormatter.ofPattern("uuuu/MM/dd") // .withResolverStyle(ResolverStyle.STRICT) // .parse(strDate, LocalDate::from); // } catch (Exception e) { // // TODO èªåçæããã catch ããã㯠// return false; // } // return true; // } }
⧠ä¸è¨ãå®è¡ããã¨ã
⧠年ã«ã¤ãã¦ã¯4æ¡ããã§ãã¯ãããã¨ã¯ã§ããã®ã§ãããææ¥ã®æ¹ãæ¡æ°ã®ãã§ãã¯ãå¿ è¦ã¨ãããã¨ãå¤æããããã¨ãã...
ããã«é å¼µã£ã¦ã¿ããã©ã
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Objects; public class CheckDateFormat { public static void main(String[] args) { // String[] testDateStrList = { "2023/1/1", "2023/1/11", "2023/11/1", "2023/11/11", "2023/01/1", "2023/1/01", "2023/01/01", "2023/02/30", "2023/1/1a", "2023/111/111", "20230/1/1", "2023/1/1/", "2023/1/001", "2023/001/1", "23/1/01", "0023/01/01" }; String dateFormat = "yyyy/MM/dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList[index] + ", "); System.out.println(checkDate(testDateStrList[index], dateFormat)); } String[] testDateStrList02 = { "2023-1-1", "2023-1-11", "2023-11-1", "2023-11-11", "2023-01-1", "2023-1-01", "2023-01-01", "2023-02-30", "2023-1-1a", "2023-111-111", "20230-1-1", "2023-1-1-", "2023-1-001", "2023-001-1", "23-1-01", "0023-01-01" }; System.out.println("â ãã¤ãã³åºåã"); String dateFormat02 = "yyyy-MM-dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList02.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList02[index] + ", "); System.out.println(checkDate(testDateStrList02[index], dateFormat02)); } // System.out.println("â Date and Time API"); // for (int index = 0; index < testDateStrList.length; index++) { // System.out.print(index + ", "); // System.out.print(testDateStrList[index] + ", "); // System.out.println(isCorrectDate(testDateStrList[index])); // } } private static boolean checkDate(String strDate, String dateFormat) { // ã¤ã³ããããã§ã㯠if (Objects.isNull(strDate) || strDate.isEmpty() || Objects.isNull(dateFormat) || dateFormat.isEmpty()) { return false; } // ã¤ã³ãããã®æ¥ä»ããåè§æ°åã®ã¿æ½åºããé·ã int dateLength = strDate.replaceAll("[^0-9]", "").length(); // æ¥ä»ãã©ã¼ãããããã¢ã«ãã¡ãããé¨åã®ã¿æ½åºããé·ã int dateFormatLength = dateFormat.replaceAll("[^a-zA-Z]", "").length(); // ã¤ã³ãããã®æ¥ä»ãæ¥ä»ã®ãã©ã¼ãããã¨åãæ¡æ°ãã¨ãå¹´ææ¥ã®å¤ãæ°åã®ã¿ããã§ã㯠if (strDate.length() != dateFormat.length() || dateLength != dateFormatLength) { return false; } // å¹´ã®æ¡æ°ãã§ã㯠// String year = strDate.substring(0, 5); // int correctYearLength = 4; // // // å¹´ææ¥ã®åºåãã¯ã¹ã©ãã·ã¥ããã¤ãã³ // Pattern pattern = Pattern.compile("[\\-\\/]+"); // Matcher matcher = pattern.matcher(year); // while (matcher.find()) { // try { // if (matcher.start() != correctYearLength) { // return false; // } // } catch (Exception e) { // return false; // } // } String year = strDate.substring(0, 4); int correctYearLength = 4; if (!year.matches("^[0-9]+$") || year.length() != correctYearLength) { return false; } // æã®æ¡æ°ãã§ã㯠String month = strDate.substring(5, 7); int correctMonthLength = 2; if (!month.matches("^[0-9]+$") || month.length() != correctMonthLength) { return false; } // æ¥ã®æ¡æ°ãã§ã㯠String day = strDate.substring(8, 10); int correctDayLength = 2; if (!day.matches("^[0-9]+$") || day.length() != correctDayLength) { return false; } // æ¥ä»ã®è§£æãè¡ã DateFormat df = new SimpleDateFormat(dateFormat); df.setLenient(false); try { df.parse(strDate); } catch (ParseException e) { // ä¸æ£ãªæ¥ä» return false; } return true; } // private static boolean isCorrectDate(String strDate) { // if (Objects.isNull(strDate) || strDate.isEmpty()) { // return false; // } // try { // DateTimeFormatter.ofPattern("uuuu/MM/dd") // .withResolverStyle(ResolverStyle.STRICT) // .parse(strDate, LocalDate::from); // } catch (Exception e) { // // TODO èªåçæããã catch ããã㯠// return false; // } // return true; // } }
⧠zero paddingãããå¹´ã«ã¤ãã¦ã¯ãæ£å¸¸ãªæ¥ä»ã¨ãªãã¾ãããã¾ããåé¡ãªããã¨ã
æ®å¿µãªãããSimpleDataFormatã®ã¿ã§å³å¯ã«æ¥ä»ã®è§£æãè¡ãã«ã¯é£ããã¨ãããã¨ã§ããããã...
ã¡ãªã¿ã«ãJava8ã§å°å ¥ãããDate and Time API ï¼JSR-310ï¼ã®LocalDateã®parseã¡ã½ããã§ãResolverStyle.STRICTãæå®ãã¦ããzero paddingãããå¹´ã«ã¤ãã¦ã¯æ£å¸¸ãªæ¥ä»ã¨ããã¾ããã
許容ããæ¥ä»ã®ãã©ã¼ãããã¯ãéçºããã·ã¹ãã ã®è¦ä»¶ã§ä»æ§æ¬¡ç¬¬ã¨ãããã決ãã®åé¡ã«ãªã£ã¦ãããã§ãããï¼
æå ¥åããããã¡ã¤ã«ã®å¤ã¨ãã¯ãå ãè§ãWebã·ã¹ãã ãªããã®ç»é¢ããå ¥åãããå¤ã«ã¤ãã¦ã¯ãããã³ãã¨ã³ãå´ã§ããªãã¼ã·ã§ã³ãã¦ãããã®ãä¸ã¤ã®æã§ããããã
ãã ãstackoverflowãè¦ãéãã
⧠ããã³ãã¨ã³ãå´ã§ãã©ã¤ãã©ãªã使ããªãå ´åã¯ãæ£è¦è¡¨ç¾ãªã©ã§æç´ã«ããªãã¼ã·ã§ã³ãã¦ãæãã§ã¯ããã£ã½ã...
JavaScriptã ã¨ã
⧠zero paddingãããå¹´ã¯è¨±å®¹ããªãåãã«ãªããã¨ããããã ã¨ãã
ããã³ãã¨ã³ãã¨ãµã¼ãã¼ãµã¤ãã§å¥ã ã«éçºãã¦ãå ´åã¯ããã®ãããã®ä½ã許容ãããã©ããã®èªèåããããã¦ããã®ãå¿ è¦ããã§ãããã
ã¡ãªã¿ã«ã
⧠Javaã®SimpleDateFormatã«ã¯ä¸è¨ã®ãããªç½ ãããããã§ãã
ãã ã
⧠éå¹´ã«ã¤ãã¦ã¯ã対å¿ãã¦ãããããã§ããæ¥ä»ãã¿ã¼ã³ã§å¹´ã¾ã§æå®ããå¿ è¦ã¯ããã¾ããã
2023å¹´1æ30æ¥ï¼æï¼è¿½è¨ï¼â ãããã
ISO 8601ã®åå¨ã失念ãã¦ãã¾ããã
ISO 8601ã¯ãæ¥ä»ã¨æå»ã®è¡¨è¨ã«é¢ããISOã®å½éè¦æ ¼ã§ããããã®è¦æ ¼ã®ä¸»ç¼ã¯ãæ¥ä»ã¨æå»ã®è¨è¿°é åºãå½ãæåã«ãã£ã¦ã¾ã¡ã¾ã¡ã§ãããã®ãã大âå°ã®é åºï¼ããã°ã¨ã³ãã£ã¢ã³Â big-endianï¼ã貫徹ãã¦ãæ¥ä»ã»æå»ã®è¨è¿°é åºããã ä¸ç¨®é¡ã«æ¨æºåãã¦ãããã¨ã«ããã
ã¾ããæå»è¡¨ç¾ã¯24æå¶ã ãã«éå®ãã¦ããã
- 2017å¹´7æ17æ¥ãã2017-07-17ï¼æ¡å¼µå½¢å¼ï¼ãããã¯20170717ï¼åºæ¬å½¢å¼ï¼ã¨è¡¨è¨ããã
- åæ¥ã®æå»ã¨ã㦠17æ07å17.77ç§ ãä½µãã¦è¡¨è¨ããå ´åã¯ã2017-07-17T17:07:17.77ï¼æ¡å¼µå½¢å¼ï¼ãããã¯20170717T170717.77ï¼åºæ¬å½¢å¼ï¼ã¨è¡¨è¨ãããããªãã¡è¨å·Â T ã§åºåã£ãå¾ã«æå»ãç¶ããã
⧠ããã¤ã®ããã§ãTã¨ãä½è¨ãªãã®ãå ¥ã£ã¦ããããã§ãæ°åã ãã®æ¡æ°ã®æ¯è¼ãå°é£ã«...
ISO 8601ã®åå¨ãèæ ®ããã¨ãå¹´ææ¥ã®æ¡æ°ãã§ãã¯ãé£ãã...
ããªãå¼·å¼ã ãã©ãå®è£ ãã¦ã¿ãã
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Objects; public class CheckDateFormat { public static void main(String[] args) { // String[] testDateStrList = { "2023/1/1", "2023/1/11", "2023/11/1", "2023/11/11", "2023/01/1", "2023/1/01", "2023/01/01", "2023/02/30", "2023/1/1a", "2023/111/111", "20230/1/1", "2023/1/1/", "2023/1/001", "2023/001/1", "23/1/01", "0023/01/01" }; String dateFormat = "yyyy/MM/dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList[index] + ", "); System.out.println(checkDate(testDateStrList[index], dateFormat)); } System.out.println("â ãã¤ãã³åºåã"); String[] testDateStrList02 = { "2023-1-1", "2023-1-11", "2023-11-1", "2023-11-11", "2023-01-1", "2023-1-01", "2023-01-01", "2023-02-30", "2023-1-1a", "2023-111-111", "20230-1-1", "2023-1-1-", "2023-1-001", "2023-001-1", "23-1-01", "0023-01-01", }; String dateFormat02 = "yyyy-MM-dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList02.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList02[index] + ", "); System.out.println(checkDate(testDateStrList02[index], dateFormat02)); } System.out.println("â ISO 8601"); String[] testDateStrList03 = { "2017-07-17T17:07:17.77", "2017-007-17T17:07:17.77", "2017-07-017T17:07:17.77", "20170-07-017T17:07:17.77", "2017-07-1TT17:07:17.77", "2017-07-1T17:07:17.77", "201a-07-17T17:07:17.77", "2017-0a-17T17:07:17.77", "2017-07-1aT17:07:17.77" }; String dateFormat03 = "yyyy-MM-dd'T'HH:mm:ss.SSS"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList03.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList03[index] + ", "); System.out.println(checkDate(testDateStrList03[index], dateFormat03)); } System.out.println("â å¹´ææ¥ã¨æå»ãã¹ãã¼ã¹åºåã"); String[] testDateStrList04 = { "2017-07-17 17:07:17", "2017-007-17 17:07:17", "2017-07-017 17:07:17", "20170-07-17 17:07:17", "2017-07-17T 17:07:17", "2017-07-1T 17:07:17", "201a-07-17 17:07:17", "2017-0a-17 17:07:17", "2017-07-1a 17:07:17" }; String dateFormat04 = "yyyy-MM-dd HH:mm:ss"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList04.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList04[index] + ", "); System.out.println(checkDate(testDateStrList04[index], dateFormat04)); } // System.out.println("â Date and Time API"); // for (int index = 0; index < testDateStrList.length; index++) { // System.out.print(index + ", "); // System.out.print(testDateStrList[index] + ", "); // System.out.println(isCorrectDate(testDateStrList[index])); // } } private static boolean checkDate(String strDate, String dateFormat) { // ã¤ã³ããããã§ã㯠if (Objects.isNull(strDate) || strDate.isEmpty() || Objects.isNull(dateFormat) || dateFormat.isEmpty()) { return false; } // // æ¥ä»ããåè§æ°åã®ã¿æ½åºããé·ã // int dateLength = strDate.replaceAll("[^0-9]", "").length(); // // æ¥ä»ãã©ã¼ãããããã¢ã«ãã¡ãããé¨åã®ã¿æ½åºããé·ã // int dateFormatLength = dateFormat.replaceAll("[^a-zA-Z]", "").length(); // // ã¤ã³ãããã®æ¥ä»ãæ¥ä»ã®ãã©ã¼ãããã¨åãæ¡æ°ãã¨ãå¹´ææ¥ã®å¤ãæ°åã®ã¿ããã§ã㯠// if (strDate.length() != dateFormat.length() // || dateLength != dateFormatLength) { // return false; // } // å¹´ã®é·ããã§ã㯠// String year = strDate.substring(0, 5); // int correctYearLength = 4; // // // å¹´ææ¥ã®åºåãã¯ã¹ã©ãã·ã¥ããã¤ãã³ // Pattern pattern = Pattern.compile("[\\-\\/]+"); // Matcher matcher = pattern.matcher(year); // while (matcher.find()) { // try { // if (matcher.start() != correctYearLength) { // return false; // } // } catch (Exception e) { // return false; // } // } String tmpFormat = dateFormat.replaceAll("'", ""); // ISO 8601対çããã¨ãå¹´ææ¥ã¨æå»ãã¹ãã¼ã¹åºåãã«ãªã£ã¦ãå ´åã®å¯¾å¿ String[] strDateArr = strDate.split("[\\s | T]"); String[] strDateFromatArr = tmpFormat.split("[\\s | T]"); if (strDateArr.length != strDateFromatArr.length) { return false; } for (int index = 0; index < strDateArr.length; index++) { // å¹´ãæãæ¥ãã®ã¿ãã§ã㯠if (strDateFromatArr[index].toUpperCase().contains("YYYY") || strDateFromatArr[index].contains("MM") || strDateFromatArr[index].toUpperCase().contains("DD")) { // æ¥ä»ã®åºåãæå String splitKey = null; if(strDate.contains("-")) { splitKey = "-"; } else if (strDate.contains("/")) { splitKey = "/"; } if (Objects.nonNull(splitKey)) { if(!checkDateValue(strDateFromatArr[index], strDateArr[index], splitKey)) { return false; } } } } // String year = strDate.substring(0, 4); // int correctYearLength = 4; // if (!year.matches("^[0-9]+$") || year.length() != correctYearLength) { // return false; // } // // // æã®æ¡æ°ãã§ã㯠// String month = strDate.substring(5, 7); // int correctMonthLength = 2; // if (!month.matches("^[0-9]+$") || month.length() != correctMonthLength) { // return false; // } // // // æ¥ã®æ¡æ°ãã§ã㯠// String day = strDate.substring(8, 10); // int correctDayLength = 2; // if (!day.matches("^[0-9]+$") || day.length() != correctDayLength) { // return false; // } // æ¥ä»ã®è§£æãè¡ã DateFormat df = new SimpleDateFormat(dateFormat); df.setLenient(false); try { df.parse(strDate); } catch (ParseException e) { // ä¸æ£ãªæ¥ä» return false; } return true; } // private static boolean isCorrectDate(String strDate) { // if (Objects.isNull(strDate) || strDate.isEmpty()) { // return false; // } // try { // DateTimeFormatter.ofPattern("uuuu/MM/dd") // .withResolverStyle(ResolverStyle.STRICT) // .parse(strDate, LocalDate::from); // } catch (Exception e) { // // TODO èªåçæããã catch ããã㯠// return false; // } // return true; // } // å¹´ææ¥ã®æ¡æ°ãã§ã㯠private static boolean checkDateValue(String strDateFormat, String strDate, String splitKey) { // ã¤ã³ãããã®ãã§ã㯠if (Objects.isNull(strDateFormat) || strDateFormat.isEmpty() || Objects.isNull(strDate) || strDate.isEmpty()) { return false; } // å¹´ã»æã»æ¥ã«åå² String[] splitDateArr = strDate.split(splitKey); String[] splitFormatArr = strDateFormat.split(splitKey); // ã¤ã³ãããã®æ¥ä»ã¨æ¥ä»ãã©ã¼ãããã®æ¡æ°ãã§ã㯠if (splitDateArr.length != splitFormatArr.length) { return false; } // å¹´ã»æã»æ¥ã®å¤ã®ãã§ã㯠for (int index = 0; index < splitDateArr.length; index++) { // æ¡æ°ãã§ã㯠if (splitDateArr[index].length() != splitFormatArr[index].length()) { return false; } // æ°åããã§ã㯠if (!splitDateArr[index].matches("^[0-9]+$")) { return false; } } return true; } }
ã¨ããããããã¤ãã³ãã¹ã©ãã·ã¥ã§åºåãããå¹´ææ¥ã«ã¤ãã¦ã¯ãæ¡æ°ãã§ãã¯ã§ãããã©ããyyyyMMddã¨ãã¿ããã«åºåãã®ç¡ãæ¥ä»ã¯ã©ãããããã...
ã¡ãªã¿ã«ã
⧠ãããã¨ãã§åºåããããã¨ããããããª...
⧠æ¥ä»ã®ãããåºåãã£ã¦ã欧米ã®ä»æ§ã£ã¦ãã¨ã§ãããï¼
ãããï¼ããªãªãï¼åºåãã追å ãã¾ããã
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Objects; public class CheckDateFormat { public static void main(String[] args) { // String[] testDateStrList = { "2023/1/1", "2023/1/11", "2023/11/1", "2023/11/11", "2023/01/1", "2023/1/01", "2023/01/01", "2023/02/30", "2023/1/1a", "2023/111/111", "20230/1/1", "2023/1/1/", "2023/1/001", "2023/001/1", "23/1/01", "0023/01/01" }; String dateFormat = "yyyy/MM/dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList[index] + ", "); System.out.println(checkDate(testDateStrList[index], dateFormat)); } System.out.println("â ãã¤ãã³åºåã"); String[] testDateStrList02 = { "2023-1-1", "2023-1-11", "2023-11-1", "2023-11-11", "2023-01-1", "2023-1-01", "2023-01-01", "2023-02-30", "2023-1-1a", "2023-111-111", "20230-1-1", "2023-1-1-", "2023-1-001", "2023-001-1", "23-1-01", "0023-01-01", }; String dateFormat02 = "yyyy-MM-dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList02.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList02[index] + ", "); System.out.println(checkDate(testDateStrList02[index], dateFormat02)); } System.out.println("â ISO 8601"); String[] testDateStrList03 = { "2017-07-17T17:07:17.77", "2017-007-17T17:07:17.77", "2017-07-017T17:07:17.77", "20170-07-017T17:07:17.77", "2017-07-1TT17:07:17.77", "2017-07-1T17:07:17.77", "201a-07-17T17:07:17.77", "2017-0a-17T17:07:17.77", "2017-07-1aT17:07:17.77" }; String dateFormat03 = "yyyy-MM-dd'T'HH:mm:ss.SSS"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList03.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList03[index] + ", "); System.out.println(checkDate(testDateStrList03[index], dateFormat03)); } System.out.println("â å¹´ææ¥ã¨æå»ãã¹ãã¼ã¹åºåã"); String[] testDateStrList04 = { "2017-07-17 17:07:17", "2017-007-17 17:07:17", "2017-07-017 17:07:17", "20170-07-17 17:07:17", "2017-07-17T 17:07:17", "2017-07-1T 17:07:17", "201a-07-17 17:07:17", "2017-0a-17 17:07:17", "2017-07-1a 17:07:17" }; String dateFormat04 = "yyyy-MM-dd HH:mm:ss"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList04.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList04[index] + ", "); System.out.println(checkDate(testDateStrList04[index], dateFormat04)); } System.out.println("â ããªãªãåºåã"); String[] testDateStrList05 = { "2023.01.30", "2023.001.30", "2023.01.030", "20230.01.30", "202a.01.30", "2023.0a.30", "2023.01.3a", }; String dateFormat05 = "yyyy.MM.dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList05.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList05[index] + ", "); System.out.println(checkDate(testDateStrList05[index], dateFormat05)); } // System.out.println("â Date and Time API"); // for (int index = 0; index < testDateStrList.length; index++) { // System.out.print(index + ", "); // System.out.print(testDateStrList[index] + ", "); // System.out.println(isCorrectDate(testDateStrList[index])); // } } private static boolean checkDate(String strDate, String dateFormat) { // ã¤ã³ããããã§ã㯠if (Objects.isNull(strDate) || strDate.isEmpty() || Objects.isNull(dateFormat) || dateFormat.isEmpty()) { return false; } // // æ¥ä»ããåè§æ°åã®ã¿æ½åºããé·ã // int dateLength = strDate.replaceAll("[^0-9]", "").length(); // // æ¥ä»ãã©ã¼ãããããã¢ã«ãã¡ãããé¨åã®ã¿æ½åºããé·ã // int dateFormatLength = dateFormat.replaceAll("[^a-zA-Z]", "").length(); // // ã¤ã³ãããã®æ¥ä»ãæ¥ä»ã®ãã©ã¼ãããã¨åãæ¡æ°ãã¨ãå¹´ææ¥ã®å¤ãæ°åã®ã¿ããã§ã㯠// if (strDate.length() != dateFormat.length() // || dateLength != dateFormatLength) { // return false; // } // å¹´ã®é·ããã§ã㯠// String year = strDate.substring(0, 5); // int correctYearLength = 4; // // // å¹´ææ¥ã®åºåãã¯ã¹ã©ãã·ã¥ããã¤ãã³ // Pattern pattern = Pattern.compile("[\\-\\/]+"); // Matcher matcher = pattern.matcher(year); // while (matcher.find()) { // try { // if (matcher.start() != correctYearLength) { // return false; // } // } catch (Exception e) { // return false; // } // } String tmpFormat = dateFormat.replaceAll("'", ""); // ISO 8601対ç String[] strDateArr = strDate.split("[\\s | T]"); String[] strDateFromatArr = tmpFormat.split("[\\s | T]"); if (strDateArr.length != strDateFromatArr.length) { return false; } for (int index = 0; index < strDateArr.length; index++) { // å¹´ãæãæ¥ãã®ã¿ãã§ã㯠if (strDateFromatArr[index].toUpperCase().contains("YYYY") || strDateFromatArr[index].contains("MM") || strDateFromatArr[index].toUpperCase().contains("DD")) { // æ¥ä»ã®åºåãæå String splitKey = null; if(strDate.contains("-")) { splitKey = "-"; } else if (strDate.contains("/")) { splitKey = "/"; } else if (strDate.contains(".")) { splitKey = "\\."; } if (Objects.nonNull(splitKey)) { if(!checkDateValue(strDateFromatArr[index], strDateArr[index], splitKey)) { return false; } } } } // String year = strDate.substring(0, 4); // int correctYearLength = 4; // if (!year.matches("^[0-9]+$") || year.length() != correctYearLength) { // return false; // } // // // æã®æ¡æ°ãã§ã㯠// String month = strDate.substring(5, 7); // int correctMonthLength = 2; // if (!month.matches("^[0-9]+$") || month.length() != correctMonthLength) { // return false; // } // // // æ¥ã®æ¡æ°ãã§ã㯠// String day = strDate.substring(8, 10); // int correctDayLength = 2; // if (!day.matches("^[0-9]+$") || day.length() != correctDayLength) { // return false; // } // æ¥ä»ã®è§£æãè¡ã DateFormat df = new SimpleDateFormat(dateFormat); df.setLenient(false); try { df.parse(strDate); } catch (ParseException e) { // ä¸æ£ãªæ¥ä» return false; } return true; } // private static boolean isCorrectDate(String strDate) { // if (Objects.isNull(strDate) || strDate.isEmpty()) { // return false; // } // try { // DateTimeFormatter.ofPattern("uuuu/MM/dd") // .withResolverStyle(ResolverStyle.STRICT) // .parse(strDate, LocalDate::from); // } catch (Exception e) { // // TODO èªåçæããã catch ããã㯠// return false; // } // return true; // } // å¹´ææ¥ã®æ¡æ°ãã§ã㯠private static boolean checkDateValue(String strDateFormat, String strDate, String splitKey) { // ã¤ã³ãããã®ãã§ã㯠if (Objects.isNull(strDateFormat) || strDateFormat.isEmpty() || Objects.isNull(strDate) || strDate.isEmpty()) { return false; } // å¹´ã»æã»æ¥ã«åå² String[] splitDateArr = strDate.split(splitKey); String[] splitFormatArr = strDateFormat.split(splitKey); // ã¤ã³ãããã®æ¥ä»ã¨æ¥ä»ãã©ã¼ãããã®æ¡æ°ãã§ã㯠if (splitDateArr.length != splitFormatArr.length) { return false; } // å¹´ã»æã»æ¥ã®å¤ã®ãã§ã㯠for (int index = 0; index < splitDateArr.length; index++) { // æ¡æ°ãã§ã㯠if (splitDateArr[index].length() != splitFormatArr[index].length()) { return false; } // æ°åããã§ã㯠if (!splitDateArr[index].matches("^[0-9]+$")) { return false; } } return true; } }
æ¥ä»ã®åºåãæåãçµ±ä¸ãã¦æ¬²ãããª...
ã¨è¨ããã
⧠æ¹ãã¦ãSimpleDateFormatã®ä¾ãè¦ãéããæ¡æ°ãã§ãã¯ãå³ããããªæ¥ä»ãã¿ã¼ã³ãå¤ããã ã...
ããããããã¤ã³ãããã®éã®æ¥ä»ãã¿ã¼ã³ã決ãããããªããã·ã³ãã«ãªæ¥ä»ãã¿ã¼ã³ã«çµ±ä¸ãããã¨ããã§ããª...
2023å¹´1æ30æ¥ï¼æï¼è¿½è¨ï¼âããã¾ã§
2023å¹´1æ31æ¥ï¼ç«ï¼è¿½è¨ï¼â ãããã
åºåããªãã®æ¥ä»ã®ãã§ãã¯å¦çã追å ãã¦ã¿ã¾ããã
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Objects; public class CheckDateFormat { public static void main(String[] args) { // String[] testDateStrList = { "2023/1/1", "2023/1/11", "2023/11/1", "2023/11/11", "2023/01/1", "2023/1/01", "2023/01/01", "2023/02/30", "2023/1/1a", "2023/111/111", "20230/1/1", "2023/1/1/", "2023/1/001", "2023/001/1", "23/1/01", "0023/01/01" }; String dateFormat = "yyyy/MM/dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList[index] + ", "); System.out.println(checkDate(testDateStrList[index], dateFormat)); } System.out.println("â ãã¤ãã³åºåã"); String[] testDateStrList02 = { "2023-1-1", "2023-1-11", "2023-11-1", "2023-11-11", "2023-01-1", "2023-1-01", "2023-01-01", "2023-02-30", "2023-1-1a", "2023-111-111", "20230-1-1", "2023-1-1-", "2023-1-001", "2023-001-1", "23-1-01", "0023-01-01", }; String dateFormat02 = "yyyy-MM-dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList02.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList02[index] + ", "); System.out.println(checkDate(testDateStrList02[index], dateFormat02)); } System.out.println("â ISO 8601"); String[] testDateStrList03 = { "2017-07-17T17:07:17.77", "2017-007-17T17:07:17.77", "2017-07-017T17:07:17.77", "20170-07-017T17:07:17.77", "2017-07-1TT17:07:17.77", "2017-07-1T17:07:17.77", "201a-07-17T17:07:17.77", "2017-0a-17T17:07:17.77", "2017-07-1aT17:07:17.77" }; String dateFormat03 = "yyyy-MM-dd'T'HH:mm:ss.SSS"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList03.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList03[index] + ", "); System.out.println(checkDate(testDateStrList03[index], dateFormat03)); } System.out.println("â å¹´ææ¥ã¨æå»ãã¹ãã¼ã¹åºåã"); String[] testDateStrList04 = { "2017-07-17 17:07:17", "2017-007-17 17:07:17", "2017-07-017 17:07:17", "20170-07-17 17:07:17", "2017-07-17T 17:07:17", "2017-07-1T 17:07:17", "201a-07-17 17:07:17", "2017-0a-17 17:07:17", "2017-07-1a 17:07:17" }; String dateFormat04 = "yyyy-MM-dd HH:mm:ss"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList04.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList04[index] + ", "); System.out.println(checkDate(testDateStrList04[index], dateFormat04)); } System.out.println("â ããªãªãåºåã"); String[] testDateStrList05 = { "2023.01.30", "2023.001.30", "2023.01.030", "20230.01.30", "202a.01.30", "2023.0a.30", "2023.01.3a", }; String dateFormat05 = "yyyy.MM.dd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList05.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList05[index] + ", "); System.out.println(checkDate(testDateStrList05[index], dateFormat05)); } System.out.println("â åºåããªãå¹´ææ¥"); String[] testDateStrList06 = { "20230130", "202300130", "202301030", "202300130", "202a0130", "20230a30", "2023013a", }; String dateFormat06 = "yyyyMMdd"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList06.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList06[index] + ", "); System.out.println(checkDate(testDateStrList06[index], dateFormat06)); } System.out.println("â åºåããªãå¹´ææ¥æåç§"); String[] testDateStrList07 = { "20230130215500", "20230013093300", "20230103083028", "20230013090005", "202a0130113030", "20230a30213045", "2023013a220000", }; String dateFormat07 = "yyyyMMddHHmmss"; // æ¥ä»ãã§ãã¯ã®å®æ½ã¨çµæã表示 for (int index = 0; index < testDateStrList07.length; index++) { System.out.print(index + ", "); System.out.print(testDateStrList07[index] + ", "); System.out.println(checkDate(testDateStrList07[index], dateFormat07)); } // System.out.println("â Date and Time API"); // for (int index = 0; index < testDateStrList.length; index++) { // System.out.print(index + ", "); // System.out.print(testDateStrList[index] + ", "); // System.out.println(isCorrectDate(testDateStrList[index])); // } } private static boolean checkDate(String strDate, String dateFormat) { // ã¤ã³ããããã§ã㯠if (Objects.isNull(strDate) || strDate.isEmpty() || Objects.isNull(dateFormat) || dateFormat.isEmpty()) { return false; } // // æ¥ä»ããåè§æ°åã®ã¿æ½åºããé·ã // int dateLength = strDate.replaceAll("[^0-9]", "").length(); // // æ¥ä»ãã©ã¼ãããããã¢ã«ãã¡ãããé¨åã®ã¿æ½åºããé·ã // int dateFormatLength = dateFormat.replaceAll("[^a-zA-Z]", "").length(); // // ã¤ã³ãããã®æ¥ä»ãæ¥ä»ã®ãã©ã¼ãããã¨åãæ¡æ°ãã¨ãå¹´ææ¥ã®å¤ãæ°åã®ã¿ããã§ã㯠// if (strDate.length() != dateFormat.length() // || dateLength != dateFormatLength) { // return false; // } // å¹´ã®é·ããã§ã㯠// String year = strDate.substring(0, 5); // int correctYearLength = 4; // // // å¹´ææ¥ã®åºåãã¯ã¹ã©ãã·ã¥ããã¤ãã³ // Pattern pattern = Pattern.compile("[\\-\\/]+"); // Matcher matcher = pattern.matcher(year); // while (matcher.find()) { // try { // if (matcher.start() != correctYearLength) { // return false; // } // } catch (Exception e) { // return false; // } // } String tmpFormat = dateFormat.replaceAll("'", ""); // ISO 8601対ç String[] strDateArr = strDate.split("[\\s | T]"); String[] strDateFromatArr = tmpFormat.split("[\\s | T]"); if (strDateArr.length != strDateFromatArr.length) { return false; } for (int index = 0; index < strDateArr.length; index++) { // å¹´ãæãæ¥ãã®ã¿ãã§ã㯠if (strDateFromatArr[index].toUpperCase().contains("YYYY") || strDateFromatArr[index].contains("MM") || strDateFromatArr[index].toUpperCase().contains("DD")) { // æ¥ä»ã®åºåãæå String splitKey = null; if(strDate.contains("-")) { splitKey = "-"; } else if (strDate.contains("/")) { splitKey = "/"; } else if (strDate.contains(".")) { splitKey = "\\."; } if (Objects.nonNull(splitKey)) { if(!checkDateValue(strDateFromatArr[index], strDateArr[index], splitKey)) { return false; } } else { if (!checkDateValueWithoutDelimiter(strDateFromatArr[index], strDateArr[index])) { return false; } } } } // String year = strDate.substring(0, 4); // int correctYearLength = 4; // if (!year.matches("^[0-9]+$") || year.length() != correctYearLength) { // return false; // } // // // æã®æ¡æ°ãã§ã㯠// String month = strDate.substring(5, 7); // int correctMonthLength = 2; // if (!month.matches("^[0-9]+$") || month.length() != correctMonthLength) { // return false; // } // // // æ¥ã®æ¡æ°ãã§ã㯠// String day = strDate.substring(8, 10); // int correctDayLength = 2; // if (!day.matches("^[0-9]+$") || day.length() != correctDayLength) { // return false; // } // æ¥ä»ã®è§£æãè¡ã DateFormat df = new SimpleDateFormat(dateFormat); df.setLenient(false); try { df.parse(strDate); } catch (ParseException e) { // ä¸æ£ãªæ¥ä» return false; } return true; } // private static boolean isCorrectDate(String strDate) { // if (Objects.isNull(strDate) || strDate.isEmpty()) { // return false; // } // try { // DateTimeFormatter.ofPattern("uuuu/MM/dd") // .withResolverStyle(ResolverStyle.STRICT) // .parse(strDate, LocalDate::from); // } catch (Exception e) { // // TODO èªåçæããã catch ããã㯠// return false; // } // return true; // } // å¹´ææ¥ã®æ¡æ°ãã§ã㯠private static boolean checkDateValue(String strDateFormat, String strDate, String splitKey) { // ã¤ã³ãããã®ãã§ã㯠if (Objects.isNull(strDateFormat) || strDateFormat.isEmpty() || Objects.isNull(strDate) || strDate.isEmpty()) { return false; } // å¹´ã»æã»æ¥ã«åå² String[] splitDateArr = strDate.split(splitKey); String[] splitFormatArr = strDateFormat.split(splitKey); // ã¤ã³ãããã®æ¥ä»ã¨æ¥ä»ãã©ã¼ãããã®æ¡æ°ãã§ã㯠if (splitDateArr.length != splitFormatArr.length) { return false; } // å¹´ã»æã»æ¥ã®å¤ã®ãã§ã㯠for (int index = 0; index < splitDateArr.length; index++) { // æ¡æ°ãã§ã㯠if (splitDateArr[index].length() != splitFormatArr[index].length()) { return false; } // æ°åããã§ã㯠if (!splitDateArr[index].matches("^[0-9]+$")) { return false; } } return true; } // åºåããªãæ¥ä»ã®ãã§ã㯠private static boolean checkDateValueWithoutDelimiter(String strDateFormat, String strDate) { // ã¤ã³ãããã®ãã§ã㯠if (Objects.isNull(strDateFormat) || strDateFormat.isEmpty() || Objects.isNull(strDate) || strDate.isEmpty()) { return false; } // æ¥ä»ãã¿ã¼ã³ã®æ¡æ°ã¨ã¤ã³ãããã®æ¥ä»ã®æ¡æ°ã®ãã§ã㯠if (strDateFormat.length() != strDate.length()) { return false; } final char charYear = 'Y'; final char charMonth = 'M'; final char charDay = 'D'; int countCharYear = 0; int countCharMonth = 0; int countCharDay = 0; // æ¥ä»ãã¿ã¼ã³ã®ãã§ã㯠for (int index = 0; index < strDateFormat.length(); index++) { // å¹´ã®ãã§ã㯠if (strDateFormat.toUpperCase().charAt(index) == charYear) { countCharYear++; } // æã®ãã§ã㯠if (strDateFormat.charAt(index) == charMonth) { countCharMonth++; } // æ¥ã®ãã§ã㯠if (strDateFormat.toUpperCase().charAt(index) == charDay) { countCharDay++; } } // å¹´ã®ãã§ã㯠if (countCharYear != 0) { int yearPosition = strDateFormat.toUpperCase().indexOf(String.valueOf(charYear)); String strYear = strDate.substring(yearPosition, yearPosition+countCharYear); if (!strYear.matches("^[0-9]{" + countCharYear + "}$")) { return false; } } // æã®ãã§ã㯠if (countCharMonth != 0) { int monthPosition = strDateFormat.indexOf(String.valueOf(charMonth)); String strMonth = strDate.substring(monthPosition, monthPosition+countCharMonth); if (!strMonth.matches("(0[1-9]|1[0-2])")) { return false; } } // æ¥ã®ãã§ã㯠if (countCharDay != 0) { int dayPosition = strDateFormat.toUpperCase().indexOf(String.valueOf(charDay)); String strDay = strDate.substring(dayPosition, dayPosition+countCharDay); if (!strDay.matches("(0[1-9]|[12][0-9]|3[01])")) { return false; } } return true; } }
⧠ã2023å¹´1æ31æ¥ãã¿ãããªæ¼¢åã§åºåããããæ¥ä»ã«ã¯å¯¾å¿ã§ãã¦ãªãã§ãã...
ãã¨ããyyyyMdãã¿ãããªææ¥ãä¸æ¡ã®æ¥ä»ãã¿ã¼ã³ã対å¿ã§ãã¦ãªãã§ã...
SimpleDateFormatã使ãã¨ä¸æ¯ãªéããå¼·ãããããã¨ã«ãªãã¨ãããã¨ã§ããã...
Java8ã§å°å ¥ãããDate and Time API ï¼JSR-310ï¼ã使ããç°å¢ã§ãããªã使ã£ãæ¹ãè¯ãæ°ããã¾ãã...
ã¨ãããããæ¥ä»ã®åºåãã¯ãã¤ãã³ã§çµ±ä¸ãã¦æ¬²ããããª...
2023å¹´1æ31æ¥ï¼ç«ï¼è¿½è¨ï¼â ããã¾ã§
æ¯åº¦ã¢ã¤ã¢ã¤æãå端ãªã...
ä»åã¯ãã®ã¸ãã§ã
Â
Â