I want to compare if 2 LocalTime are equal, but only using the hours, minutes and seconds, not with all the data of the variable, like milliseconds.
How can I accomplish that?
Considering the last edited version of your question, you can compare two instances of LocalTime by just hour, minute and second part this way:
LocalTime lt1 = LocalTime.now(); // usually contains seconds and subseconds
LocalTime lt2 = LocalTime.of(22, 48); // example with zero second part
boolean isEqualInSecondPrecision =
lt1.truncatedTo(ChronoUnit.SECONDS).equals(lt2.truncatedTo(ChronoUnit.SECONDS));
You can set the hours to the same in both with myTime.withHour(0), than you have left only the minutes and seconds that differ and you are able to come these 2 times.
Example:
time1 = ...
time2 = ...
if (time1.withHour(0).equals(time2.withHour(0))) {
System.out.println('Minutes and Seconds of time1 and time2 are equal!');
}
You can just set both nanos to one number to make them the same with each other, say, zero, to compare them.
LocalTime localTime1 = LocalTime.of(1, 2, 3, 100);
LocalTime localTime2 = LocalTime.of(1, 2, 3, 47);
if (localTime1.withNano(0).equals(localTime2.withNano(0))){
System.out.println("something");
}
Something like this
LocalTime t1=..
LocalTime t2=..
LocalTime.of(t1.getHour(), t1.getMinutes()).compareTo(LocalTime.of(t2.getHour(),t2.getMinutes());
with also seconds if u need of course
Related
This question already has answers here:
How to get milliseconds from LocalDateTime in Java 8
(14 answers)
Closed 3 years ago.
Executed piece of code :
String lapTime = "27:10.190";
int minutes = Integer.parseInt(lapTime.substring(0, lapTime.indexOf(":")));
int seconds = Integer.parseInt(lapTime.substring(lapTime.indexOf(":")+1, lapTime.indexOf(".")));
int milliseconds = Integer.parseInt(lapTime.substring(lapTime.indexOf(".")+1));
LocalTime localTime = LocalTime.of(0, minutes, seconds, milliseconds);
How to gain number of milliseconds of LocalTime localTime = LocalTime.of(0, minutes, seconds, milliseconds); ?
TL;DR Use Duration, not LocalTime. See end of answer.
Question code is incorrect
Be aware that the 4th argument to LocalTime.of() is nanosecond, not millisecond, which you'd see if you print localTime:
System.out.println(localTime); // prints: 00:27:10.000000190
So you need to change your code to:
LocalTime localTime = LocalTime.of(0, minutes, seconds, milliseconds * 1000000);
System.out.println(localTime); // prints: 00:27:10.190
Using LocalTime
If you wanted the milliseconds value back, call getLong(TemporalField field) with ChronoField.MILLI_OF_SECOND:
localTime.getLong(ChronoField.MILLI_OF_SECOND) // returns 190
To gain total number of milliseconds, i.e. not just the milliseconds value, use ChronoField.MILLI_OF_DAY as argument:
localTime.getLong(ChronoField.MILLI_OF_DAY) // returns 1630190
Using Duration
However, since the input is named lapTime, the LocalTime class is not the right tool for the job. E.g. your code will fail if minutes >= 60.
The right tool is the Duration class, e.g.
Duration duration = Duration.ofMinutes(minutes).plusSeconds(seconds).plusMillis(milliseconds);
Or:
Duration duration = Duration.ofSeconds(seconds, milliseconds * 1000000).plusMinutes(minutes);
You can then get the milliseconds directly by calling toMillis():
duration.toMillis(); // returns 1630190
That works even if the lap time exceeds one hour, e.g.
String lapTime = "127:10.190";
. . .
duration.toMillis(); // returns 7630190
In Java 9+, you can get the millisecond part back easily, by calling toMillisPart():
duration.toMillisPart(); // returns 190
To get the number of milliseconds within the second, you can do localTime.get(ChronoField.MILLI_OF_SECOND).
You can do localTime.toNanoOfDay() / 1000000 to get the number of milliseconds since the start of the day.
Since a LocalTime has no day or date associated with it, you can't directly get milliseconds-since-the-epoch.
You can attach a LocalDate to a LocalTime using LocalTime.atDate to get a LocalDateTime. Then you can call atZone, atOffset or toInstant to get an object that can return epochMillis.
I am using Joda DateTime and have 2 dates :
DateTime old //which is 1:46PM
DateTime new //which is 6:46PM
note: excluded the dates.
How may i be able to loop through the difference in this order :
(print a message for the first half hour, another message for the next 30 minutes and another message per subsequent hour) ?
I was thinking of Subtracting the old date from the new date then make a loop but i don't get the logic. Any pointers will be helpful.
example
If i subtract both times above, i will have an elapsed time of 5 hours.
Loop 5 hours
{
for the first hour (print this)
next 30minutes (print that)
every subsequent hour (print ....)
}
I would use the type LocalTime instead. If you have DateTime as input then please convert it using the method toLocalTime() (with same time and chronology and timezone).
LocalTime start = new LocalTime(13, 46);
LocalTime end = new LocalTime(18, 46);
LocalTime current = start;
for (int i = 0; current.isBefore(end); i++) {
// code your print action here
current = current.plusMinutes((i < 2) ? 30 : 60);
}
Then you get an action for following times:
13:46
14:16
14:46
15:46
16:46
17:46
using Joda Time how do I find the number of milliseconds between 2 LocalTime objects?
LocalTime two = new LocalTime(2,0);
LocalTime six = new LocalTime(6,0)
So the number of milliseconds between 2am and 6am?
Thanks
System.out.println(six.getMillisOfDay() - two.getMillisOfDay());
Hello I have this excerpt of code:
end = new DateTime(mergeToDateTime(this.endDate, this.empEndTime));
Duration extraTime = new Duration(this.preTime.getTime()); //add the first 30 mins
extraTime = extraTime.plus(new Duration(this.postTime.getTime())); //add the second 30 mins
end = end.plus(extraTime); // extraTime = -3600?
When I look in the debugger my durations are always coming up negative. I have no idea why this is, even though according to the API, it is possible to create a duration out of the a long type, hence the getTime(). (preTime and postTime are java.sql.Time types)
I guess your instances of java.sql.Time were created in such a way that their millisecond values include timezone offset.
For example, deprecated java.sql.Time(int hour, int minute, int second) constructor takes offset of the current timezone into account:
System.out.println(new Time(1, 0, 0).getTime()); // Prints -7200000 in UTC+3 timezone
It looks like timezone offset is introduced by JDBC driver, and it can be easily compensated by converting java.sql.Time to LocalTime (and vice versa):
LocalTime lt = new LocalTime(time);
Then you can convert LocalTime to duration:
Duration d = new Duration(lt.getMillisOfDay());
Aren't you starting out wrong when you use an instant in time as duration? The constructor signature you are using is Duration(long duration), not Duration(long startInstant) -- there is no such constructor, in fact.
Can you please help in matter:
I have defined a variable which is:
Time from_time = rs.getTime("nfrm_time");
and it will read the values 7:15:00
How to convert this type to seconds?
Call getTime to get the number of milliseconds since January 1, 1970. Divide by 1000 to get it in seconds:
long unixTime = from_time.getTime() / 1000;
To get the number of seconds since 00:00 of the current day, use the
Calendar c = Calendar();
c.setTime(from_time);
long daySeconds = (c.get(Calendar.SECONDS) +
c.get(Calendar.MINUTES) * 60 +
c.get(Calendar.HOURS) * 3600);
long seconds = rs.getTime("nfrm_time").getTime() / 1000
Here's the explanation:
rs.getTime("nfrm_time") returns java.sql.Time which is actually a sub class of java.util.Date.
java.util.Date.getTime() returns time in milli seconds which we divide by 1000 to get seconds.
Note:
If you're looking for duration instead,
Calendar cal = Calendar.getInstance();
cal.setTime(rs.getTime("nfrm_time")); // set to the time returned by resultset
cal.set(0, 0, 0); // reset the year, month and date fields
Calendar cal2 = Calendar.getInstance();
cal2.set(0, 0, 0, 0, 0, 0); // reset all the fields, including time
long duration = ((cal.getTimeInMillis() - cal2.getTimeInMillis()) / 1000) + 1;
Try:
from_time.getTime() / 1000
This might work since:
The date components should be set to the "zero epoch" value of January 1, 1970 and should not be accessed.
This means that the date part is always the epoch day, which means the Time instance is represented by the number of milliseconds since the beginning of the day.
java.sql.Time inherits from java.util.Date which has a method getTime() which returns the number of milliseconds since January 1, 1970, 00:00:00 GMT.
So from_time.getTime()/1000 should do the trick.
Cleaner way is
Time from_time = Math.round(rs.getTime("nfrm_time")/1000);
To get the milliseconds:
long ms = from_time.getTime();