0

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

1

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

10 Comments

I got parseexception like in my question
Post your stacktrace and your system environment.
in android emulator this is working, but on my device not
What is your default locale? Add a statement to your unit test: Locale.getDefault() and see what it tells you.
my default locale is hu_HU
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.