检查给定日期时间是否在当前日期时间范围内(+/- 1小时)

时间:2015-10-16 14:08:03

标签: java date time

我有一个给定的日期,格式为dd。 MMMM yyyy HH:mm'Uhr'

现在我想用当前日期检查这个日期,检查它是否在当前日期时间的+1小时和-1小时的范围内,当它在此范围内时,if条件应该是否满足。

当有人能帮助我时,我将不胜感激!

是的,我没有机会使用JODA。

4 个答案:

答案 0 :(得分:1)

Eric在另一个答案中发布:Here's the link for credit.一个相当简单的方法,可以使用Calendar类分开时间。如果有的话,你可以分开来学习一下两次之间的差异。

public static int hoursAgo(String datetime) {

    Calendar date = Calendar.getInstance();
    date.setTime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH).parse(datetime)); // Parse into Date object
    Calendar now = Calendar.getInstance(); // Get time now
    long differenceInMillis = now.getTimeInMillis() - date.getTimeInMillis();
    long differenceInHours = (differenceInMillis) / 1000L / 60L / 60L; // Divide by millis/sec, secs/min, mins/hr
    return (int)differenceInHours;

}

答案 1 :(得分:1)

DateFormat format = new SimpleDateFormat("dd. MM yyyy HH:mm");
String dateString= "16. 10 2015 11:05";

Date date = format.parse(dateString);
    private static boolean DateInScope(Date date) {
        Date currentTime = new Date();
        long hours = TimeUnit.MILLISECONDS.toHours(currentTime.getTime());
        long hours2 = TimeUnit.MILLISECONDS.toHours(date.getTime());
        return hours - 1 == hours2 || hours + 1 == hours2;
}

答案 2 :(得分:0)

可能以下方法可以帮到你。

public static String getDiffBtwnDates(Date date1, Date date2){
        long date1InMillis = date1.getTime();
        long date2InMillis;
        if (date2==null){
            date2InMillis = System.currentTimeMillis();
        }else{
            date2InMillis = date2.getTime();
        }
        long dateDiffInMillis = date2InMillis-date1InMillis;


        StringBuffer sTimeSince = new StringBuffer("");
        if(dateDiffInMillis > YEAR){
            if(dateDiffInMillis/YEAR>1){
                sTimeSince.append(dateDiffInMillis / YEAR).append(" Years ");
                dateDiffInMillis %= YEAR;
            }else {
                sTimeSince.append(dateDiffInMillis / YEAR).append(" Year ");
                dateDiffInMillis %= YEAR;
            }
        }
        if (dateDiffInMillis > DAY) {
            if(dateDiffInMillis/DAY > 1){
                sTimeSince.append(dateDiffInMillis / DAY).append(" Days ");
                dateDiffInMillis %= DAY;
            }else {
                sTimeSince.append(dateDiffInMillis / DAY).append(" Day ");
                dateDiffInMillis %= DAY;
            }
        }
        if (dateDiffInMillis > HOUR) {
            if(dateDiffInMillis/HOUR>1){
                sTimeSince.append(dateDiffInMillis / HOUR).append(" Hrs ");
                dateDiffInMillis %= HOUR;
            }else{
                sTimeSince.append(dateDiffInMillis / HOUR).append(" Hr ");
                dateDiffInMillis %= HOUR;
            }
        }
        if (dateDiffInMillis > MINUTE) {
            if (dateDiffInMillis / MINUTE > 1) {
                sTimeSince.append(dateDiffInMillis / MINUTE).append(" Mins ");
                dateDiffInMillis %= MINUTE;
            } else {
                sTimeSince.append(dateDiffInMillis / MINUTE).append(" Min ");
                dateDiffInMillis %= MINUTE;
            }
        }
        if (dateDiffInMillis > SECOND) {
            sTimeSince.append(dateDiffInMillis / SECOND).append(" Sec ");
            dateDiffInMillis %= SECOND;
        }
        sTimeSince.append(dateDiffInMillis + " ms");
        sTimeSince.toString();
        return sTimeSince.toString();
    }

答案 3 :(得分:0)

这是一个非常简单的方法,可以进行您需要的精确比较,我做了一些测试,似乎有效:

package test;

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

public class TestDateDiff {

    public static void main(String[] args) throws Exception {
        if (isWhitinOneHOur("16. October 2015 16:30")) {
            System.out.println("Date OK");
        } else {
            System.out.println("Date KO");
        }

    }

    public static boolean isWhitinOneHOur(String dateAsStr) throws Exception {
        SimpleDateFormat sdf= new SimpleDateFormat("dd. MMMM yyyy HH:mm", Locale.US); 
        Date date=sdf.parse(dateAsStr);
        Date now=new Date();
        long oneHour=1000*60*60;
        if ((now.getTime()+oneHour)>=date.getTime() && (now.getTime()-oneHour)<=date.getTime()) {
            return true;
        } else {
            return false;
        }           
    }

}