Java – get Japanese year name
•
Java
I have a program to get Japanese character year names:
Locale locale = new Locale("ja","JP","JP");
Calendar calendar = Calendar.getInstance(locale);
calendar.set(Calendar.YEAR,1);
Date date = calendar.getTime();
DateFormat format1
= new SimpleDateFormat("GGGGy年",locale);
System.out.println("y: " + format1.format(date));
DateFormat format2
= new SimpleDateFormat("GGGGyy年",locale);
System.out.println("yy: " + format2.format(date));
DateFormat format3
= new SimpleDateFormat("GGGGyyy年",locale);
System.out.println("yyy: " + format3.format(date));
DateFormat format4
= new SimpleDateFormat("GGGGyyyy年",locale);
System.out.println("yyyy: " + format4.format(date));
Printing in the eclipse console is like this, which is what I want
y: 平成1年 yy: 平成01年 yyy: 平成001年 yyyy: 平成元年
However, when I make a jar and CMD runs it, the output changes to:
y: Heisei1年 yy: Heisei01年 yyy: Heisei001年 yyyy: Heisei1年
I don't know why. I want to get Chinese characters Please help me thank you.
Solution
I run the following test cases:
Locale WAREKI_LOCALE = new Locale("ja","JP");
TimeZone tz = TimeZone.getTimeZone("JST");
Calendar jcal = Calendar.getInstance(tz,WAREKI_LOCALE);
SimpleDateFormat sdf = new SimpleDateFormat("GGGG y-MM-dd zzz",WAREKI_LOCALE);
sdf.setCalendar(jcal);
SimpleDateFormat sdf2 = new SimpleDateFormat("GGGG y-MM-dd zzz",Locale.UK);
System.out.println("got = " + sdf.format(1549556043201L) + " (" + sdf2.format(1549556043201L) + ")");
System.out.println("got = " + sdf.format(-1357544756799L) + " (" + sdf2.format(-1357544756799L) + ")");
And use JDK 8 and JDK 11 to obtain the following outputs:
got = 平成 31-02-08 JST (AD 2019-02-07 CET) got = 昭和 1-12-26 JST (AD 1926-12-25 CET)
You can also see: https://github.com/twosigma/OpenJDK/blob/master/test/jdk/java/util/Calendar/SupplementalJapaneseEraTest.java
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
