Groovy - Calculate if certain minutes are elapsed from the current time
isn't it way too complicated?
final long TWO_HOURS_MILIS = 2 * 60 * 60 * 1000l
Date downTimeStartCFDate = (Date)issue.getCustomFieldValue(downTimeStart)
Date downTimeEndCFDate = (Date)issue.getCustomFieldValue(downTimeEnd)
long now = System.currentTimeMillis()
long delta = now - downTimeStartCFDate.time
// fire between 1:59 and 2:00
if( delta % TWO_HOURS_MILIS >= TWO_HOURS_MILLIS - 60000 && now < downTimeEndCFDate.time ){
sendEmail()
}
Checking elapsed time in Groovy/Grails
You can put the value in the session and it will persist between requests. For example:
def connected = {
session.startTime = new Date().getTime()
}
def callEnded = {
def endTime = new Date().getTime()
timeHandler(endTime, session.startTime)
}
def timeHandler = {end, start->
return end - start
}
How to calculate elasped time in Java from a provided hour ONLY
Firstly, I'd suggest using the Joda Time API. It's the best date/time API available for Java, in my opinion.
Next you need to work out exactly what to do in various corner cases. In particular, suppose the user enters "1" and you're near a daylight saving transition. It's possible that 1am happened twice (if the the time went 1:58, 1:59, 1:00, 1:01 because of a transition back away from DST) or that it didn't happen at all (if the time went 12:58, 12:59, 2:00 because of a transition forward into DST). You need to work out what to do in each of those situations - and bear in mind that this means knowing the time zone too.
Once you've worked that out, it may not be too hard. With Joda Time you can use withHourOfDay
method to get from one time to another having set one component of the time - and likewise there are simple APIs for adding or subtracting a day, if you need to. You can then work out the time between two DateTime
values very easily - again, Joda Time provides everything you need.
Here's an example which doesn't try to do anything with DST transitions, but it's a good starting point:
import org.joda.time.*;
public class Test
{
public static void main(String[] args)
{
// Defaults to current time and time zone
DateTime now = new DateTime();
int hour = Integer.parseInt(args[0]);
DateTime then = now
.withHourOfDay(hour)
.withMinuteOfHour(0)
.withSecondOfMinute(0);
if (then.isAfter(now))
{
then = then.minusDays(1);
}
Period period = new Period(then, now, PeriodType.seconds());
System.out.println("Difference in seconds: " + period.getSeconds());
}
}
Duration between two dates in Groovy
TimeCategory has some methods for getting a duration. You could use it like
use(groovy.time.TimeCategory) {
def duration = date1 - date2
print "Days: ${duration.days}, Hours: ${duration.hours}, etc."
}
Is there a Java library for calculating elapsed time?
Use Joda time's Period & PeriodFormatter. Example.
Related Topics
Why Can Not I Add Two Bytes and Get an Int and I Can Add Two Final Bytes Get a Byte
What Is Difference Between Collection.Stream().Foreach() and Collection.Foreach()
How Apply CSS on a <H:Inputtext>
Preemptive Basic Auth with Httpurlconnection
How to Parse Date from Gmt Timezone to Ist Timezone and Vice Versa in Android
Android Days Between Two Dates
Android:How to Read File in Bytes
Java.Net.Socketexception: Socket Failed: Eperm (Operation Not Permitted)
Retrofit and Rxjava: How to Combine Two Requests and Get Access to Both Results
How to Close a Java Swing Application from the Code
Getresourceasstream() Is Always Returning Null
When Should We Use Observer and Observable
Unobtrusive Way to Combine and Compress JavaScript/CSS for Java/Spring/Maven Applications
Send Sms Until It Is Successful
How to Draw Circle by Canvas in Android
Which Jdk Version (Language Level) Is Required for Android Studio