java 如何在 GWT 中检查时间戳的正则表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14662597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:00:43  来源:igfitidea点击:

How to check Regular Expression for Timestamp in GWT?

javaregexgwt

提问by StackOverFlow

What will be the regular expression for following Timestamp format

以下时间戳格式的正则表达式是什么

YYYY-MM-DD HH:mm:ss.S

YYYY-MM-DD HH:mm:ss.S AM/PM

YYYY-MM-DD HH:mm:ss.S AM/PM Z

YYYY-MM-DD HH:mm:ss.S Z

Where

在哪里

Y: year, 
M: Month,
D: Date,
H: hour,
m: minute,
s: second,
S: Milisecond 3 digit only,
Z: Time zone.

I am getting timestamp format in string format so want to validate it.

我正在以字符串格式获取时间戳格式,因此想对其进行验证。

How to check above regular expression in GWT?

如何在 GWT 中检查上述正则表达式?

回答by guido

Just something simple as only describing the pattern like this:

只是一些简单的事情,只描述这样的模式:

^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}(?: [AP]M)?(?: [+-]\d{4})?$

as any tentative of real date validation with a regex sounds inherently wrong.

因为任何尝试使用正则表达式进行的实际日期验证听起来本质上都是错误的。

I got the uppercase Z as RFC822 timezone, regex needs changes to comply to TZDs or general textual time zones.

我得到了大写的 Z 作为 RFC822 时区,正则表达式需要更改以符合 TZD 或一般文本时区。

回答by Manolo Carrasco Mo?ino

Apart from Datejswhich relays on js to check a date-string, Gwt comes with DateTimeFormatto parse date-string and format dates with support for locales. It raises a IllegalArgumentExceptionin the case the parsed string doesn't match the expected format .

除了通过Datejsjs 检查日期字符串之外,Gwt 还带有DateTimeFormat来解析日期字符串和格式化日期并支持语言环境。它IllegalArgumentException在解析的字符串与预期格式不匹配的情况下引发 a 。

String dateStr = "2011-04-21 20:37:36.999 -0800";
String fmt = "yyyy-MM-dd HH:mm:ss.S Z"; // your 4th case: YYYY-MM-DD HH:mm:ss.S Z
DateTimeFormat format = DateTimeFormat.getFormat(fmt);
try {
  Date date = format.parse(dateStr);
  System.out.println("Validated: " + date);
} catch (IllegalArgumentException e) {
  System.out.println("Validation error: " + e.getMessage());
}

dateStr = "2011-04-21 08:37:36.999 PM -0800"; // your 3rd case YYYY-MM-DD HH:mm:ss.S AM/PM Z
fmt = "yyyy-MM-dd hh:mm:ss.S a Z";
format = DateTimeFormat.getFormat(fmt);
try {
  Date date = format.parse(dateStr);
  System.out.println("Validated: " + date);
} catch (IllegalArgumentException e) {
  System.out.println("Validation error: " + e.getMessage());
}

You dont say whether the format string is fixed or it can be provided in runtime before performing the validation. So in the second case you need to use replaceto change 'Y' by 'y', and 'AM/PM' to 'a' which are the symbols used in DateTimeFormat

你没有说格式字符串是固定的还是可以在执行验证之前在运行时提供。因此,在第二种情况下,您需要使用replace将 'Y' 更改为 'y',并将 'AM/PM' 更改为 'a',这些符号是DateTimeFormat

回答by MrSimpleMind

I would say use Datejs

我会说使用Datejs

Otherwise you will need to do a lot of coding, and regex is not the best for verifying timestamps and if it is valid. Datejs will check the date validity, and from that you will receive a Date object or null (if it is invalid!).

否则,您将需要进行大量编码,并且正则表达式不是验证时间戳及其是否有效的最佳选择。Datejs 将检查日期有效性,然后您将收到一个 Date 对象或 null(如果它无效!)。

Date.parse("2013-02-02 12:01:01.000", "yyyy-MM-dd HH:mm:ss.u");

Date.parse("2013-02-02 12:01:01.000", "yyyy-MM-dd HH:mm:ss.u");

For more information see:

有关更多信息,请参阅:

Datejs API documentation

Datejs Format spec

Datejs API 文档

Datejs格式规范