0

我正在将具有“1520056800”的字符串转换为长日期。但我得到 NumberFormatException 来转换它

请帮我。

long expiryDateMS = Long.parseLong(responseArray[0].replaceAll(" ", ""));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date date = new Date(expiryDateMS);

堆栈跟踪

09-02 00:52:28.984: E/AndroidRuntime(12025): Caused by: java.lang.NumberFormatException: 1520056800
09-02 00:52:28.984: E/AndroidRuntime(12025):    at java.lang.Long.parse(Long.java:353)
09-02 00:52:28.984: E/AndroidRuntime(12025):    at java.lang.Long.parseLong(Long.java:344)
09-02 00:52:28.984: E/AndroidRuntime(12025):    at java.lang.Long.parseLong(Long.java:311)
09-02 00:52:28.984: E/AndroidRuntime(12025):    at com.example.astrill_openvpn.MainOnOffActivity.onCreate(MainOnOffActivity.java:99)
09-02 00:52:28.984: E/AndroidRuntime(12025):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-02 00:52:28.984: E/AndroidRuntime(12025):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
4

2 回答 2

3

You probably have invisible or invalid characters in the string. Try this post for more details.

于 2013-09-01T19:33:12.563 回答
0

这段代码应该可以完成这项工作。所以我猜你有一些编码/特殊字符问题。试试这个来验证你有一个真正的 ASCII 编码数字:

String str = responseArray[0].replaceAll(" ", "");
for (int i = 0; i < str.length(); ++i)
{
    char a = str.charAt(i);
    if (!('0' <= a && a <= '9')) System.out.println(a + " is not a valid digit!");
}
于 2013-09-01T19:32:56.577 回答