views:

221

answers:

6

Hi,

I'm working on a functionality related to job scheduling in Java, where I'm required to schedule jobs based on days, weeks, or hours.

I'm running into 2 problems:

  • What is a good representation/library to handle a duration of time (not date)?

  • What is a good library to parse a text representation of time, i.e. 2d 3wk for 3 weeks and 2 days? similary to what JIRA has for their.


I'm thinking this must've been done before, but I can't seem to find the correct word to google it.

+3  A: 

Joda Time is THE reference for handling date in Java.

Valentin Rocher
I remember using this for an application I made to count the hours I'd have to sleep, which would sit in the system tray and warn me when I was going under my allocated sleep quota :) It was pretty cool and handled everything.
Chris Dennett
A: 

have a look at Joda

objects
+12  A: 

The JODA time library http://joda-time.sourceforge.net/ gives some nice Java time functionality. You may have to write some regular expressions to parse the type of text strings you're talking about though.

For scheduling the jobs, the Quartz scheduler http://www.opensymphony.com/quartz/;jsessionid=LDKHONNCOPJC may be useful to you.

Jeff Storey
JODA time seems to be the standard, and it's looking good from the API too. Thanks object and Valentin Rocher, I'll tick Jeff for the most detailed answer.
John
+2  A: 

Have a look at Quartz, it s a powerful cron like system for Java.

Jack Leow
A: 

The nicest way to use Quartz is probably by using the interface to it that Spring Framework provides, here's a link to the reference manual.

Hans Westerbeek
A: 

You could parse a jira style time string into seconds using Joda time using something like this:

import org.joda.time.format.*;

import org.joda.time.; import java.util.;

public class JiraStyleTimeParser {

public static void main(String[] args)
{
String example = "1h 1m 30s";

MutablePeriod parsedPeriod = new MutablePeriod();

PeriodFormatter formatter = new PeriodFormatterBuilder()
    .appendDays().appendSuffix("d")
    .appendSeparator(" ")
    .appendHours().appendSuffix("h")
    .appendSeparator(" ")
    .appendMinutes().appendSuffix("m")
    .appendSeparator(" ")
    .appendSeconds().appendSuffix("s")
    .printZeroAlways()
    .toFormatter();


PeriodParser parser = new PeriodFormatterBuilder()
    .appendDays().appendSuffix("d")
    .appendSeparator(" ")
    .appendHours().appendSuffix("h")
    .appendSeparator(" ")
    .appendMinutes().appendSuffix("m")
    .appendSeparator(" ")
    .appendSeconds().appendSuffix("s")
    .printZeroAlways()
    .toParser();

int working = parser.parseInto(parsedPeriod, example,0, new Locale("en"));
System.out.println(formatter.print(parsedPeriod));

Duration theduration = parsedPeriod.toPeriod().toStandardDuration();
System.out.println("period in seconds: " + theduration.getStandardSeconds());

}

}

Harrypotter2k5