This requirement can be achieved using java.text.DateFormat
like so:
DateFormat format = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT,
Locale.getDefault());
String pattern = ((SimpleDateFormat)format).toPattern();
System.out.println(pattern);
Or using java.time.format.DateTimeFormatterBuilder
like so:
String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.SHORT, FormatStyle.SHORT,
Chronology.ofLocale(Locale.getDefault()),
Locale.getDefault());
System.out.println(pattern);
In both cases the pattern needs to be converted for usage in Excel
using DateFormatConverter like so:
pattern = DateFormatConverter.convert(Locale.getDefault(), pattern);
System.out.println(pattern);
Complete example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.DateFormatConverter;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.time.chrono.Chronology;
class CreateExcelCellDateFormat {
public static void main(String[] args) throws Exception {
//Locale.setDefault(new Locale("en", "US"));
System.out.println(Locale.getDefault());
String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.SHORT, FormatStyle.SHORT,
Chronology.ofLocale(Locale.getDefault()), Locale.getDefault());
System.out.println(pattern);
/*
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
String pattern = ((SimpleDateFormat)format).toPattern();
System.out.println(pattern);
*/
pattern = DateFormatConverter.convert(Locale.getDefault(), pattern);
System.out.println(pattern);
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
CellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat(pattern));
Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellStyle(style);
cell.setCellValue(new Date());
sheet.setColumnWidth(0, 25 * 256);
workbook.write(fileout);
}
}
}
But if the really requirement is to create a Excel
workbook which shows date-time values dependent on the user locale the Excel application runs in, then this all is not necessary. Then do using the BuiltinFormats 0xe, "m/d/yy" for short date or 0x16, "m/d/yy h:mm" for short date-time.
Example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class CreateExcelCellDateFormatUserLocale {
public static void main(String[] args) throws Exception {
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
CellStyle style = workbook.createCellStyle();
//style.setDataFormat((short)14); //0xe, "m/d/yy"
style.setDataFormat((short)22); //0x16, "m/d/yy h:mm"
Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellStyle(style);
cell.setCellValue(new java.util.Date());
sheet.setColumnWidth(0, 25 * 256);
workbook.write(fileout);
}
}
}
That code produces a Excel
file which shows the date-time exactly as Excel
does dependent on the locale the Excel
application runs in. For Germany it shows dd.MM.yyyy hh:mm. For US it shows MM/dd/yy h:m AM/PM. For UK it shows dd/MM/yyyy hh:mm.