Mixed Western calendar output in Java

Preamble

I feel that it seems quite likely that the Western calendar and the Japanese calendar will be displayed side by side. "Yyyy (ggge)" year "mm" month "dd" day "" in Excel is output in Java.

java8.java


JapaneseDate japdate = JapaneseDate.now();
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy");
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("'('Gyy')Year'MM'Month'dd'Day'");
System.out.println(LocalDate.from(japdate).format(dtf1) + japdate.format(dtf2));

And I think it's probably right. 2017/01/17 added ↓ It wasn't there (sweat)

java8.java


JapaneseDate japdate = JapaneseDate.now();
DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("uuuu'('Gyy')Year'MM'Month'dd'Day'");
System.out.println(japdate.format(dtf3));

There is a pattern letter "u", and if you use that, you can go in one shot. It has evolved properly. 2017/01/17 added ↑

java6.java


Locale loc = new Locale("ja", "JP", "JP");
Calendar cal = Calendar.getInstance(loc);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("'('GGGGyyyy')Year'MM'Month'dd'Day'", loc);
System.out.println(sdf1.format(cal.getTime()) + sdf2.format(cal.getTime()));

So, it should be right for the time being.

Supplement

By passing a Japanese locale to getInstance of Calendar, you can return the Japanese Imperial Calendar (java.util.Japanese ImperialCalendar) instead of the Gregorian Calendar (java.util.GregorianCalendar), so it corresponds to the so-called Japanese era. The story that will be possible. I think there was a story that it shouldn't be used easily because the interpretation of the boundary of the era is delicate, but basically I understand that there is no problem if the latest date is given.

Serpentine?

However, it's kind of unpleasant to pass a string to the constructor of this locale, so I simply pass Japan explicitly.

Locale loc = Locale.JAPAN;
Calendar cal = Calendar.getInstance(loc);

So, I thought it was good, but this does not return the Japanese Empire Calendar. The reason is that Locale.JAPAN.getVariant () does not return "JP"?

Is this something like that? Something strange?

Recommended Posts

Mixed Western calendar output in Java
Log output to file in Java
Output Notes document as XML document in Java
Output Date in Java in ISO 8601 extended format
[Java] output, variables
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Try scraping about 30 lines in Java (CSV output)
I tried to output multiplication table in Java
[Java] Something is displayed as "-0.0" in the output
Compare PDF output in Java for snapshot testing
About Java log output
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
How to input / output IBM mainframe files in Java?
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier)
Express failure in Java
Output triangle in Ruby
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Gzip-compress byte array in Java and output to file
Use PreparedStatement in Java
Output in multiples of 3
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Was done in the base year of the Java calendar week
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel