Skip to content

Instantly share code, notes, and snippets.

@asdf913
Created April 17, 2024 22:55
Show Gist options
  • Save asdf913/69d56762919454c7b78f4c28c83e48ac to your computer and use it in GitHub Desktop.
Save asdf913/69d56762919454c7b78f4c28c83e48ac to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.time.chrono.JapaneseEra;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.BIPUSH;
import org.apache.bcel.generic.ICONST;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.MethodGen;
import org.apache.bcel.generic.PUTSTATIC;
import org.apache.bcel.generic.SIPUSH;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import edu.stanford.nlp.util.IntTriple;
public class JapaneseDateWesternDateSpreadSheet {
public static void main(final String[] args) throws IOException, IllegalAccessException {
//
// https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/time/chrono/JapaneseEra.java
//
// {Meiji=1868-01-01, Taisho=1912-07-30, Showa=1926-12-25, Heisei=1989-01-08,
// Reiwa=2019-05-01}
//
// Year Month Day 年号
// 1868 1 1 明治 元 明治元年
//
final List<IntTriple> japaneseEraSinceDates = getJapaneseEraSinceDates();
//
final IntTriple firstJapaneseEraSinceDate = Collections.min(japaneseEraSinceDates,
(a, b) -> StringUtils.compare(a.toString(), b.toString()));
//
Calendar calendar = null;
//
if (firstJapaneseEraSinceDate != null && (calendar = Calendar.getInstance()) != null) {
//
calendar.set(firstJapaneseEraSinceDate.getSource(), firstJapaneseEraSinceDate.getMiddle() - 1,
firstJapaneseEraSinceDate.getTarget(), 0, 0, 0);
//
calendar.set(Calendar.MILLISECOND, 0);
//
} // if
//
final Calendar now = Calendar.getInstance();
//
List<Date> dates = null;
//
while (calendar != null && ObjectUtils.compare(calendar, now) <= 0) {
//
if (dates == null) {
//
dates = new ArrayList<>();
//
} // if
//
dates.add(calendar.getTime());
//
calendar.add(Calendar.DATE, 1);
//
} // while
//
final File file = new File("JapaneseDateWesternDateSpreadSheet.xlsx");
//
try (final Workbook wb = new XSSFWorkbook(); final OutputStream os = new FileOutputStream(file)) {
//
Date date = null;
//
Sheet sheet = null;
//
int physicalNumberOfRows;
//
Row row;
//
Cell cell;
//
for (int i = 0; dates != null && i < dates.size(); i++) {
//
if ((date = dates.get(i)) == null) {
//
continue;
//
} // if
//
if (sheet == null) {
//
sheet = wb.createSheet();
//
} // if
//
if (sheet == null) {
//
continue;
//
} // if
//
if ((physicalNumberOfRows = sheet.getPhysicalNumberOfRows()) == 0
&& (row = sheet.createRow(physicalNumberOfRows)) != null) {
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
cell.setCellValue("Year");
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
cell.setCellValue("Month");
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
cell.setCellValue("Day");
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
cell.setCellValue("年号");
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
cell.setBlank();
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
cell.setBlank();
//
} // if
//
} // if
//
if ((row = sheet.createRow(sheet.getPhysicalNumberOfRows())) == null) {
//
continue;
//
} // if
//
if ((calendar = Calendar.getInstance()) != null) {
//
calendar.setTime(date);
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
if (calendar != null) {
//
cell.setCellValue(calendar.get(Calendar.YEAR));
//
} else {
//
cell.setBlank();
//
} // if
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
if (calendar != null) {
//
cell.setCellValue(calendar.get(Calendar.MONTH) + 1);
//
} else {
//
cell.setBlank();
//
} // if
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
if (calendar != null) {
//
cell.setCellValue(calendar.get(Calendar.DATE));
//
} else {
//
cell.setBlank();
//
} // if
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
if (calendar != null) {
//
cell.setCellValue(
new SimpleDateFormat("G", new Locale("ja", "JP", "JP")).format(calendar.getTime()));
//
} else {
//
cell.setBlank();
//
} // if
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
if (calendar != null) {
//
cell.setCellValue(
new SimpleDateFormat("yyyy", new Locale("ja", "JP", "JP")).format(calendar.getTime()));
//
} else {
//
cell.setBlank();
//
} // if
//
} // if
//
if ((cell = row.createCell(row.getPhysicalNumberOfCells())) != null) {
//
if (calendar != null) {
//
cell.setCellValue(new SimpleDateFormat("Gyyyy年", new Locale("ja", "JP", "JP"))
.format(calendar.getTime()));
//
} else {
//
cell.setBlank();
//
} // if
//
} // if
//
} // for
//
wb.write(os);
//
} // try
//
System.out.println(file.getAbsolutePath());
//
}
private static List<IntTriple> getJapaneseEraSinceDates() {
//
List<IntTriple> list = null;
//
final Class<?> clz = JapaneseEra.class;
//
try (final InputStream is = clz != null
? clz.getResourceAsStream(String.format("/%1$s.class", StringUtils.replace(clz.getName(), ".", "/")))
: null) {
//
final JavaClass jc = new ClassParser(is, null).parse();
//
final List<Method> ms = Arrays.stream(jc.getMethods())
.filter(m -> m != null ? Objects.equals(m.getName(), "<clinit>") : null).toList();
//
if (ms != null && ms.size() > 1) {
//
throw new IllegalStateException();
//
} // if
//
final Method m = ms != null && ms.size() == 1 ? ms.get(0) : null;
//
final Instruction[] ins = m != null ? new MethodGen(m, null, null).getInstructionList().getInstructions()
: null;
//
Instruction in = null;
//
SIPUSH sipush = null;
//
final int length = ins != null ? ins.length : 0;
//
Number year, month, day = null;
//
for (int i = 0; ins != null && i < length; i++) {
//
if ((in = ins[i]) == null) {
//
continue;
//
} // if
//
if ((sipush = cast(SIPUSH.class, in)) != null && length > i + 5) {
//
year = sipush.getValue();
//
month = getNumberValue(ins[i + 1]);
//
day = getNumberValue(ins[i + 2]);
//
if (cast(PUTSTATIC.class, ins[i + 5]) != null) {
//
if (list == null) {
//
list = new ArrayList<>();
//
} // if
//
list.add(year != null && month != null && day != null
? new IntTriple(year.intValue(), month.intValue(), day.intValue())
: null);
//
} // if
//
} // if
//
} // for
//
} catch (final IOException e) {
//
throw new RuntimeException(e);
//
} // try
//
return list;
//
}
private static Number getNumberValue(final Instruction in) {
//
final ICONST iconst = cast(ICONST.class, in);
//
if (iconst != null) {
//
return iconst.getValue();
//
} // if
//
final BIPUSH bipush = cast(BIPUSH.class, in);
//
if (bipush != null) {
//
return bipush.getValue();
//
} // if
//
throw new IllegalStateException();
//
}
private static <T> T cast(final Class<T> clz, final Object instance) {
return clz != null && clz.isInstance(instance) ? clz.cast(instance) : null;
}
}
@asdf913
Copy link
Author

asdf913 commented Apr 17, 2024

Generate a spreadsheet (xlsx) to list out all the Western Date and its corresponding Japanese Date.
The spreadsheet contains all the date from the first day of Meiji era to the date the program executed.

YearMonthDay年号  
186811明治明治元年

image

@asdf913
Copy link
Author

asdf913 commented Apr 17, 2024

Required dependencies

<!-- https://mvnrepository.com/artifact/org.apache.bcel/bcel -->
<dependency>
	<groupId>org.apache.bcel</groupId>
	<artifactId>bcel</artifactId>
	<version>6.8.2</version>
</dependency>
<!--https://mvnrepository.com/artifact/edu.stanford.nlp/stanford-corenlp-->
<dependency>
	<groupId>edu.stanford.nlp</groupId>
	<artifactId>stanford-corenlp</artifactId>
	<version>4.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>5.2.5</version>
</dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment