Reputation: 2443
I would like to determine when the current time equals a defined time + 15mins.
The defined time here is in the format:
private Date fajr_begins;
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
fajr_begins = new Time(formatter.parse(prayerTimes.get(0)).getTime());
The code I have come up so far, which is not working is (the code below is crappy I know
DateTime today = new DateTime();
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
now1 = new Time(formatter.parse(today));
Duration duration = new Duration(sunrise, now1);
System.out.println(" time to duha " + duration);
Upvotes: 0
Views: 1348
Reputation: 347314
The context of the question is a little light. Do you want to use a thread, do you want to be alerted...?
However, as a basic example you could do something like...
// The time we want the alert...
String time = "16:00";
// The date String of now...
String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
try {
// The date + time to give us context
Date timeAt = sdf.parse(date + " " + time);
boolean rollOver = false;
// Determine if the time has already passed, if it has
// we need to roll the date to the next day...
if (timeAt.before(new Date())) {
rollOver = true;
}
// A Calendar with which we can manipulate the date/time
Calendar cal = Calendar.getInstance();
cal.setTime(timeAt);
// Skip 15 minutes in advance
cal.add(Calendar.MINUTE, 15);
// Do we need to roll over the time...
if (rollOver) {
cal.add(Calendar.DATE, 1);
}
// The date the alert should be raised
Date alertTime = cal.getTime();
System.out.println("Raise alert at " + alertTime);
// The timer with which we will wait for the alert...
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("duha");
}
}, alertTime);
} catch (ParseException ex) {
}
Now, before you complain about the Date
, everything is relative. Without the Date
part of the time, it's difficult to know when we should raise our alert. The Date
just helps us pinpoint the when the alert should be raised...
Additional
Context is everything, for example...if we use the following...
String time = "16:00";
try {
Date timeAt = new SimpleDateFormat("HH:mm").parse(time);
System.out.println(timeAt);
} catch (ParseException ex) {
ex.printStackTrace();
}
The timeAt
value be Thu Jan 01 16:00:00 EST 1970
, which is really useless, the time will always be before now...
If, instead, we use something like...
String time = "16:00";
String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
try {
Date timeAt = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(date + " " + time);
System.out.println(timeAt);
} catch (ParseException ex) {
ex.printStackTrace();
}
The timeAt
will now be Thu Sep 05 16:00:00 EST 2013
which gives us some context to now
Now if we use Calendar
to advance the time by 15 minutes...
Calendar cal = Calendar.getInstance();
cal.setTime(timeAt);
cal.add(Calendar.MINUTE, 15);
Date checkTime = cal.getTime();
System.out.println(checkTime);
The checkTime
becomes Thu Sep 05 16:15:00 EST 2013
. I use Calendar
because it will automatically roll the hour and date for me should it need to be...
This now allows us to start using the default available API functionality. Because it's highly unlikely that the milliseconds will ever match, I would be temtered to do something like...
Calendar watchFor = Calendar.getInstance();
watchFor.setTime(timeAt);
watchFor.set(Calendar.MILLISECOND, 0);
watchFor.set(Calendar.SECOND, 0);
Calendar now = Calendar.getInstance();
now.setTime(new Date());
now.set(Calendar.MILLISECOND, 0);
now.set(Calendar.SECOND, 0);
if (watchFor.equals(now)) {
System.out.println("Go for it...");
}
Zeroing out the milliseconds and seconds, so I can compare the Date and time (HH:mm) alone.
You could of course compare the milliseconds directly as well...
Upvotes: 2
Reputation: 35577
Is this you want to do? Following sentence I got in that way.
I would like to determine when the current time equals a defined time + 15mins.
you can simply do as follows
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
Date preDefineTime=formatter.parse("10:00");
long additionMin=15*60*1000;
System.out.println(formatter.format(preDefineTime.getTime()+additionMin));
Upvotes: 1