skip to main content
Programmer's documentation > Developing with the JViews Gantt SDK > Introducing the main classes > Time and duration
 
Time and duration
Throughout the JViews Gantt product, time is represented by the standard java.util.Date class, duration by IlvDuration, and time intervals by IlvTimeInterval.
The following figure shows the classes for Manipulating Time and Duration.
Date
The standard java.util.Date class is capable of representing an instant in time with millisecond precision, starting from January 1, 1970. As of JDK 1.1, all methods that can modify a Date instance have been deprecated. JViews Gantt considers dates to be immutable and a separate java.util.Calendar object must be used to perform date arithmetic. Some useful arithmetic methods, such as add and subtract, are bundled into the utility classes IlvCalendarUtil and IlvTimeUtil. These methods always return a new Date instance instead of modifying the original object.
You can create a Date instance using one of the following constructors:
*Date()
*Date(long millis)
The following methods can be used to compare or perform arithmetic on dates:
*Date IlvTimeUtil.add(Date date, IlvDuration delta)
*Date IlvTimeUtil.subtract(Date date, IlvDuration delta)
*IlvDuration IlvTimeUtil.subtract(Date date1, Date date2)
*Date IlvCalendarUtil.min(Date a, Date b)
*Date IlvCalendarUtil.max(Date a, Date b)
The following methods can be used to compare or perform arithmetic on dates:
The following example shows how to create a Date that represents 8:00 am on April 4, 2001:
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(2001, Calendar.APRIL, 4, 8, 0);
Date date = calendar.getTime();
NOTE An instance of the java.util.Calendar class initializes all its time fields to the current time. You must explicitly clear those Calendar fields that you want set to zero. In the previous example, calling the clear method ensures that the second and millisecond fields of the Calendar object are set to zero.
IlvDuration
The IlvDuration class creates duration objects that represent a length of time with millisecond precision. Like Date, IlvDuration is an immutable class. Therefore, to create a different duration, you must create a new IlvDuration object. The class IlvDuration has a single constructor that takes the length of time expressed in milliseconds:
IlvDuration(long millis)
The IlvDuration class also has several convenient static constants that represent commonly used time spans:
*IlvDuration.ONE_SECOND
*IlvDuration.ONE_MINUTE
*IlvDuration.ONE_HOUR
*IlvDuration.ONE_DAY
*IlvDuration.ONE_WEEK
There are also several methods you can use to perform arithmetic on durations:
*Date add(Date date)
*IlvDuration add(IlvDuration delta)
*IlvDuration subtract(IlvDuration delta)
*IlvDuration multiply(int multiplier)
The following example shows how to create a duration of three weeks:
IlvDuration threeWeeks = IlvDuration.ONE_WEEK.multiply(3);
IlvTimeInterval
The IlvTimeInterval class creates time objects that represent an interval of time between a start time and an end time.
You can create time intervals by using the constructors:
*IlvTimeInterval(Date start, Date end)
*IlvTimeInterval(Date start, IlvDuration duration)
The following example shows how to create a time interval that starts on February 15, 2001 and lasts for one week:
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(2001, Calendar.FEBRUARY, 15);
Date start = calendar.getTime();
IlvTimeInterval interval = new IlvTimeInterval(start,
                                               IlvDuration.ONE_WEEK);
Unlike the Date and IlvDuration classes, the IlvTimeInterval class is mutable.
You can manipulate a time interval using the following methods:
*Date getStart()
*void setStart(Date t)
*Date getEnd()
*void setEnd(Date t)
*void setInterval(Date start, Date end)
*void setInterval(Date start, IlvDuration duration)
*IlvDuration getDuration()
*void setDuration(IlvDuration duration)
*boolean overlaps(IlvTimeInterval interval)
*boolean contains(Date time)

Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.