0

I am converting the string which have '1520056800` to long to have date. But I am getting NumberFormatException to convert this

Please help me.

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

Stack trace

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)
5
  • 1
    I'm 100% sure you have some invalid non-printable (so you don't see it) character in your string, if i use long expiryDateMS = Long.parseLong("1520 056800 ".replaceAll(" ", "")); it works, so please check responseArray[0] contents Commented Sep 1, 2013 at 19:31
  • @BackSlash I have checked the content..its 1520056800 Commented Sep 1, 2013 at 19:32
  • that's my guess too! maybe some cr lf which you don't see Commented Sep 1, 2013 at 19:32
  • Maybe you should break your chained call into two statements so you can see what you're trying to parse. You don't save anything by combining them, you know. But you greatly increase debugging difficulty. Commented Sep 1, 2013 at 19:33
  • 1
    @Android re-check my comment, i said invalid non-printable (so you don't see it) Loop through your string and print every character. Or maybe just print responseArray[0].replaceAll(" ", "").length(), if it will output something greater than 10 you have invalid characters in it. Commented Sep 1, 2013 at 19:34

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

This code should do the job. So I'm guessing you have got some encoding/special character problem. Try this to verify you have a real ASCII encoded number:

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!");
}

3 Comments

I checked and str is 1520056800. I think every digit in this is valid digit
I am using this SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd"); then it diplaying the date 1970-44-18..Its wrong. Please help to fix it
Use yyyy-MM-dd instead. mm is minute. MM is month.

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.