简体   繁体   English

java中任何形式的日期实现的格式日期

[英]Format date of any form of date implementation in java

I looking for a function which will format date into given format if type of date or any other implementation else throw exception我正在寻找一个函数,如果日期类型或任何其他实现抛出异常,该函数会将日期格式化为给定格式

Eg: formatDate(java.util.Date date) Or formatDate(org.joda.time.LocalDateTime) or formatDate(any valid date implementation) eg: java.time.LocalDateTime , javax.xml.datatype.XMLGregorianCalendar or other date time implementation例如: formatDate(java.util.Date date) 或 formatDate(org.joda.time.LocalDateTime) 或 formatDate(任何有效的日期实现) 例如: java.time.LocalDateTime 、 javax.xml.datatype.XMLGregorianCalendar 或其他日期时间实现

I cannot be sure for the type of date i receive so don't ask me to implement same date type everywhere in project我不能确定我收到的日期类型,所以不要让我在项目中的任何地方实现相同的日期类型

LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); for example will format into a string such as: 2019-07-25T15:05:54.432例如将格式化为字符串,如:2019-07-25T15:05:54.432

Other formats are available, practically every standard format you could want is included as a static constant in the class.其他格式也可用,实际上您可能需要的所有标准格式都作为静态常量包含在类中。 However, it also supports custom patterns, the following page explains how to create these: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html但是,它也支持自定义模式,以下页面解释了如何创建这些模式: https : //docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Two options that I would at least consider:我至少会考虑两种选择:

  1. One method that takes an Object argument.一种采用Object参数的方法。 Inside the method do a long chain of if - else using instanceof to find the right type.在方法内部做一长串if - else使用instanceof来找到正确的类型。 If you want to support the java.sql types that extend java.util.Date (an awful hack), remember to test for those first because instanceof java.util.Date will also be true.如果你想支持扩展java.util.Datejava.sql类型(一个可怕的黑客),记得先测试那些类型,因为instanceof java.util.Date也将是真的。 Down side: Your user will not get a compile-time error for passing an unsupported type and may find it difficult to find out why your method throws an exception given a String or an Integer , for example (which may represent dates too).不利的一面:您的用户不会因为传递不受支持的类型而收到编译时错误,并且可能会发现很难找出您的方法在给定StringInteger抛出异常的原因,例如(也可能表示日期)。
  2. An overloaded method for each supported type, as @MarkB suggested in a comment.每个受支持类型的重载方法,如@MarkB 在评论中建议的那样。 Down side: You need to modify your API for each new supported type added.缺点:您需要为每个添加的新支持类型修改 API。

Whether you choose one option or the other, convert whatever you get to something that implements TemporalAccessor and pass it to DateTimeFormatter.format() .无论您选择一个选项还是另一个,将您获得的任何内容转换为实现TemporalAccessor并将其传递给DateTimeFormatter.format()

Just mentioned as a curiosity, String.format() already implements some of the functionality you envision.刚刚提到的好奇心, String.format()已经实现了你想象的一些功能。 It handles TemporalAccessor , Calendar , Date and Long .它处理TemporalAccessorCalendarDateLong

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM