Java 日期格式 - 包括附加字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2138085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java date format - including additional characters
提问by Sejanus
Is there an equivalent to php date() style formatting in Java? I mean, in php I can backslash-escape characters to have them treated literally. I.e. yyyy \y\e\a\rwould become 2010 year. I did not find anything similar in Java, all examples deal only with built-in date formats.
Java 中是否有与 php date() 样式格式等效的格式?我的意思是,在 php 中,我可以使用反斜杠转义字符来按字面意思处理它们。即yyyy \y\e\a\r将成为2010 年。我在 Java 中没有找到类似的东西,所有的例子都只处理内置的日期格式。
In particular, I deal with JCalendardate pickers and their dateFormatStringproperty.
特别是,我处理JCalendar日期选择器及其dateFormatString属性。
I need it because in my locale it is required to write all sorts of additional stuff in date format, like d. (for day) after days part, m. (for years) after years part and so on. At the worst case I could use string replace or regexp but maybe there's a simpler way? Thanks in advance!
我需要它,因为在我的语言环境中,需要以日期格式编写各种其他内容,例如 d。(对于一天)后天部分,米。(数年)之后的部分等等。在最坏的情况下,我可以使用字符串替换或正则表达式,但也许有更简单的方法?提前致谢!
采纳答案by Thilo
Sure, with the SimpleDateFormatyou can include literal strings:
当然,使用SimpleDateFormat您可以包含文字字符串:
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.
在日期和时间模式字符串中,从“A”到“Z”以及从“a”到“z”的未加引号的字母被解释为表示日期或时间字符串组成部分的模式字母。可以使用单引号 (') 引用文本以避免解释。“''”代表单引号。不解释所有其他字符;它们只是在格式化过程中复制到输出字符串中,或者在解析过程中与输入字符串匹配。
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
回答by R Samuel Klatchko
You can use String.format as documented in java.util.Formatter:
您可以使用java.util.Formatter 中记录的 String.format :
Calendar c = ...;
String s = String.format("%tY year", c);
// -> s == "2010 year" or whatever the year actually is
回答by Javi
回答by Mark Jeronimus
Just for completeness, Java 8's DateTimeFormatter
also supports this:
为了完整起见,Java 8DateTimeFormatter
也支持这个:
DateTimeFormatter.ofPattern("yyyy 'year'");
回答by Ole V.V.
java.time
时间
Mark Jeronimus said italready. I am fleshing it out a bit more. Just put the text to be printed literally inside single quotes.
马克·杰罗尼姆斯已经说过了。我正在充实它一点。只需将要打印的文本逐字放在单引号内。
DateTimeFormatter yearFormatter = DateTimeFormatter.ofPattern("yyyy 'year'");
System.out.println(LocalDate.of(2010, Month.FEBRUARY, 3).format(yearFormatter));
System.out.println(Year.of(2010).format(yearFormatter));
System.out.println(ZonedDateTime.now(ZoneId.of("Europe/Vilnius")).format(yearFormatter));
Output when running just now:
刚才运行时的输出:
2010 year 2010 year 2019 year
2010 year 2010 year 2019 year
If you are using a DateTimeFormatterBuilder
and its appendPattern
method, use single quotes in the same way. Or use its appendLiteral
method instead and no single quotes.
如果您使用 aDateTimeFormatterBuilder
及其appendPattern
方法,请以相同的方式使用单引号。或者使用它的appendLiteral
方法而不是单引号。
How do we put a single quote in the format, then? Two single quotes produce one. It doesn't matter if the double single quote is inside single quotes or not:
那么我们如何在格式中放置单引号呢?两个单引号产生一个。双单引号是否在单引号内并不重要:
DateTimeFormatter formatterWithSingleQuote = DateTimeFormatter.ofPattern("H mm'' ss\"");
System.out.println(LocalTime.now(ZoneId.of("Europe/London")).format(formatterWithSingleQuote));
10 28' 34"
10 28' 34"
DateTimeFormatter formatterWithSingleQuoteInsideSingleQuotes
= DateTimeFormatter.ofPattern("hh 'o''clock' a, zzzz", Locale.ENGLISH);
System.out.println(ZonedDateTime.now(ZoneId.of("America/Los_Angeles"))
.format(formatterWithSingleQuoteInsideSingleQuotes));
02 o'clock AM, Pacific Daylight Time
太平洋夏令时间凌晨 2 点
All of the formatters above can be used for parsing too. For example:
上面的所有格式化程序也可用于解析。例如:
LocalTime time = LocalTime.parse("16 43' 56\"", formatterWithSingleQuote);
System.out.println(time);
16:43:56
16:43:56
The SimpleDateFormat
class used when this question was asked nearly 10 years ago is notoriously troublesome and long outdated. I recommend that instead you use java.time, the modern Java date and time API. Which is why I demonstrate just that.
SimpleDateFormat
近 10 年前问这个问题时使用的类是出了名的麻烦且已经过时。我建议您改用 java.time,现代 Java 日期和时间 API。这就是为什么我要证明这一点。
Links
链接
- Oracle tutorial: Date Timeexplaining how to use java.time.
- Documentation of
DateTimeFormatter
- Oracle 教程:解释如何使用 java.time 的日期时间。
- 文件
DateTimeFormatter