SimpleDateFormat does not process DD properly

☆樱花仙子☆ 提交于 2019-12-13 13:46:10

问题


I am trying to get a format like this:

2013-06-15-17-45

I do the following in my code:

  Date d = new Date();
  SimpleDateFormat ft = new SimpleDateFormat ("YYYY_MM_DD_HH_mm");          

  String fileName = "D:\\"+ft.format(d)+".csv";

But I don't get the DD right. It creates a file with a name like this:

D:\\2013_12_340_13_53.csv

回答1:


Capital "D" is day of year; lowercase "d" is day of month (SimpleDateFormat javadocs). Try:

SimpleDateFormat ft = new SimpleDateFormat ("yyyy_MM_dd_HH_mm");

Also, capital "Y" is "week year"; usually lowercase "y" ("year") is used.




回答2:


The answer by rgettman is correct.

Joda-Time

Here's the same kind of code, but using Joda-Time 2.3 with Java 7.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTime now = new DateTime();

DateTimeFormatter formatter = DateTimeFormat.forPattern( "yyyy_MM_dd_HH_mm" );

System.out.println( "now ISO 8601 format: " + now.toString() );
System.out.println( "now in special format: " + formatter.print( now ) );

When run…

now ISO 8601 format: 2013-12-06T20:02:52.070-08:00
now in special format: 2013_12_06_20_02

Suggestion: ISO 8601 Style

I do the same kind of date-time in labeling files and folders. I suggest using a format closer to the standard ISO 8601 format.

Joda-Time has a built-in format close to what we need, dateHourMinute() on ISODateTimeFormat class.

For use with file systems, we should avoid using slash (Unix), backslash (MS Windows), and colon (Mac) characters. By starting with ISO format, we have no slash or backslash characters. That leaves only the colons from the time portion, to be removed with a call to replaceAll method of String class in Java.

You may wish to replace the T as well.

// import org.joda.time.*;
// import org.joda.time.format.*;

DateTime now = new DateTime();
System.out.println( "now: " + now );

DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinute();
String formatted = formatter.print( now ).replaceAll( ":", "." ).replaceAll( "T", "_" );
System.out.println( "formatted: " + formatted );

When run…

now: 2013-12-06T21:10:07.382-08:00
formatted: 2013-12-06_21.10

UTC

Furthermore, for server-side or other serious work consider converting to UTC rather than your local time to avoid ambiguity.

    DateTime now = new DateTime();
    System.out.println( "now: " + now );

    DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinute();
    String formatted = formatter.print( now.toDateTime( DateTimeZone.UTC ) ).replaceAll( ":", "." ).replaceAll( "T", "_" ) + "Z";
    System.out.println( "formatted: " + formatted );

When run…

now: 2013-12-06T21:21:00.128-08:00
formatted: 2013-12-07_05.21Z


来源:https://stackoverflow.com/questions/20435638/simpledateformat-does-not-process-dd-properly

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