3

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;
}
Johann
  • 27,536
  • 39
  • 165
  • 279
  • 1
    Personally I suggest you to look into Joda Time library, it's far better than Java's in-built date/time library. Source: using in my projects – Pradeep Simha Feb 20 '13 at 12:12
  • 1
    java.util.Date represents a unix time stamp (seconds since 1970) which can also be represented as a long variable. For simple things this is good enough. If you really just want hours and minutes and seconds just write your own class. – John Smith Feb 20 '13 at 12:14
  • 1
    Search for "java 8 date time" and you'll see the improvements of Java 8. So I would for the moment try to live with what you have, maybe have some temporary utility classes. – Joop Eggen Feb 20 '13 at 12:15
  • Is Java 8 in Android? – Johann Feb 20 '13 at 12:20

4 Answers4

3

JodaTime is one of external libraries that help manipulate times easily. You can add time. Also find the difference between times.

You also might want to check the difference between interval and duration which are concepts in jodatime lib. [SO post]

Refer its javadoc for more info

Community
  • 1
  • 1
mtk
  • 13,221
  • 16
  • 72
  • 112
2

try this approach

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,17);
cal.set(Calendar.MINUTE,30);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

Date d = cal.getTime();
SimpleDateFormat f =new SimpleDateFormat("hh:mm:ss aa");
System.out.println(f.format(d));
Azad
  • 5,047
  • 20
  • 38
  • That's what I am already doing. That's a horrible amount of code for working with just hours, minutes and seconds. See the code I pasted above that I just updated. – Johann Feb 20 '13 at 12:26
1

I think the java.util.Calendar gives you everything you need. Have a look at Calendar#set().

public final void set(int year, int month, int date, int hourOfDay, int minute, int second)

Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND. Previous values of other fields are retained. If this is not desired, call clear() first.

mtk
  • 13,221
  • 16
  • 72
  • 112
semTex
  • 343
  • 5
  • 16
  • I mentioned that in my post and stated how much work is required to move dates and times into and out of that class. I'm looking for something pretty simple. – Johann Feb 20 '13 at 12:19
0

I would prefer to use DateUtils from Commons Lang. This along with Calendar and DateFormat(SimpleDateFormat) should suffice all the date requirements in Java

Abhijith Nagarajan
  • 3,865
  • 18
  • 23