Skip to main content
gamedev.net
🔒 Locked

[java] Relative date/time

Started by SteveTaylor Dec 26, 2006 at 12:56 PM 3 replies 850+ views
Original Post
SteveTaylor
SteveTaylor
This question isn't really game related, but certainly Java related. I'm building a Java EE-based rostering system for work. I've sorted out the basic data design for employees, sites and shifts. Shifts have a start and end time, which are java.util.Date instances (how they're persisted I don't really know or care). What I want to also do is store some kind of ongoing weekly roster. This would require storing relative start and end times. The only problem is that the Java API doesn't seem to have a way to do this, as far as I can tell. What would be the best way to go about this? I was thinking just using an arbitrary absolute date (let's say Monday 2000-01-03). Or perhaps I could use millisecond offsets since the beginning of the week, but that wouldn't really seem like the 'correct' method to use. Any suggestions?
capn_midnight
capn_midnight
First, a lot of stuff in the Date class is deprecated. Are you making sure to use GregorianCalendar?

Second, GregorianCalendar has a method add(int field, int amount). Field is a flag to select what part of the date you're changing (Milliseconds, Seconds, Minutes, Hours, Days, Months, Years, etc.).

Now, .NET already has a TimeSpan struct that can be added or subtracted from DateTime objects to manipulate time. Unfortunately, Java doesn't have such a class already. With the GregorianCalender and the add method, it shouldn't be too hard to write one.

Just off the cuff:
class TimeSpan{    public TimeSpan(int millis, int seconds, int minutes, int hours, int days, int months, int years){        //snip setting member variables of the same name    }    public void alterCalendar(GregorianCalendar calendar)    {        calendar.add(Calendar.MILLISECOND, millis);        calendar.add(Calendar.SECOND, seconds);        calendar.add(Calendar.MINUTE, minutes);        //etc...    }}
SteveTaylor
SteveTaylor
Hi capn_midnight. Thanks for your reply. I think I'll implement something like TimeSpan as a wrapper around a long millisecond value. I'll use only day, hour, minute, second and millisecond.

import java.util.*;// Intended only for short time offsets (up to 24 days)public class TimeSpan {	private int offset;		public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds) {		offset = milliseconds + 1000 * seconds + 60000 * minutes + 3600000 * hours + 86400000 * days;	}		public TimeSpan(int milliseconds) {		this.offset = milliseconds;	}		public Calendar addToCalendar(Calendar cal) {		cal.add(Calendar.MILLISECOND, offset);		return cal;	}		public int getOffset() {		return offset;	}}


[Edited by - SteveTaylor on December 26, 2006 11:56:13 PM]
SteveTaylor
SteveTaylor
As a convenience, so I can do a one-liner instead of a two-liner. I know Code Complete tells me that's naughty, but it also tells me I shouldn't follow all it's advice religiously. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.