如何使用可选的时间参数创建DateFormat? [英] How do I create a DateFormat with an optional time argument?

查看:158
本文介绍了如何使用可选的时间参数创建DateFormat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望构建一个可以有时间参数的日期格式。

I wish to construct a date format that will optionally have a time argument.

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd [hh:mm]");

是否也可以构建能够解析不同格式的日期格式对象?例如尝试当前的区域设置,然后回到ISO-8601,或者如果一个失败,我应该写多个日期格式?

Is it also possible to construct a date format object that is capable of parsing different formats? Such as try the current locale but then fall back to ISO-8601 or should I just write multiple date formats if one fails?

更新:回顾这个问题,我可以看到,我没有指定多个日期格式的原因是用于解析字符串,而不是格式化日期,因此格式化日期对象的歧义对我来说不是一个问题。如果你考虑到这个时间部分是或不包括在解析字符串中。

UPDATE: Looking back at this question I can see I didn't specify that the reason for multiple date formats was for parsing strings, not for formatting a date, thus ambiguity for formatting date objects wasn't a concern for me. If you take this into account the time portion is or is not included in the parsing string.

推荐答案

SimpleDateFormat不会让你去做。它不支持(单一)格式的替代方案。

SimpleDateFormat won't let you do that. It doesn't support alternatives within a (single) format.

即使如此,也会有问题。使用这个

Even if it did, there would a problem. Consider using this

new SimpleDateFormat("yyyy-MM-dd [hh:mm]");

与使用

new SimpleDateFormat("yyyy-MM-dd hh:mm");
new SimpleDateFormat("yyyy-MM-dd ");

在第一种情况下,当我按照格式解析日期时,我无法区分差异通过查看发送给我的 Date 之间的2010-01-01和2010-01-01 00:00之间。在第二种情况下,我可以。

In the first case, when I parsed a date against the format, I couldn't tell the difference between "2010-01-01" and "2010-01-01 00:00" by looking at the Date delivered to me. In the 2nd case, I can.

在第一种情况下,当我在分钟和秒字段中格式化为零的日期时,不清楚结果是否应该以00:00结束...否则。在第二种情况下,这完全是在应用程序的手中。

In the first case, when I format a Date with zero in the minutes and seconds fields, it is not clear whether the result should end with "00:00" ... or not. In the second case, this is entirely in the hands of the application.

我想我在这里真正做的是提出日期和日期/时间的问题对于不同的人和不同的环境,意味着不同的东西。有时他们是指瞬间,有时间隔。有时缺乏表达的精确度意味着不精确,有时精确度是隐含的。

I guess that what I'm really doing here is raising the issue that dates and date/times mean different things to different people and in different contexts. Sometimes they mean instants and sometimes intervals. Sometimes a lack of expressed precision means imprecision, and sometimes that the precision is implied.

作为开发人员,我们必须在编写易于挑剔的软件和软件之间运行这对于用户实际意味着日期/时间值的假设是不正确的。让用户正确使用的第一步就是了解问题的复杂性。将变体重载为单个格式的字符串(将是)将毯子下的问题扫除。

As developers we have to run the line between writing software that is annoyingly picky, and software that makes incorrect assumptions about what the user actually means by a date / time value. The first step in getting it right for the user is understanding the complexity of the problem. Overloading variations into a single format string is (would be) sweeping the problem under the carpet.

这篇关于如何使用可选的时间参数创建DateFormat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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