0

I have a given date, with the format dd. MMMM yyyy HH:mm 'Uhr'

Now I want to check this date with the current date, checking if its in the scope of +1 hour and -1 hour of the current date time, when its in this scope, the if condition should be statisfied.

I would appreciate it, when someone could help me!

Btw, I have no opportunity to use JODA.

Matej
  • 147
  • 2
  • 12

4 Answers4

1

Eric posted in another answer: Here's the link for credit. A rather simple method that get's the time apart using the Calendar class. If anything you can pick it apart to learn a bit about getting the differences between two times.

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;

}
Community
  • 1
  • 1
General Charismo
  • 123
  • 1
  • 1
  • 10
  • No problem at all, it was thanks to Eric that I didn't have to rewrite the code all by myself to prove my point! – General Charismo Oct 16 '15 at 14:17
  • This code ignores the issue of time zone. I don't know for sure as I avoid these old date-time questions, but I expect the JVM’s current default time zone will be applied in parsing. That may not be the time zone represented by this input string. – Basil Bourque Oct 19 '15 at 13:32
  • Depends, from my experience a lot of languages pull from the client sides time zone. Of course I don't believe this should matter as if the application picks up that somebody who lives in a -9:00 UTC did something and then waited an hour it would use the JVM's default time zone in place and still solve the issue in question. – General Charismo Oct 20 '15 at 13:58
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;
}
vanlooverenkoen
  • 2,121
  • 26
  • 50
0

May be the following method could help you.

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();
    }
Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50
0

Here is a very simple method that does the exact comparison you need, i did some tests and it seems to work:

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

}
Giovanni
  • 3,951
  • 2
  • 24
  • 30