java 自定义date,Java自定义日期参数输入

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值