1

I need to figure out how to store times like 12:45, 13:45 etc.. so in format of HH:MM It is NOT NECESSARY to use date functionality in java, these will not be used to time an event or anything of that matter, all I need them for is to perform operations like 13:45 - 12:45 and get output which in this case is 01:00 I tried doing this with doubles, however ran into issue of something like 13.45 + 00.35 = 13.80; as there are only 60 minutes in a hour, expected output should be 14.20

I'm trying to figure out how to do this with date as well, but remember at the moment I do not need to be concerned about things like time zones, calendar dates etc.. I need to figure out how to make numbers behave like hours and minutes.

Sorry if this is confusing.

Ilja
  • 44,142
  • 92
  • 275
  • 498
  • 5
    If you want numbers to behave like times, treat them as such. Then you won't have to bother with wrapping around 60 and stuff like that. – Jeroen Vannevel Nov 02 '13 at 13:40
  • @JeroenVannevel That's totally what I just wrote, but you were faster ;-) – L.Butz Nov 02 '13 at 13:43
  • @JeroenVannevel could you expand on your comment, as now even if I treat a double like 12.30 as a time adding lets say 45 minutes, so 00.45 to it will result in 12.75, and that is not time at all, I probably don't get the idea just yet. – Ilja Nov 02 '13 at 13:48
  • @ilya: just create two dates and use those objects (`Calendar`). You don't care about the date aspect, just the time. – Jeroen Vannevel Nov 02 '13 at 14:00
  • "there are only 60 minutes in a hour" = The reason you should time data as time rather than as decimal numbers. [Joda-Time](http://www.joda.org/joda-time/) provides the time-based objects you need. – Basil Bourque Nov 03 '13 at 07:23

5 Answers5

1

You should create an object. Call it HHMM. It would look something like this

public class HHMM {

  private int HH;
  private int MM;

  public HHMM(HH, MM) {

    this.HH = HH
    this.MM = MM

  }

  public toStr() {
    return HH + ":" + MM
  }

  public minus(HHMM hhmm){
    //write code here that subtracts the passed HHMM from this HHMM
  }

  //make a plus method too, and any other methods, etc.


}
Peter Berg
  • 6,006
  • 8
  • 37
  • 51
1

Alright, here's my comment written out:

Calendar end = new GregorianCalendar(2013, 05, 05, 12, 40, 0);
Calendar start = new GregorianCalendar(2013, 05, 05, 12, 15, 0);

Calendar diff = end - start;
int minutes = diff.get(Calendar.Minutes);
int seconds = diff.get(Calendar.Seconds);

You don't have to use the date part to use the time part.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
1

Joda-Time has already implemented what you are looking for in the LocalTime class:

LocalTime start = LocalTime.parse("12:45");
LocalTime end = LocalTime.parse("13:45");
Period period = new Period(start, end);

and

LocalTime start = new LocalTime(13, 45);
LocalTime later = start.plusMinutes(35);
matsev
  • 32,104
  • 16
  • 121
  • 156
  • 1
    Correct answer by matsev. With a Period object in hand, you can calculate elapsed time. See [How to calculate elapsed time from now with Joda Time?](http://stackoverflow.com/questions/2179644/how-to-calculate-elapsed-time-from-now-with-joda-time). – Basil Bourque Nov 03 '13 at 07:29
0

Store all numbers in minutes then. Using a single integer, you can do simple time manipulation by doing integer math. Mod all your operations by 1440 (60*24) if you don't care about what day it is:

Example:

int time = 600;

//Example time operation
time = (5000 + time) % 1440; //Day-insensitve

//Perform this check if you suspect time will become negative.
if(time < 0){
    time = 1440 + time;
}

//Display time
int hours = time / 60; //Integer division of 60, get the maximum number of "perfect 60s" in your time component.
int minutes = time % 60; //Integer mod, get the remainder since the last "perfect 60"

System.out.println(String.format("%02d:%02d", hours, minutes));

Note how most time keeping constructs simply store time/date or whatever as a single numerical value with a base unit. Take for example: System.currentTimeMillis() which returns time as a number of milliseconds since the epoch.

If you require only simple manipulation operations with something like time, you need only understand how to parse a single value into your desired components (e.g seconds, minutes, hours). Operations on this numerical value is same like traditional mathematics.

initramfs
  • 8,275
  • 2
  • 36
  • 58
0

One way I use to make transactions with date and hours in java, I use a framework, called Joda-Time, he is very good. Only you just download the. Jar, and looks at the ways how to do operations with més, years and days. Using this framework vc saves a lot of headaches by doing more complex codes.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154