开发者

how can I format a date in java with "ff"?

I'm trying to print a开发者_开发问答 date with the following format:

"HHmmssff" 

I'm using SimpleDateFormatter. It fails because it can't recognize "ff". Is there another formatter that can? Or any other way to do it?


If you are looking for printing second in fractions (milliseconds), this example will be helpful.

public class DateFormatter {
    public static void main(String[] args) {
        Date date = Calendar.getInstance().getTime();
        DateFormat formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
        String today = formatter.format(date);
        System.out.println("Today : " + today);
    }
}

Output: Today : Tuesday, 24 May 2011, 07:23:30.627 PM

You see SSS in the formatter returns fractions of seconds for you.


f is not a valid value for SimpleDateFormat.

F means Day of week in month, that might be what you are looking for.


S gives you millisecond:

DateFormat formatter = new SimpleDateFormat ("hhmmssS");

with substring, you may cut off the last 2 digits:

String d = formatter.format (new Date ());
System.out.println (d.substring (0, 7));


Maybe you're looking for FF instead of ff, as used here?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜