简体   繁体   中英

Android SimpleDateFormat parse error

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

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM