Java Utililty Methods Duration Parse

List of utility methods to do Duration Parse

Description

The list of methods to do Duration Parse are organized into topic(s).

Method

StringparseDuration(final Duration duration)
parse Duration
return parseTimeString(duration.toString());
DurationparseDuration(String datePattern)
Parses patterns like:
  • 1min or 10mins
  • 1day or 10days
  • 1week or 4weeks
  • 1month or 12months
  • 1year or 5years
final int index = findIndexOfNonDigit(datePattern);
if (index == -1) {
    throw new IllegalStateException("Incorrect time format given: " + datePattern);
try {
    final int val = Integer.parseInt(datePattern.substring(0, index));
    final String type = datePattern.substring(index);
    final ChronoUnit unit;
...
DurationparseDurationString(String time)
parse Duration String
Pattern timePattern = Pattern.compile("[1-9][0-9]*(y|mo|w|h|d|m|s)");
Matcher matcher = timePattern.matcher(time);
int years = 0;
int months = 0;
int weeks = 0;
int days = 0;
int hours = 0;
int minutes = 0;
...