如果日期错误,请勿打印错误信息

时间:2014-10-08 12:27:54

标签: java

我想要回复消息" Date is very wrong"如果交易日期大于计算日期。相反,它只是打印" -1 days"。

import java.text.SimpleDateFormat;
import java.util.Date;

public class Age {

    public static long computeAge(String transDate, String computeDate){
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

        long lapseDays = 0;

        try {
            Date TDate = format.parse(transDate);
            Date CDate = format.parse(computeDate);

            long aging = CDate.getTime() - TDate.getTime();
            lapseDays = aging/(24*60*60*1000);

        }catch (Exception e){
            e.printStackTrace();
            System.out.print("Date is very wrong"); 
        }

        return lapseDays;
    }
    public static void main(String[] args) {

        String transaction = "10/01/2014";
        String computation = "09/30/2014";
        System.out.println(computeAge(transaction, computation)+ " days");
    }
 }

3 个答案:

答案 0 :(得分:3)

如果我理解了您的问题,您可以调用Date.after(Date)方法,例如

Date TDate = format.parse(transDate);
Date CDate = format.parse(computeDate);
if (TDate.after(CDate)) {
  System.out.print("Date is very wrong");
  return -1;
}

答案 1 :(得分:1)

试试这个:将返回类型更改为String方法的computeAge()

public class Test2 {

public static String computeAge(String transDate, String computeDate){
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

    long lapseDays = 0;

    try {
        Date TDate = format.parse(transDate);
        Date CDate = format.parse(computeDate);

        long aging = CDate.getTime() - TDate.getTime();
        lapseDays = aging/(24*60*60*1000);
        if(lapseDays<0){
            return "Date is very wrong";
        }

    }catch (Exception e){
        e.printStackTrace();
        System.out.print("Date is very wrong"); 
    }

   return lapseDays+" days";
 }
 public static void main(String[] args) {

    String transaction = "10/01/2014";
    String computation = "09/30/2014";
    System.out.println(computeAge(transaction, computation));
 }
 }

答案 2 :(得分:0)

约达时间

使用Joda-Time库版本2.4,这是一些更简单的代码。 Joda-Time远远优于令人困惑和麻烦的java.util.Date&amp; .Calendar类与Java捆绑在一起。

LocalDate

特别是,Joda-Time提供了一个类来表示仅限日期,没有时间或时区LocalDate

Java 8中内置的java.time包(受Joda-Time启发)也提供了LocalDate类。

测试所有结果

这个问题可能会混淆三种可能的结果,而不能测试第二种结果:

  • 字符串输入格式无效,无法解析。 (坏)
  • 第一个日期可能在第二个日期之后发生,或者相等。 (坏)
  • 第一个日期出现在第二个日期之前。 (良好)

对于更强大的代码,您还应该对数据进行健全性检查,查找过去或将来过多的日期。

提供更好的反馈

请注意,我为每个可能的结果使用不同的消息。该课题中的代码使用相同的信息,这会导致混淆。

示例代码

通过抛出Joda-Time异常IllegalArgumentException,在解析过程中检测到无效的字符串输入。

String inputA = "10/01/2014";
String inputB = "09/30/2014";
DateTimeFormatter formatter = DateTimeFormat.forPattern( "MM/dd/yyyy" );
try {
    LocalDate a = formatter.parseLocalDate( inputA );
    LocalDate b = formatter.parseLocalDate( inputB );
    int daysBetween = Days.daysBetween( a , b ).getDays();
    if ( a.isBefore( b ) ) {
        System.out.println( "First date is " + daysBetween + " days before second date." );
    } else {  // Else the same date or a is later than b.
        System.out.println( "Error: First date is same or later than second date." );
    }

} catch ( IllegalArgumentException e ) {
    System.out.println( "Invalid date string." );
}