Java日期更改格式 [英] Java Date changing format

查看:89
本文介绍了Java日期更改格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改Date对象的格式,我正在尝试通过以下方式进行操作:

I am trying to change the format of Date objects, I am trying to do it in this way:

for(Date date : dates){
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    String formatterDate = formatter.format(date);
    Date d = formatter.parse(formatter.format(date));                      
}

但这对d对象没有任何影响,它仍然是旧格式,无法真正理解为什么会这样.

But this does not have any effect on the d object, it is still with the old format, can't really understand why it is like that.

推荐答案

请尝试将两个概念区分开:您的数据和向用户呈现的数据(或用于其他目的的格式设置,例如包含在JSON中).保持值为7的int可以表示为(格式化为)7、07、007或+7,而仍然保持相同的值而没有任何格式信息-格式位于int之外.同样,Date保留一个时间点,它可以显示为(格式为"2017年6月1日,12:46:01.169","2017/06/01"或"2017年6月1日")只是保持相同的值而没有任何格式信息-格式位于Date之外.

Please try to keep two concepts apart: your data and the presentation of the data to your user (or formatting for other purposes like inclusion in JSON). An int holding the value 7 can be presented as (formatted into) 7, 07, 007 or +7 while still just holding the same value without any formatting information — the formatting lies outside the int. Just the same, a Date holds a point in time, it can be presented as (formatted into) "June 1st 2017, 12:46:01.169", "2017/06/01" or "1 Jun 2017" while still just holding the same value without any formatting information — the formatting lies outside the Date.

根据您的要求,一种选择是将日期存储为Date(或更好的是,现代日期和时间类之一的实例,例如LocalDate),并保留一个格式化程序,以便对其进行格式化每次您需要向用户展示时.如果这行不通,并且您需要以特定格式存储日期,然后将其存储为String.

Depending on your requirements, one option is you store your date as a Date (or better, an instance of one of the modern date and time classes like LocalDate) and keep a formatter around so you can format it every time you need to show it to the user. If this won’t work and you need to store the date in a specific format, then store it as a String.

现在,我一直在评论中使用较新的Java日期和时间类,因此不向您展示它们可以工作可能是不公平的.该问题尝试将格式设置为yyyy-MM-dd,我们可以使用以下代码来实现.

Now I have been ranting about using the newer Java date and time classes in the comments, so it might be unfair not to show you that they work. The question tries to format as yyyy-MM-dd, which we may do with the following piece of code.

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu/MM/dd");
    for (LocalDate date : localDates) {
        String formatterDate = date.format(dateFormatter);
        System.out.println(formatterDate);
    }

一次运行,我就得到了

2017/05/23
2017/06/01

如果列表中的对象具有除LocalDate以外的其他类型,则大多数其他较新的日期和时间类型可以使用相同的DateTimeFormatter以完全相同的方式进行格式化. Instant在这方面有点特殊,因为它不包含日期,但是您可以例如通过myInstant.atZone(ZoneId.of("Europe/Oslo")).format(dateFormatter)获取该时刻在奥斯陆所在时区的日期.

Should your objects in the list have other types than LocalDate, most other newer date and time types can be formatted in exactly the same way using the same DateTimeFormatter. Instant is a little special in this respect because it doesn’t contain a date, but you may do for example myInstant.atZone(ZoneId.of("Europe/Oslo")).format(dateFormatter) to obtain the date it was/will be in Oslo’s time zone at that instant.

现代类在Java 8中引入,并在Java 9中进行了增强.在 ThreeTen Backport ,带有适用于Android的特殊版本, ThreeTenABP .因此,我真的没有理由不解释为什么您不希望在自己的代码中使用它们.

The modern classes were introduced in Java 8 and are enhanced a bit in Java 9. They have been backported to Java 6 and 7 in the ThreeTen Backport with a special edition for Android, ThreeTenABP. So I really see no reason why you should not prefer to use them in your own code.

这篇关于Java日期更改格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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