Java date format - including additional characters

雨燕双飞 提交于 2019-11-28 19:03:12
Thilo

Sure, with the SimpleDateFormat you can include literal strings:

Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

 "hh 'o''clock' a, zzzz"    12 o'clock PM, Pacific Daylight Time

Just for completeness, Java 8's DateTimeFormatter also supports this:

DateTimeFormatter.ofPattern("yyyy 'year'");
R Samuel Klatchko

You can use String.format as documented in java.util.Formatter:

Calendar c = ...;
String s = String.format("%tY year", c);
// -> s == "2010 year" or whatever the year actually is

java.text.SimpleDateFormat

SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); 
String formattedDate = formatter.format(date);

You'll get more info here link text

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