I often need to manipulate time. The Calendar class is good for working with dates and times but I can't seem to find a simple way of just storing elements like hours, minutes and seconds into some structure. There seems to be a lot of conversion required from strings to actual ints. I can always create my own class and just store the elements into fields but it would be nicer if a class exists that provides more functionality (adding time, etc) yet keeps it simple for storing and accessing. I'm looking for something like this:
Time time = new Time();
time.parse("13:30:15");
t.Hour = 13;
t.Minute = 30;
t.Second = 15;
Are there any Java classes that exist that do something simple like this. Here's how I do it now. What a horrific amount of code just to parse out hours and minutes:
String time = "17:30";
TimeInfo timeInfo = new TimeInfo();
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
Date d = df.parse(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
timeInfo.Hours = calendar.get(Calendar.HOUR_OF_DAY);
timeInfo.Minutes = calendar.get(Calendar.MINUTE);
timeInfo.Seconds = calendar.get(Calendar.SECOND);
class TimeInfo
{
public int Hours;
public int Minutes;
public int Seconds;
public boolean Is24HourFormat;
}