How to handle time in Java [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it开发者_C百科 can be answered with facts and citations.
Closed 5 years ago.
Improve this questionI'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.
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.
Joda Time is THE reference for handling date in Java.
Have a look at Quartz, it s a powerful cron like system for Java.
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());
}
}
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.
have a look at Joda
Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API. The 'default' calendar is the ISO8601 standard which is used by XML. The Gregorian, Julian, Buddhist, Coptic, Ethiopic and Islamic systems are also included, and we welcome further additions. Supporting classes include time zone, duration, format and parsing.
java.time
Use the java.time classes classes found bundled with Java 8 and later and back-ported to earlier versions.
Duration
To represent a span of time with a granularity of seconds-minutes-hours-days, use Duration
.
Period
To represent a span of time with a granularity of days-months-years, use Period
.
ISO 8601
Also, study up on standard ISO 8601 formats for strings representing date-time values.
For spans of time, the standard uses the format PnYnMnDTnHnMnS
where P
marks the beginning and T
separates any years-months-days from any hours-minutes-seconds. So an hour and a half is PT1H30M
.
The java.time classes use ISO 8601 standard formats by default when parsing and generating strings.
String output = duration.toString();
PT1H30M
Duration duration = Duration.parse( "PT1H30M" );
ThreeTen-Extra
See the ThreeTen-Extra project for more classes such as Interval
, a plural amount of Days
& Weeks
& Months
& Years
, quarters, and standard ISO 8601 weeks.
精彩评论