两个特定日期之间的差异

时间:2012-03-19 09:26:06

标签: java date

我有两个日期20-03-2011和1-04-2011,存储为String。如何将其解析为日期格式并计算两个日期之间的差异?

7 个答案:

答案 0 :(得分:4)

以下是一种解决方案,因为我们可以通过多种方式实现这一目标:

 import java.util.*; 
 int syear = 2000;
 int eyear = 2000;
 int smonth = 2;//Feb
 int emonth = 3;//Mar
 int sday = 27;
 int eday = 1;
 Date startDate = new Date(syear-1900,smonth-1,sday);
 Date endDate = new Date(eyear-1900,emonth-1,eday);
 int difInDays = (int) ((endDate.getTime() - startDate.getTime())/(1000*60*60*24));

答案 1 :(得分:1)

解析在此解释:Parse A Java Date

在此计算差异:https://stackoverflow.com/a/3100373/66686

答案 2 :(得分:1)

您可以使用joda-time

public void diff(String str1, String str2)
    {
        DateTimeFormatter FMT = DateTimeFormat.forPattern("dd-mm-yyyy");
        final DateTime dt1 = new DateTime(FMT.parseDateTime(str1));
        final DateTime dt2 = new DateTime(FMT.parseDateTime(str2));
        Days days = Days.daysBetween(dt1, dt2);
    }

答案 3 :(得分:0)

java.text.DateFormat

中使用解析功能

答案 4 :(得分:0)

试试这个:

long getDateDiff(String str1, String str2) throws ParseException {

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date1 = simpleDateFormat.parse(str1);
    Date date2 = simpleDateFormat.parse(str2);

    return date2.getTime() - date1.getTime();
}

答案 5 :(得分:0)

这是一个小程序,我想说明你可能想要这样做。但要注意,它可能包含错误,因为我只用一些示例日期测试它。

你应该总是对包含日期计算的任何代码进行彻底的单元测试,因为用于完成所有这些工作的Java API非常笨重,而且似乎总是产生错误的代码。如果可能,请使用更好的日期和时间API,例如JodaTime,这是我工作场所使用的。


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

public class DateFun {

   public static void main(String[] args) throws ParseException {
      String dateOne = "20-03-2011 06:44:03 GMT";
      String dateTwo = "25-03-2011 23:12:59 GMT";
      String format = "dd-MM-yyyy HH:mm:ss z";
      System.out.println(getDiff(parseDateString(format, dateTwo), 
                  parseDateString(format, dateOne)));
      dateOne = "20-03-2011 GMT";
      dateTwo = "1-04-2011 GMT";
      format = "dd-MM-yyyy z";
      System.out.println(getDiff(parseDateString(format, dateOne), 
                  parseDateString(format, dateTwo)));
   }

   private static Calendar parseDateString(final String format, 
                  final String date) throws ParseException {
      final Date parsed = new SimpleDateFormat(format).parse(date);
      final Calendar cal = Calendar.getInstance();
      cal.setTime(parsed);
      return cal;
   }

   private static TimeDiff getDiff(final Calendar calOne, final Calendar calTwo) {
      return new TimeDiff(Math.abs(calOne.getTimeInMillis() - calTwo.getTimeInMillis()));
   }

   private static class TimeDiff {
      private static final long MS_IN_SECOND = 1000;
      private static final long MS_IN_MINUTE = MS_IN_SECOND * 60;
      private static final long MS_IN_HOUR = MS_IN_MINUTE * 60;
      private static final long MS_IN_DAY = MS_IN_HOUR * 24;
      private final long days;
      private final long hours;
      private final long minutes;
      private final long seconds;
      private final long milliseconds;
      TimeDiff(final long msDiff) {
         long msRemainder = msDiff;
         days = msRemainder / MS_IN_DAY;
         msRemainder = msRemainder - (days * MS_IN_DAY);
         hours = msRemainder / MS_IN_HOUR;
         msRemainder = msRemainder - (hours * MS_IN_HOUR);
         minutes = msRemainder / MS_IN_MINUTE;
         msRemainder = msRemainder - (minutes * MS_IN_MINUTE);
         seconds = msRemainder / MS_IN_SECOND;
         msRemainder = msRemainder - (seconds * MS_IN_SECOND);
         milliseconds = msRemainder;
      }

      @Override
      public String toString() {
         return "TimeDiff[days: " + days + ", hours: " + 
                  hours + ", minutes: " + minutes + 
                  ", seconds: " + seconds + ", milliseconds: " + 
                  milliseconds + "]";
      }
   }
}

如果有人发现了这个错误,请告诉我。

答案 6 :(得分:0)

请尝试以下代码:

public void compareDate(String date1, String date2){
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar1.setTime(format.parse(date1));
    calendar2.setTime(format.parse(date2));
    long milliseconds1 = calendar1.getTimeInMillis();
    long milliseconds2 = calendar2.getTimeInMillis();
    long diff = milliseconds2 - milliseconds1;
    long diffSeconds = diff / 1000;
    long diffMinutes = diff / (60 * 1000);
    long diffHours = diff / (60 * 60 * 1000);
    long diffDays = diff / (24 * 60 * 60 * 1000);
    System.out.println("\nThe Date Different Example");
    System.out.println("Time in milliseconds: " + diff+ " milliseconds.");
    System.out.println("Time in seconds: " + diffSeconds+ " seconds.");
    System.out.println("Time in minutes: " + diffMinutes + " minutes.");
    System.out.println("Time in hours: " + diffHours + " hours.");
    System.out.println("Time in days: " + diffDays + " days.");
}