我有一个西式日期字符串,我想用日文格式。
import re
a=['','apple','ball','cat']
regex = re.compile(input())
# filter only the strings in 'a' which match the given pattern
matches = filter(lambda x: regex.match(x), a)
for i in matches:
print(i)
我想要的是日文格式。请帮帮我!
String date = "2015-06-01";
答案 0 :(得分:1)
您可以使用我的库Time4A,该库也可用于较低的Android API级别,然后使用其JapaneseCalendar:
String input = "2015-06-01";
PlainDate gregorian = Iso8601Format.EXTENDED_DATE.parse(input);
ChronoFormatter<JapaneseCalendar> f = // immutable, so you can make it static
ChronoFormatter.ofStyle(DisplayMode.MEDIUM, Locale.JAPANESE, JapaneseCalendar.axis());
String output = f.print(gregorian.transform(JapaneseCalendar.axis()));
System.out.println(output); // 平成27年6月1日
我还对java.time
软件包进行了实验,该软件包自API级别26开始可用,但无法快速找到一种产生所需格式的方法:
DateTimeFormatter dtf =
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(Locale.JAPAN)
.withChronology(JapaneseChronology.INSTANCE);
JapaneseDate japaneseDate = JapaneseDate.from(LocalDate.parse(input));
System.out.println(dtf.format(japaneseDate)); // H27.06.01
尽管我指定了日本语言环境,但数字是正确的,但它不使用日语符号和字母。也许使用构建器和手工制作的模式的变通办法会有所帮助。请注意,我的实验是在Java-8环境中执行的。也许Android或更新的Java版本与众不同?!
答案 1 :(得分:1)
作为Meno Hochschild答案的次要补充,如果您想为此使用java.time,则可以这样进行:
DateTimeFormatter japaneseEraDtf = DateTimeFormatter.ofPattern("GGGGy年M月d日")
.withChronology(JapaneseChronology.INSTANCE)
.withLocale(Locale.JAPAN);
String date = "2015-06-01";
LocalDate gregorianDate = LocalDate.parse(date);
JapaneseDate japaneseDate = JapaneseDate.from(gregorianDate);
System.out.println(japaneseDate.format(japaneseEraDtf));
输出为:
平成27年6月1日
我想这就是你要的。
如果我正确理解,java.time不会像Meno Hochschild的Time4A库那样以日语通常期望的方式来格式化一个时代的第一年,因此在所有条件都相同的情况下,您会喜欢他的答案。
我从this answer by buræquete那里盗走了格式化程序。