62448

determine if current time in java is past a predetermined time by 15mins

Question:

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

Answer1:

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...

<strong>Additional</strong>

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...

Answer2:

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

Recommend

  • PHP Carbon “month()” method generates wrong DateTime
  • Year, Month, and Day parameters describe an un-representable DateTime in Persian calender
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • Visual Studio 2010 - 2015 does not use ymm* registers for AVX optimization
  • jQuery random blockquote
  • A simple datepicker in VueJS
  • How to make Plotly chart with year mapped to line color and months on x-axis
  • How to parsing NSDate to RFC 822 always use in English?
  • iOS Date formatting
  • Is the Go HTTP handler goroutine expected to exit immediately in this case?
  • Better file search algorithm than creating a list of files
  • stop execution in Custom validator if it false
  • Smack 4.1.0 android Roster not displaying
  • Why isn't my “Fizz Buzz” test in R working?
  • Python PIL to extract number from image
  • Get localized short date pattern as String?
  • CodeIgniter URI Parameter is partially bypassing an “if” statement
  • If statement skipping right to else after being called once?
  • opencv display image without x server
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • Reading a file into a multidimensional array
  • How do I configure context broker accept post requests from my remote sensor?
  • App restarts from wrong activity
  • Parse a date string in a specific locale (not timezone!)
  • How to use JavaScript to determine whether a file exists in a directory?
  • Functions in global context
  • Jetty Server not starting: Unable to establish loopback connection
  • FileReader+canvas image loading problem
  • Illegal mix of collations for operation for date/time comparison
  • Javascript Callbacks with Object constructor
  • sending/ receiving email in Java
  • Release, debug version and Authorization Google?
  • AT Commands to Send SMS not working in Windows 8.1
  • Compare two NSDates in iPhone
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Hits per day in Google Big Query
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++