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

just
  • 1,900
  • 4
  • 25
  • 46

2 Answers2

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);
JanPl
  • 794
  • 6
  • 19
  • I got parseexception like in my question – just May 04 '16 at 19:49
  • Post your stacktrace and your system environment. – JanPl May 04 '16 at 20:00
  • in android emulator this is working, but on my device not – just May 04 '16 at 20:08
  • What is your default locale? Add a statement to your unit test: Locale.getDefault() and see what it tells you. – JanPl May 04 '16 at 20:10
  • my default locale is hu_HU – just May 04 '16 at 20:21
  • I don't have hungarian locale on my system. You can try to put an explicit locale into the SimpleDateFormat: SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.US); – JanPl May 04 '16 at 21:11
  • If I set the locale to hungarian, I get the ParseException as well: Locale.forLanguageTag("HU") – JanPl May 04 '16 at 21:17
  • So, I set Locale.getDefault() to the simpledateformat. If the device language is hungarian I get parseexception, but if I set the device language to english everything is working.... – just May 05 '16 at 03:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111082/discussion-between-jan-and-user3275938). – JanPl May 05 '16 at 06:29
  • Did you recognize my code example? You get the ParseException when parsing an english date string with hungarian locale. – JanPl Jan 12 '17 at 09:29
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.

Community
  • 1
  • 1
Jlegend
  • 531
  • 1
  • 6
  • 19