如何初始化公历,日期为 YYYY-MM-DD 格式?
我有一个包含变量的类
private GregorianCalendar cal;
现在我想用 yyyy-mm-dd
格式的日期对其进行初始化,我尝试使用默认构造函数,但得到的输出类似于 "2017-05-25T14:36:52+03:00"
,我只想要 "2017-05-25"
日期部分?
Now I want to initialize it with a date in yyyy-mm-dd
format, I tried using the default constructor but the output I get is something like this "2017-05-25T14:36:52+03:00"
, I only want "2017-05-25"
date part?
如何实现?
推荐答案
首先,您可能会讨厌我的重复,您需要了解日期和该日期的字符串表示之间存在差异;基本数据和它的字符串表示之间的区别.
First, you may hate me for reiterating, you need to understand that there’s a difference between a date and a string representation of that date; a difference between the fundamental data and a string representation of it.
其次,如果您愿意,可以在 Java 7 中使用 LocalDate
(以及其他 Java 日期和时间类).它(它们都在)在 ThreeTen-Backport ,将 Java SE 8 日期时间类向后移植到 Java SE 6 和 7.
Second, you can use LocalDate
(and the other Java date and time classes) with Java 7 if you want. It is (and they are all) in the ThreeTen-Backport , a back-port of the Java SE 8 date-time classes to Java SE 6 and 7.
由于我收集到 LocalDate
更符合您的要求很多,我真的认为您应该考虑一下.所以我建议代替你的 cal
Since I gather that LocalDate
fits your requirements much better, I really think you should give it a thought or two. So instead of your cal
I suggest
private LocalDate date = LocalDate.now(ZoneId.systemDefault());
还要考虑您是想要 JVM 的当前时区设置(如上所示)还是想要控制您使用的时区.它会有所作为.
Also think about whether you want the current time zone setting of your JVM (as the above will give you) or you want to control which time zone you use. It will make a difference.
最后,如果你真的坚持.正如你现在所理解的,我不能给你一个字符串in一个GregorianCalendar
.您可以丢弃 GregorianCalendar
的时间部分,因此您只有日期部分.你可以把它格式化成你喜欢的字符串.
Finally, if you really insist. As you have understood by now, I cannot give you a string in a GregorianCalendar
. You may discard the time part of your GregorianCalendar
so you only have the date part. And you may format it into a string of your liking.
public class GregorianCalendarDemo {
private GregorianCalendar cal;
public GregorianCalendarDemo() {
cal = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
// discard time of day so we only have the date
cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
}
protected GregorianCalendar getCal() {
return cal;
}
public String getFormattedCal() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(getCal().getTime());
}
}
当我刚刚调用 getFormattedCal()
时,它返回了 2017-05-25
.
When I just called getFormattedCal()
, it returned 2017-05-25
.
再次,确定时区和语言环境的默认值是否合适,或者您想要其他值.
Again, decide whether default values for time zone and locale are fine or you want something else.
您可能认为我们可以只使用 cal.set(Calendar.HOUR_OF_DAY, 0):
来丢弃小时,同样使用分钟和秒.它至少适用于所有情况的 99%.但是,随着夏令时(夏令时)的过渡,一天不能保证从 0 小时开始,所以上面的代码更加安全.
You might have thought that we could discard the hours with just cal.set(Calendar.HOUR_OF_DAY, 0):
, and similarly with minutes and seconds. It would work in 99 % of all cases at least. However, with transistion to summer time (daylight savings time), the day is not guaranteed to begin at 0 hours, so the above code is more bulletproof.
ThreeTen Backport 主页
相关文章