public final class DateFormatConverter
extends java.lang.Object
// returns "[$-0411]dd MMMM, yyyy;@" where the [$-0411] prefix tells Excel to use the Japanese locale
String excelFormatPattern = DateFormatConverter.convert(Locale.JAPANESE, "dd MMMM, yyyy");
CellStyle cellStyle = workbook.createCellStyle();
DataFormat poiFormat = workbook.createDataFormat();
cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle); // formats date as '2012骞?3鏈?17鏃?'
TODO Generalise this for all Excel format strings
(将 java DateFormat 模式转换为 Excel 自定义数字格式。例如,要使用“dd MMMM, yyyy”模式和日语区域设置在 excel 中格式化日期,请使用以下代码: // 返回“[$-0411]dd MMMM, yyyy;@” 其中 [$-0411]前缀告诉 Excel 使用日语区域设置 String excelFormatPattern = DateFormatConverter.convert(Locale.JAPANESE, "dd MMMM, yyyy"); CellStyle cellStyle = workbook.createCellStyle();数据格式 poiFormat = workbook.createDataFormat(); cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern)); cell.setCellValue(新日期()); cell.setCellStyle(cellStyle); // 将日期格式化为 '2012 年 3 月 17 日' TODO 将其推广到所有 Excel 格式字符串)
Modifier and Type | Class and Description |
---|---|
static class |
DateFormatConverter.DateFormatTokenizer |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
convert(java.util.Locale locale, java.text.DateFormat df) |
static java.lang.String |
convert(java.util.Locale locale, java.lang.String format) |
static java.lang.String |
getJavaDatePattern(int style, java.util.Locale locale) |
static java.lang.String |
getJavaDateTimePattern(int style, java.util.Locale locale) |
static java.lang.String |
getJavaTimePattern(int style, java.util.Locale locale) |
static java.lang.String |
getPrefixForLocale(java.util.Locale locale) |
public static java.lang.String getPrefixForLocale(java.util.Locale locale)
public static java.lang.String convert(java.util.Locale locale, java.text.DateFormat df)
public static java.lang.String convert(java.util.Locale locale, java.lang.String format)
public static java.lang.String getJavaDatePattern(int style, java.util.Locale locale)
public static java.lang.String getJavaTimePattern(int style, java.util.Locale locale)
public static java.lang.String getJavaDateTimePattern(int style, java.util.Locale locale)
Copyright 2021 The Apache Software Foundation or its licensors, as applicable.