Java11 DateTimeFormatterを利用して有効な日付をチェックするサンプル

環境
Java SE 11
Eclipse 4.26.0

構文
String 日付の変数名 =値;
DateTimeFormatter.ofPattern(“uuuuMMdd").withResolverStyle(ResolverStyle.STRICT).parse(日付の変数名,LocalDate::from);
DateTimeFormatterでは「yyyy」ではなく「uuuu」で指定します。
withResolverStyle(ResolverStyle.STRICT)を指定することで、有効な日付をチェックします。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
パターンは、ofPattern(String)メソッドおよびofPattern(String, Locale)メソッドを使用してFormatterを作成する場合に使用されます。
たとえば、"d MMM uuuu"を使用すると、2011-12-03は「3 Dec 2011」と書式設定されます。
パターンは、ofPattern(String)メソッドおよびofPattern(String, Locale)メソッドを使用してFormatterを作成する場合に使用されます。 たとえば、"d MMM uuuu"を使用すると、2011-12-03は「3 Dec 2011」と書式設定されます。
パターンは、ofPattern(String)メソッドおよびofPattern(String, Locale)メソッドを使用してFormatterを作成する場合に使用されます。
たとえば、"d MMM uuuu"を使用すると、2011-12-03は「3 Dec 2011」と書式設定されます。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
public class ArktestDemo {
public static void main(String[] args) {
System.out.println("yyyy-MM-dd形式:" + isFunDate("2023-02-29"));
System.out.println("yyyyMMdd形式:" + isFunDate("20230126"));
System.out.println("yyyy-M-d形式:" + isFunDate("2023-2-1"));
}
/**
* 日付チェック
* @param str
* @return true 存在する日付 false日付ではない
*/
public static boolean isFunDate(String str) {
boolean result = false;
if (str != null) {
try {
String checkDate = str.replace("-", "").replace("/", "");
DateTimeFormatter.ofPattern("uuuuMMdd").withResolverStyle(ResolverStyle.STRICT).parse(checkDate,
LocalDate::from);
result = true;
} catch (Exception e) {
result = false;
}
}
return result;
}
}
package com.arkgame.study; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.ResolverStyle; public class ArktestDemo { public static void main(String[] args) { System.out.println("yyyy-MM-dd形式:" + isFunDate("2023-02-29")); System.out.println("yyyyMMdd形式:" + isFunDate("20230126")); System.out.println("yyyy-M-d形式:" + isFunDate("2023-2-1")); } /** * 日付チェック * @param str * @return true 存在する日付 false日付ではない */ public static boolean isFunDate(String str) { boolean result = false; if (str != null) { try { String checkDate = str.replace("-", "").replace("/", ""); DateTimeFormatter.ofPattern("uuuuMMdd").withResolverStyle(ResolverStyle.STRICT).parse(checkDate, LocalDate::from); result = true; } catch (Exception e) { result = false; } } return result; } }
package com.arkgame.study;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;

public class ArktestDemo {

      public static void main(String[] args) {
            System.out.println("yyyy-MM-dd形式:" + isFunDate("2023-02-29"));
            System.out.println("yyyyMMdd形式:" + isFunDate("20230126"));
            System.out.println("yyyy-M-d形式:" + isFunDate("2023-2-1"));

      }

      /**
       * 日付チェック
       * @param str
       * @return true 存在する日付 false日付ではない
       */
      public static boolean isFunDate(String str) {
            boolean result = false;

            if (str != null) {
                  try {
                        String checkDate = str.replace("-", "").replace("/", "");
                        DateTimeFormatter.ofPattern("uuuuMMdd").withResolverStyle(ResolverStyle.STRICT).parse(checkDate,
                                    LocalDate::from);
                        result = true;

                  } catch (Exception e) {
                        result = false;
                  }
            }

            return result;
      }
}

実行結果
yyyy-MM-dd形式:false
yyyyMMdd形式:true
yyyy-M-d形式:false

Java

Posted by arkgame