[JAVA] Check when moss with SimpleDateFormat parse

Thu, 13 Jul 2017 18:00:15 +0900 In Java, do you want to convert a character string of this format to Date type?

Like this

public class Main {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
        System.out.println(sdf.parse("Thu, 13 Jul 2017 18:00:15 +0900"));
    }
}

It can be realized by using the parse method of SimpleDateFormat ... I wrote in various articles, but when I actually execute it

Exception in thread "main" java.text.ParseException: Unparseable date: "Thu, 13 Jul 2017 18:00:15 +0900"
	at java.text.DateFormat.parse(DateFormat.java:366)
	at Main.main(Main.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

An exception like this will occur.

So rewrite it like this.

public class Main {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
        System.out.println(sdf.parse("Thu, 13 Jul 2017 18:00:15 +0900"));
    }
}

On the third line, Locale is passed as the second argument of the SimpleDateFormat constructor.

This will give you the output Thu Jul 13 18:00:15 JST 2017.

Cause

I mentioned the cause in this article, so if you are interested, please have a look.

Check when moss with SimpleDateFormat parse # 60 --Yurufuwa Technical Diary

Recommended Posts

Check when moss with SimpleDateFormat parse
Check the actual date and time at parse with Java's SimpleDateFormat
(RubyMine) Check when you cannot debug with docker-compose
Things to check when it doesn't work with proguard
Sample code to parse date and time with Java SimpleDateFormat
Check CSV value with RSpec
Integer check method with ruby
Check performance quickly with irb
Error when playing with java
When SimpleDateFormat is garbled like ٢٠١٨١٠٠٤٠٨٣١٣٣٦٥٧
Exciting environment check with mkmf
Check with Mockito whether Method was called when Map → List (when, verify)