I am currently writing a Spigot/Craftbukkit plugin that will manage bans. In one method, I have the TempBan Command Executor. My goal is for a user to specify a ban duration using a custom "shorthand" format.
For example: /tempban MyUser 5d2w My Reason"
I would like for the string 5d2w to be parsed and returned in MilliSeconds. I have tried to make it myself, but it was unreliable and only supported one time format. So you couldn't do combinations. Is there a good efficient way to use JodaTime or Java's default date format class to accomplish this? Thanks!
tl;dr
ISO 8601
No need to invent your own custom shorthand format. The ISO 8601 standard already defines a similar format for durations.
The pattern
PnYnMnDTnHnMnSuses aPto mark the beginning, aTto separate any years-months-days portion from any hours-minutes-seconds portion.Examples:
PT1H30M.P3Y6M4Drepresents “three years, six months, four days”.java.time
The java.time classes use the ISO 8601 formats by default when parsing/generating strings. This includes the
PeriodandDurationclasses for representing spans of time not attached to the timeline.DurationAnd parsing.
You can ask for the duration a total number of milliseconds.
See live code in IdeOne.com.
But remember that java.time classes have much finer resolution, nanoseconds. So you may be losing data when asking for milliseconds.
Also, I strongly suggest you stick to using the java.time objects and the ISO 8601 strings and avoid representing date-time values as a count of milliseconds or such.
PeriodFor years-months-days, use the
Periodclass.…and…
Note the
normalizedmethod on this class. For example, a period of "1 Year and 15 months" will be normalized to "2 years and 3 months".Also note that a
Periodis built onLocalDateinfo. As such it has no concept of time zones nor any idea about anomalies such as Daylight Saving Time (DST).This class works with a resolution of whole days. So the class does not provide an explicit way to count milliseconds.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date,Calendar, &SimpleDateFormat.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval,YearWeek,YearQuarter, and more.