Java异常处理

Java异常处理,java,parsing,exception,date-parsing,Java,Parsing,Exception,Date Parsing,嗨,我正在尝试使用下面的代码,我遇到了一个解析异常,需要知道如何处理 DateFormat formatter1 = new SimpleDateFormat("dd/MM/yyyy"); Date date1 = (Date)formatter1.parse(startDate); Date date2 = (Date)formatter1.parse(endDate); DateFormat formatter2=new SimpleDateFormat("yy

嗨,我正在尝试使用下面的代码,我遇到了一个解析异常,需要知道如何处理

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);
提前感谢

尝试使用:


你需要把你的异常信息

    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);

很高兴看到您的输入和异常消息您有一个解析异常,我们也可以有一个吗?startdate和enddate中有什么?我们几乎没有足够的上下文来知道您应该如何处理异常。这是用户输入吗?如果是这样,您应该向用户显示一条错误消息。例如,它是来自文件的系统输入吗?如果是这样,也许您应该忽略该记录,或者您应该完全中止该操作。对于异常处理,没有一刀切的方法。
    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);