Java Date changing format [duplicate]

无人久伴 提交于 2019-11-27 09:05:44

问题


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));                      
}

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.


回答1:


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.

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 8 (7, 6) date and time API

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);
    }

In one run I got

2017/05/23
2017/06/01

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.

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.




回答2:


Try this one.

String formattedDate = null;
    SimpleDateFormat sdf = new SimpleDateFormat(format you want);
    formattedDate = sdf.format( the date you want to format );
   return formattedDate;



回答3:


some not best solution, but it works: this method will convert Date object to String of any pattern you need

public static void format(Date date){
   String pattern = "MMM d yyyy";
   LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
   DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
   String result = formatter.format(localDate);
   // new Date() -> Jun 1 2017
 }



回答4:


SimpleDateFormat is useful while converting Date to String or vice-versa. java.util.Date format will always remain same. If you want to display it in standalone application then use date.getxxx() methods and choose your design.



来源:https://stackoverflow.com/questions/44306533/java-date-changing-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!