joda时间查找小时之间的差异

hfwmuf9z  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(411)

我有一个日期字符串:

Thu, 15 Jan 2015, 9:56 AM

我将其转换为日期变量:

Thu Jan 15 09:56:00 GMT+05:30 2015

使用:

String pattern = "EEE, d MMM yyyy, hh:mm a";
        try {
            date = new SimpleDateFormat(pattern).parse(getPref("refresh", getApplicationContext()));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

现在我有了下面的函数,并将日期变量传递给下面的函数:

public static int getDiffHour(Date first) {
        int hoursBetween = Hours.hoursBetween(new LocalDate(first), new LocalDate()).getHours();
        return hoursBetween;
    }

它总是返回0。可能的原因是什么?

pu3pd22g

pu3pd22g1#

DateTimeUtils obj = new DateTimeUtils();
  SimpleDateFormat simpleDateFormat = 
            new SimpleDateFormat("EEE, d MMM yyyy, hh:mm a");

  try {

    Date date1 = simpleDateFormat.parse("Thu Jan 15 09:56:00 GMT+05:30 2015");
    Date date2 = simpleDateFormat.parse("Thu Jan 16 09:56:00 GMT+05:30 2015");

    obj.printDifference(date1, date2);

  } catch (ParseException e) {
    e.printStackTrace();
  }

}

//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public void printDifference(Date startDate, Date endDate){

    //milliseconds
    long different = endDate.getTime() - startDate.getTime();

    System.out.println("startDate : " + startDate);
    System.out.println("endDate : "+ endDate);
    System.out.println("different : " + different);

    long secondsInMilli = 1000;
    long minutesInMilli = secondsInMilli * 60;
    long hoursInMilli = minutesInMilli * 60;
    long daysInMilli = hoursInMilli * 24;

    long elapsedDays = different / daysInMilli;
    different = different % daysInMilli;

    long elapsedHours = different / hoursInMilli;
    different = different % hoursInMilli;

    long elapsedMinutes = different / minutesInMilli;
    different = different % minutesInMilli;

    long elapsedSeconds = different / secondsInMilli;

    System.out.printf(
        "%d days, %d hours, %d minutes, %d seconds%n", 
        elapsedDays,
        elapsedHours, elapsedMinutes, elapsedSeconds);

}
xoshrz7s

xoshrz7s2#

试试下面code:-

public static void main(String[] args) {

    String dateStart = "01/14/2012 09:29:58";
    String dateStop = "01/15/2012 10:31:48";

    //HH converts hour in 24 hours format (0-23), day calculation
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    Date d1 = null;
    Date d2 = null;

    try {
        d1 = format.parse(dateStart);
        d2 = format.parse(dateStop);

        //in milliseconds
        long diff = d2.getTime() - d1.getTime();

        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);

        System.out.print(diffDays + " days, ");
        System.out.print(diffHours + " hours, ");
        System.out.print(diffMinutes + " minutes, ");
        System.out.print(diffSeconds + " seconds.");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

有关更多信息,请参见以下链接:-
http://www.mkyong.com/java/how-to-calculate-date-time-difference-in-java/

zaqlnxep

zaqlnxep3#

像这样试试,

int diff_hrs = getDiffHours(date,new Date());// pass your date object as startDate and pass current date as your endDate

public int getDiffHours(Date startDate, Date endDate){

  Interval interval = new Interval(startDate.getTime(), endDate.getTime());
  Period period = interval.toPeriod();
  return period.getHours();
}

相关问题