【问题标题】:Joda time find difference between Hours乔达时间发现小时之间的差异
【发布时间】:2015-03-13 12:16:03
【问题描述】:

我有一个日期字符串:

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。可能的原因是什么?

【问题讨论】:

标签: java android datetime jodatime


【解决方案1】:

试试这样,

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

【讨论】:

  • 如果 endDate 是 currentdate 怎么办?它不允许我做一个新的 LocalDate().getTime.
  • 您可能会因为解析日期格式不同而不是LocalDateJodaTime 使用的格式而面临问题。所以我会坚持让你按照我的回答中提到的那样解析它。
  • 像魅力一样工作 :) 谢谢伙计!
【解决方案2】:
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);

}

【讨论】:

    【解决方案3】:

    试试下面的代码:-

    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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 2013-03-22
      • 2013-03-18
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      相关资源
      最近更新 更多