How to handle time in Java

I am working on functionality related to scheduling tasks in Java, where I need to assign tasks by day, week or hour.

I ran into two problems:

  • What is a good view / library for handling length of time (not date)?

  • What is a good library for parsing textual representation of time, i.e. 2d 3wk for 3 weeks and 2 days? similar to what JIRA has for its own.


I think this must have been done before, but I cannot find the right word for its google.

+4
source share
7 answers

The JODA Time Library http://joda-time.sourceforge.net/ provides excellent Java functionality. You may need to write some regular expressions to parse the types of text strings you are talking about.

You can use the Quartz Scheduler http://www.opensymphony.com/quartz/;jsessionid=LDKHONNCOPJC to schedule tasks.

+14
source

Joda Time is a reference for processing dates in Java.

+3
source

Check out Quartz , it's a powerful cron system for Java.

+2
source

You can play the jira style time string in seconds using Joda's 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()); } 

}

+1
source

look joda

Joda-Time provides quality date and time replacement for Java classes. The design allows the use of multiple calendar systems, providing a simple API. The default calendar is the ISO8601 standard used by XML. Gregorian, Julian, Buddhist, Coptic, Ethiopian and Islamic systems are also included, and we welcome further additions. Helper classes include time zone, duration, format, and parsing.

0
source

The most enjoyable way to use Quartz is probably to use the interface for it, which the Spring Framework provides here with a link to a reference guide .

0
source

java.time

Use the java.time class classes that were found bundled with Java 8 and later and migrated back to earlier versions.

Duration

To represent a time span with granularity of seconds-minutes-hours-days, use Duration .

Period

To represent a time span with granularity of days-months-years, use Period .

ISO 8601

Also study the standard ISO 8601 formats for strings representing date and time values.

For time periods, 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 - PT1H30M .

The java.time classes use the standard ISO 8601 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 , the plurality of Days and Weeks and Months and Years , quarters and standard ISO 8601 weeks .

0
source

Source: https://habr.com/ru/post/1301643/


All Articles