0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Java の SimpleDateFormat は「Jun」でも「June」でも Parse してくれる

Posted at

SimpleDateFormatで「MMM」を指定すると、月の書き方が短縮系でもそうでなくてもParseしてくれる。

コード


public static void main(String[] args) throws Exception {

	{
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
		System.err.println(sdf.parse("01-June-2020"));// OK
	}
	{
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
		System.err.println(sdf.parse("01-Jun-2020"));// OK
	}
	{
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
		System.err.println(sdf.parse("01-September-2020"));// OK
	}
	{
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
		System.err.println(sdf.parse("01-Sep-2020")); // OK
	}
	{
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
		System.err.println(sdf.parse("01-Sept-2020")); // NG
	}

}


結果

Mon Jun 01 00:00:00 JST 2020
Mon Jun 01 00:00:00 JST 2020
Tue Sep 01 00:00:00 JST 2020
Tue Sep 01 00:00:00 JST 2020
Exception in thread "main" java.text.ParseException: Unparseable date: "01-Sept-2020"
	at java.text.DateFormat.parse(DateFormat.java:366)
	at hello.date.HelloSimpleDateFormat2.main(HelloSimpleDateFormat2.java:28)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?