[英]java.text.ParseException Unparsable Date: “0”
我正在尝试转换字符串
2010-03-09
安排日期,以进一步获得年度日期。
String dt = "2010-03-09";
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = new GregorianCalendar();
Date d = date.parse(dt);
cal.setTime(d);
int doy = cal.get(Calendar.DAY_OF_YEAR);
我不确定为什么会收到此错误
Exception in thread "main" java.text.ParseException: Unparseable date: "0"
我的参数SimpleDateFormat字符串模式有问题吗?
我的条件很严格,字符串格式为
2010-03-09
BC我有4000个这种格式的日期数组
任何帮助将不胜感激,我是Calendar,Date和SimpleDateFormat类的新手。
我正在处理数组,上面的示例是一个一般情况。 这是我正在处理的实际情况
public static int[] todoy(String[] dt) { // input array of String (dates) and will return array of DOY's
int[] re = new int[size];
Date d = null;
for (int i=0;i<size;i++){
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = new GregorianCalendar();
try {
d = date.parse(dt[i]);
} catch (ParseException e) {
e.printStackTrace();
}
cal.setTime(d);
re[i] = cal.get(Calendar.DAY_OF_YEAR);
}//fill in array of DOY's
return re;
}//inputs date such as 5/2 and returns integer for which day of the year it is
你能试试这个代码吗?
public static int[] todoy(String[] dt) { // input array of String (dates) and will return array of DOY's
int[] re = new int[size];
Date d = null;
if(dt == null) return null;
for (int i=0;i<size;i++){
if(dt[i] != null && dt[i] != "0") {
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = new GregorianCalendar();
try {
d = date.parse(dt[i]);
} catch (ParseException e) {
e.printStackTrace();
}
if(d != null ) {
cal.setTime(d);
re[i] = cal.get(Calendar.DAY_OF_YEAR);
}
}
}//fill in array of DOY's
return re;
}
您需要使用try-catch子句来包围parse
方法。 似乎其中一个条目的格式不正确。 当您发现该异常时,您可以检查原因到底是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.