Searching for a "TimeInterval" type...

  • Thread starter Andreas Leitgeb
  • Start date
A

Arne Vajhøj

Having worked with with JODA in the past, I *strongly* recommend you
made use of it, regardless of the circumstances.

Joda has a very good reputation.

I have used Joda at a few occasions and I must admit that I find
it over-engineered.

Arne
 
A

Arne Vajhøj

Maybe two classes RelativeTime and AbsoluteTime so your
problem could be one RelativeTime with 1 HOUR and one
AbsoluteTime with HOUR X (where X is now hour minus one).

Illustration:

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class TimeManipulation {
public static interface TimeUpdate {
public void applyTo(Calendar cal);
}
public static class AbsoluteTimeUpdate implements TimeUpdate {
private int value;
private int field;
public AbsoluteTimeUpdate(int value, int field) {
this.value = value;
this.field = field;
}
@Override
public void applyTo(Calendar cal) {
cal.set(field, value);
}
}
public static class RelativeTimeUpdate implements TimeUpdate {
private int value;
private int field;
public RelativeTimeUpdate(int value, int field) {
super();
this.value = value;
this.field = field;
}
@Override
public void applyTo(Calendar cal) {
cal.add(field, value);
}
}
private List<TimeUpdate> upds;
public TimeManipulation(TimeUpdate... upds) {
this.upds = new ArrayList<TimeUpdate>();
for(TimeUpdate upd : upds) this.upds.add(upd);
}
public void applyTo(Calendar cal) {
for(TimeUpdate upd : upds) {
upd.applyTo(cal);
}
}
public static void main(String[] args) {
TimeManipulation tm = new TimeManipulation(new RelativeTimeUpdate(1,
Calendar.DAY_OF_MONTH),
new RelativeTimeUpdate(-1,
Calendar.HOUR_OF_DAY));
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime());
tm.applyTo(cal);
System.out.println(cal.getTime());
}
}

Arne
 
A

Andreas Leitgeb

Arne Vajhøj said:
Maybe two classes RelativeTime and AbsoluteTime so your
problem could be one RelativeTime with 1 HOUR and one
AbsoluteTime with HOUR X (where X is now hour minus one).
Illustration: [...]

Thanks for the illustration, because I wasn't really able to
follow you on the Relative/Absolute distinction in that context.
(maybe I was also confused by the "RelativeTime with 1 HOUR")

Now, that I think I see what you meant, I can say, that I wouldn't
need the "AbsoluteTimeUpdate", at least not for my intervals.

When I was speaking of "tomorrow same wallclocktime" part I was NOT
thinking of "adding 24 hours, then setting the hour to old hour value."
While Calendar might internally do something like that for adding days,
*I* don't have to code that explicitly. For me as a user of Calendar
it's just add()s for each relevant field.

For any real mixture of set()ting and add()ing fields at the same
level, I agree to all the warnings about its imminent fragility.
 
A

Arne Vajhøj

Arne Vajhøj said:
Maybe two classes RelativeTime and AbsoluteTime so your
problem could be one RelativeTime with 1 HOUR and one
AbsoluteTime with HOUR X (where X is now hour minus one).
Illustration: [...]

Thanks for the illustration, because I wasn't really able to
follow you on the Relative/Absolute distinction in that context.
(maybe I was also confused by the "RelativeTime with 1 HOUR")

Now, that I think I see what you meant, I can say, that I wouldn't
need the "AbsoluteTimeUpdate", at least not for my intervals.

It may not be a perfect fit for what you need.

But as a data structure changing a Calendar I think it is
both simple and powerful.

Arne
 
D

Daniele Futtorovic

Joda has a very good reputation.

I have used Joda at a few occasions and I must admit that I find
it over-engineered.

I don't know about that. All that date and time stuff is nigh-insanely
complicated. Relative to that, Joda-Time is rather simple, and it does
the job. I have also found it pleasurable to use, in terms of coding.
YMMV with regards to the latter, but chances are it won't w.r.t. the
former; hence my unadorned recommendation.
 
S

Sebastian

Am 02.10.2013 16:42, schrieb Andreas Leitgeb:
Thanks to mark, Arne& Sebastian for pointing towards Joda and
future Java 8. It's good to know that this swamp is getting dried,
but I'll probably not hold my breath for it, and I'll probably stay
with Calendar for now, but at least I'm now aware to not fully embrace
it, but only pick minimal functionality from it. I hope I will then be
able to eventually translate to new date-API once Java 8 arrives.
[snip]

It just came to my attention that there is a JDK 1.7 backport of the new
Date and Time API. On the JavaOne 2013 pages there are downloadable
presentations that explain the API. The backport itself is at:
http://threeten.github.io/

-- Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top