-3

您好我正在尝试使用以下代码我遇到解析异常并且需要知道如何处理

DateFormat formatter1 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = (Date)formatter1.parse(startDate);
Date date2 = (Date)formatter1.parse(endDate);               
DateFormat formatter2=new SimpleDateFormat("yyyy-MM-dd");
String startDate1=formatter2.format(date1);
String endDate1=formatter2.format(date2);

提前致谢

4

2 回答 2

1

尝试使用getErrorOffset()

DateFormat formatter1 = new SimpleDateFormat("dd/MM/yyyy");
try
{
   Date date1 = (Date)formatter1.parse(startDate);
   Date date2 = (Date)formatter1.parse(endDate);              
}catch(ParseException e)
{
  // Handle Exception, take a look at e.ErrorOffset to know where the problem occurs
  // if the Message doesnt give enough information
}            
DateFormat formatter2=new SimpleDateFormat("yyyy-MM-dd");            
String startDate1=formatter2.format(date1);             
String endDate1=formatter2.format(date2);
于 2012-08-29T06:07:30.110 回答
0

你需要把你的异常味精,

    Date date1 = null, date2= null;
    DateFormat formatter1 = new SimpleDateFormat("dd/MM/yyyy");


    try {
        date1 = (Date)formatter1.parse("08/01/1988");
        date2 = (Date)formatter1.parse("08/01/2000");
    } catch (ParseException ex) {
        //Handle Exception here
    }
    DateFormat formatter2=new SimpleDateFormat("yyyy-MM-dd");
    String startDate1=formatter2.format(date1);
    String endDate1=formatter2.format(date2);

    System.out.println(startDate1);
    System.out.println(endDate1);
于 2012-08-29T06:08:29.347 回答