0
votes

I would like to parse this string: Thu Jan 01 00:00:58 CET 1970

I use this pattern: EEE MMM dd hh:mm:ss z yyyy

But I got this exception: java.text.ParseException: Unparseable date: "Thu Jan 01 00:00:58 CET 1970" (at offset 20)

stacktrace:

java.text.ParseException: Unparseable date: "Thu Jan 01 00:01:18 CET 1970" (at offset 20) W/System.err: at java.text.DateFormat.parse(DateFormat.java:571)

system env: android studio 2.0, compileSdkVersion 23, buildToolsVersion "23.0.3" device: HTC One M7, android 5.0.2

2

2 Answers

2
votes

You should create a test case and demonstrate the behavior. I did it for you:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.junit.Test;

public class DateParseTest {

@Test
public void testDateFormat() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.US);
    try {
        Date date = dateFormat.parse("Thu Jan 01 00:00:58 CET 1970");
        System.out.println("parsed date:" + date);
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
  }    
}

Use an explicit locale setting Locale.US. In your case hungarian is the default locale and you have to parse a date string in hungarian format.

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", new Locale("HU"));
String dateString = "P máj. 01 01:00:58 CET 1970";
Date date = dateFormatHu.parse(dateString);
0
votes

Please refer to this post Java Date(0) is not 1/1/1970.

All of the issues with the 1/1/1970 date are explained in full detail.