It was decided to implement a specification that displays both the Western calendar and the Japanese calendar, such as "Western calendar (Heisei XX year)" in SelectBox.
Convert Japanese calendar to Western calendar, Western calendar to Japanese calendar with Java https://qiita.com/u-chida/items/31b3415a52f0e1818ab2
You can get the Japanese calendar with this
To quote
//Get Calendar instance by specifying locale
Locale locale = new Locale("ja", "JP", "JP");
Calendar calendar = Calendar.getInstance(locale);
//calendar.getTime()Get the current date and time with and format it into the Japanese calendar
DateFormat japaseseFormat = new SimpleDateFormat("GGGGy year M month d day", locale);
String dateStr = japaseseFormat.format(calendar.getTime());
System.out.println("Japanese Calendar:" + dateStr);
//Parses the current date and time formatted in the Japanese calendar to the Christian era
//SetLenient to check the validity of the date()Pass false to
calendar.setLenient(false);
Date date = japaseseFormat.parse(dateStr);
System.out.println("Year:" + date);
So, I decided to write it together with the Japanese calendar from 1990 to 2000.
for (int seireki = 1990 ; seireki <= 2000; seireki++ ){
//Get Calendar instance by specifying locale
Locale locale = new Locale("ja", "JP", "JP");
Calendar calendar = Calendar.getInstance(locale);
//Clear value
calendar.clear();
//Set year
calendar.set(Calendar.YEAR, seireki);
// calendar.getTime()Get the current date and time with and format it into the Japanese calendar
DateFormat japaseseFormat = new SimpleDateFormat("(GGGGy year)", locale);
String dateStr = japaseseFormat.format(calendar.getTime());
System.out.println(seireki + dateStr);
}
result
1990(1990)
1991(1991)
1992(1992)
1993(1993)
1994(1994)
1995(1995)
1996(1996)
1997(1997)
1998(1998)
1999(1999)
2000(2000)
It's not cool.
When I looked at the contents with calendar.getTimeInMillis ()
, it contained a disastrous value such as "633798000000".
Since the locale of the calendar is the Japanese calendar, if you enter 1990 in the year, it will be 1990. It's a matter of course.
Unfortunately, Java 6 doesn't have a constructor that specifies the locale and year at the same time.
Improvement plan
for (int seireki = 1990 ; seireki <= 2000; seireki++ ){
//Get Calendar instance by specifying locale
Locale locale = new Locale("ja", "JP", "JP");
//Make a calendar for 1990
Calendar greCal = new GregorianCalendar(seireki, 1, 1);
Calendar calendar = Calendar.getInstance(locale);
//Clear value
calendar.clear();
//Set the year from the calendar
calendar.setTimeInMillis(greCal.getTimeInMillis());
// calendar.getTime()Get the current date and time with and format it into the Japanese calendar
DateFormat japaseseFormat = new SimpleDateFormat("(GGGGy year)", locale);
String dateStr = japaseseFormat.format(calendar.getTime());
System.out.println(seireki + dateStr);
}
1990(1990)
1991(1991)
1992(1992)
1993(1993)
1994(1994)
1995(1995)
1996(1996)
1997(1997)
1998(1998)
1999(1999)
2000(2000)
Good vibes. The boundary between Heisei and Showa is perfect (born in 1989 is treated uniformly in 1991)
1987(1987)
1988(1988)
1989(Heisei 1)
1990(1990)
Should one year really be the first year ...? I was able to do it well for the time being, but I'm wondering if this is really all right.
2017/11/17 postscript The above was done in Java 6, but fixed in Java 8. In response to the comment, the display was changed to "1989 is Showa and Heisei".
import java.time.chrono.JapaneseDate;
import java.time.format.DateTimeFormatter;
public class Qiita21ee9377032b650c2de6 {
public static void main(String[] args) {
QiitaExecute qiitaExe = new QiitaExecute();
qiitaExe.execute();
}
}
class QiitaExecute {
private final DateTimeFormatter japaseseFormat = DateTimeFormatter.ofPattern("GGGGy year");
/**
*Run
*/
public void execute() {
for (int annoDomini = 1987; annoDomini <= 1990; annoDomini++) {
String eraString = getEraString(annoDomini);
System.out.println(annoDomini + "(" + eraString + ")");
}
}
/**
*Return of Japanese calendar
*Pass the Christian era, and if there is a change in the era between New Year's Day and the end of the year, return both eras by concatenating them with "・"
*If there is no change in the era, return the era on New Year's Day
* @param annoDomini
* @return
*/
private String getEraString(int annoDomini) {
//New Year's Day
//(The month of Japanese Date is displayed from 1 to 12)
JapaneseDate japaneseNewYearDate = JapaneseDate.of(annoDomini, 1, 1);
//Year-end
JapaneseDate japaneseNewYearEveDate = JapaneseDate.of(annoDomini, 12, 31);
//If the era is the same, the era of New Year's Day is returned, otherwise New Year's Day and the end of the year are connected with "・" and returned
if (japaneseNewYearDate.getEra().getValue() == japaneseNewYearEveDate.getEra().getValue()) {
return japaseseFormat.format(japaneseNewYearDate);
} else {
return japaseseFormat.format(japaneseNewYearDate) + "・" + japaseseFormat.format(japaneseNewYearEveDate);
}
}
}
result
1987(1987)
1988(1988)
1989(1988/Heisei 1)
1990(1990)
It is a matter of concern whether to display in 1989 (Showa 64, 1989). I'm Japanese, so I want to be in the first year.
1987(1987)
1988(1988)
1989(Showa 64)
1989(Heisei 1)
1990(1990)
May be the correct display, but it can be confusing to be born in 1989 or 1990. (I think it was born in 1990 under 1989. Considering the possibility of mistakes such as those who select the select box with the keyboard)