• Post Reply Bookmark Topic Watch Topic
  • New Topic

Time conversion help in Java  RSS feed

 
Greenhorn
Posts: 21
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a unique time problem

I need to determine if a given time is between two values, that is easy to do, but where my issue is that my upper limit number is different,
so my lower Limit number = 0
my upper limit number is 1:00:00:00

the upper limit needs to be that way

so an idea i had was to make the upper limit an array and then perform checks on each part of the array to determine if the value is within the range of values, but not sure if that is the best approach

2nd idea would be to convert that upper limit number in seconds and the perform the check that way

or is there something a bit better
thanks for helping
 
Saloon Keeper
Posts: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What format is this: 1:00:00:00
days:hours:minutes:seconds ?

If it is hours:minutes:seconds:unknown what is the last field?

If it is a piece smaller than a second it is usually expressed as a decimal value of seconds. E.g. 0:00:01.001
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would convert everything to java.time.LocalTime objects and do the obvious comparisons. Don't reinvent your own Time objects and comparisons thereof.

And I'm a bit confused about why the upper limit being "different" is a problem. It would be normal for the upper limit to be different from the lower limit, so you must mean that it's "different" from something else. Like what?
 
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:What format is this: 1:00:00:00
days:hours:minutes:seconds ?

This is difficult to say without some more sample data.

This Oracle Standard Calendar Java Trail may help you along
https://docs.oracle.com/javase/tutorial/datetime/iso/index.html
Or more specifically this one
https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
It contains some information/samples on isBefore, isEqual methods as well as the Period, Duration and ChronoUnit enum.

Period can handle years, months, days values.
While Duration can handle hours, minutes, seconds, and maybe even nanoseconds values.
 
Chris Kislow
Greenhorn
Posts: 21
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:What format is this: 1:00:00:00
days:hours:minutes:seconds ?

If it is hours:minutes:seconds:unknown what is the last field?

If it is a piece smaller than a second it is usually expressed as a decimal value of seconds. E.g. 0:00:01.001

the first one is days, then hours, minutes, seconds
 
Pete Letkeman
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you may be able to use the DateTimeFormater  (https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) to get these values into usable values.
You can find the Java Trail for this here https://docs.oracle.com/javase/tutorial/datetime/iso/format.html

I think you would want to have your time pattern to be something like this:
D:HH:mm:ss
But you may have to do some trial and error to get the correct pattern.

Once you have the correct pattern then you do what Paul stated:
Paul Clapham wrote:I would convert everything to java.time.LocalTime objects and do the obvious comparisons.

All the possible values for the time pattern are listed here
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!