Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm getting a NumberFormatException while executing the below statements.

Calendar cal = Calendar.getInstance();

int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day_of_month = 15;

long m_time = Long.parseLong((month + 1) + "/" + day_of_month + "/" + year);

and

long m_time = Long.parseLong(String.valueOf((month + 1) + "/" + day_of_month + "/" + year));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
234 views
Welcome To Ask or Share your Answers For Others

1 Answer

The reason for the NumberFormatException is cause you are trying to parseLong a String that is not a valid long representation: "2/15/2015"

To parse the date string you've come up with correctly use this code:

SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy");
Date date = format.parse(month + 1 + "/" + day_of_month + "/" + year);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...