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
Duration.parse( "PT1H30M" ).toMillis()
ISO 8601
No need to invent your own custom shorthand format. The ISO 8601 standard already defines a similar format for durations.
The pattern PnYnMnDTnHnMnS uses a P to mark the beginning, a T to separate any years-months-days portion from any hours-minutes-seconds portion.
Examples:
An hour and a half is PT1H30M.
P3Y6M4D represents “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 Period and Duration classes for representing spans of time not attached to the timeline.
Duration
Duration d = Duration.ofHours( 1 ).plusMinutes( 30 );
String output = d.toString();
PT1H30M
And parsing.
Duration d2 = Duration.parse( "PT1H30M" );
You can ask for the duration a total number of milliseconds.
long millis = d2.toMillis();
5400000
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.
Period
For years-months-days, use the Period class.
Period p = Period.parse( "P3Y6M4D" );
…and…
String output = p.toString();
P3Y6M4D
Note the normalized method 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 Period is built on LocalDate info. 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?
Java SE 8 and SE 9 and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
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.