简体   繁体   English

如何在Java中验证DateTime字符串格式“ 2018-01-22T18:23:00.000Z”?

[英]How to validate the DateTime string format “2018-01-22T18:23:00.000Z” in Java?

I need to validate in my code if the format of the DateTime string 2018-01-22T18:23:00.000Z is a valid one or not. 我需要在我的代码中验证DateTime字符串2018-01-22T18:23:00.000Z是否有效。

Regex solution or any other solution is fine.Can someone help me doing this? 正则表达式解决方案或任何其他解决方案都可以,有人可以帮我吗?

2018-01-22T18:23:00.000Z is the ISO 8601 format for an instant. 2018-01-22T18:23:00.000Z是即时的ISO 8601格式。 So you may just use Instant.parse("2018-01-22T18:23:00.000Z") . 因此,您可以只使用Instant.parse("2018-01-22T18:23:00.000Z") Catch a DateTimeParseException from the case where the string isn't valid, either because it's in the wrong format or the date and time is not valid (like month 13 or hour 25). 如果字符串格式错误或日期和时间无效(例如,第13个月或第25小时),则从字符串无效的情况下捕获DateTimeParseException It will accept 2018-01-22T18:23Z and 2018-01-22T18:23:00.000000000Z too. 它将也接受2018-01-22T18:23Z2018-01-22T18:23:00.000000000Z This should be OK for most purposes since it is still allowed within the ISO 8601 standard. 对于大多数目的,这应该可以,因为在ISO 8601标准中仍然允许这样做。

You may want to add a range check. 您可能要添加范围检查。 Probably instants that are too far into the past or the future should be considered invalid for your application. 可能距离过去或将来太远的瞬间对于您的应用程序而言无效。 Use Instant.isBefore() and/or Instant.isAfter() . 使用Instant.isBefore()和/或Instant.isAfter()

Don't use a regular expression. 不要使用正则表达式。 It will be complicated to write and very, very complicated to read for those maintaining your code. 对于那些维护您的代码的人来说,编写起来会复杂,而阅读起来会非常非常复杂。 If you do need more detailed syntax validation, use a DateTimeFormatter as already mentioned in Akshay Batra's answer . 如果确实需要更详细的语法验证,请使用Akshay Batra的答案中已经提到的DateTimeFormatter

Create a String called "format", and put the required format there. 创建一个名为“ format”的字符串,然后在其中放置所需的格式。 After that try below code and check if dt returns anything 之后,尝试下面的代码,并检查dt是否返回任何内容

DateTimeFormatter formatter = DateTimeFormat.forPattern(format);//required format
LocalDateTime dt = formatter.parse(oldstring);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将日期时间转换为以前的格式2019-02-28T11:30:00.000Z? - How to convert datetime to ago format 2019-02-28T11:30:00.000Z? Apache Axis-序列化为0001-01-01T00:00:00.000Z的日历实例 - Apache Axis - Calendar instance that gets serialized to 0001-01-01T00:00:00.000Z 我收到此错误 java.text.ParseException: Unparseable date: “2017-03-09T18:30:00.000Z” - i am getting this error java.text.ParseException: Unparseable date: “2017-03-09T18:30:00.000Z” 如何将“2020-12-20T00:00:00.000Z”转换为 java.util.Date? - How to convert “2020-12-20T00:00:00.000Z” to java.util.Date? java.text.ParseException:无法解析的日期:“ 20:01:00.000Z” - java.text.ParseException: Unparseable date: “20:01:00.000Z” java/Kotlin 中的“2020-05-08T11:01:48.3300000Z”日期时间字符串将日期字符串格式化为 m/d/yyyy 的正确方法是什么 - What is the correct way to format a date string to m/d/yyyy from “2020-05-08T11:01:48.3300000Z” datetime string in java/ Kotlin 如何格式化此日期“ 2018-08-02T00:00:00 + 05:30”字符串为此“ 2018-18-02” - How to Format this Date “2018-08-02T00:00:00+05:30” String to this “2018-18-02” 怎么把“ 01/30/2018 02:29:23 PM”转换成smalldatetime格式? 我正在为我的应用程序使用MSSQL,列为smalldatetime格式 - How to convert “01/30/2018 02:29:23 PM” into smalldatetime format? I am using MSSQL for my application and the column is smalldatetime format 如何以hh:mm格式在dd / mm / yyyy中格式化此字符串“ 2015-06-22T09:40:30 + 01:00”? - How to format this String “2015-06-22T09:40:30+01:00” in dd/MM/yyyy at hh:mm format? 如何在 java 中格式化 2021-12-17T06:23:49.000000Z - How to format 2021-12-17T06:23:49.000000Z in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM