Java自定义日期参数输入 [英] Java Custom Date Argument Input

查看:490
本文介绍了Java自定义日期参数输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个用于管理禁令的 Spigot / Craftbukkit插件。在一种方法中,我有 TempBan命令执行程序。我的目标是让用户使用自定义的速记格式指定禁止时长。
例如: / tempban MyUser 5d2w我的原因

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"

我要为字符串 5d2w 可以在MilliSeconds中解析并返回。我尝试自己制作,但它不可靠,仅支持一种时间格式,因此您无法进行组合。

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



tl;dr

Duration.parse( "PT1H30M" ).toMillis()



ISO 8601



无需发明自己的自定义速记格式。 ISO 8601 标准已经定义

模式 PnYnMnDTnHnMnS 使用 P 标记开始ng, T 将任何年-月-日-日部分与任何时-分-秒部分分开。

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.

示例:


  • 一个半小时是 PT1H30M

  • P3Y6M4D 表示三年零六个月四天。

  • An hour and a half is PT1H30M.
  • P3Y6M4D represents "three years, six months, four days".

在解析/生成字符串时,java.time类默认使用ISO 8601格式。这包括 Period Duration 类,用于表示未附加在时间轴上的时间跨度。

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 d = Duration.ofHours( 1 ).plusMinutes( 30 );
String output = d.toString();




PT1H30M

PT1H30M

并进行解析。

Duration d2 = Duration.parse( "PT1H30M" );

您可以要求持续时间总计毫秒。

You can ask for the duration a total number of milliseconds.

long millis = d2.toMillis();




5400000

5400000

请参见 IdeOne.com中的实时代码

但是请记住,java.time类具有更好的分辨率(纳秒)。因此,在请求毫秒数时,您可能会丢失数据。

But remember that java.time classes have much finer resolution, nanoseconds. So you may be losing data when asking for milliseconds.

此外,我强烈建议您坚持使用java.time对象和ISO 8601字符串,并避免将日期时间值表示为计数

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" );

…和…

String output = p.toString();




P3Y6M4D

P3Y6M4D

请注意此类的归一化 方法。例如,将 1年15个月的期限归一化为 2年3个月。

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".

还请注意,期间建立在 LocalDate 信息的基础上。因此,它没有时区的概念,也没有关于异常的任何想法,例如夏令时(DST)。

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.

java .time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧旧版日期时间类,例如 java.util.Date Calendar ,& SimpleDateFormat

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.

现在处于维护模式的Joda-Time 项目建议迁移到 java.time 类。

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参见 Oracle教程。并在Stack Overflow中搜索许多示例和说明。规范为 JSR 310

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

从哪里获取java.time类?

Where to obtain the java.time classes?


  • Java SE 8 SE 9 和后来的


    • 内置。

    • 具有捆绑实现的标准Java API的一部分。

    • Java 9添加了一些次要功能和修复。

    • 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.
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

      ThreeTen-Extra 项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容打下了基础。您可能会在这里找到一些有用的类,例如 时间间隔 YearWeek YearQuarter 更多

      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.

      这篇关于Java自定义日期参数输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆