将日期解析为长整数时,数字格式异常 [英] Number format exception while parsing the date to long
本文介绍了将日期解析为长整数时,数字格式异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在执行以下语句时,我遇到了NumberFormatException.
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);
和
long m_time = Long.parseLong(String.valueOf((month + 1) + "/" + day_of_month + "/" + year));
推荐答案
NumberFormatException的原因是,您试图解析不是有效的长表示形式的字符串:"2/15/2015"
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);
这篇关于将日期解析为长整数时,数字格式异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文