Java 日期格式-包括附加字符

在 Java 中是否有与 php date ()样式格式等价的东西?我的意思是,在 php 中,我可以反斜杠-转义字符按字面意思对待它们。也就是说 是呀,是呀会变成 二零一零年。我在 Java 中没有发现任何类似的东西,所有示例都只处理内置的日期格式。

特别是,我处理 日历日期选择器和他们的 DateFormatString属性。

我之所以需要它,是因为在我的语言环境中,它需要以日期格式编写各种额外的东西,比如 d (代表天后的部分) ,m (代表年后的部分) ,年后的部分等等。在最坏的情况下,我可以使用字符串替换或 regexp,但也许有一个更简单的方法?先谢谢你!

59966 次浏览

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

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'");

java.time

Mark Jeronimus said it already. 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

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.

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"

    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

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

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.

Links