From 12c9b2391742e45e7878eb0be4474c1b8f56b0ad Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sat, 16 Sep 2017 08:29:28 +0000 Subject: Fix some compiler warnings git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808523 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/util/TestDateFormatConverter.java | 50 ++++++++++------------ 1 file changed, 22 insertions(+), 28 deletions(-) (limited to 'src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java') diff --git a/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java b/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java index 2b6e6e3127..70a90492a8 100644 --- a/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java +++ b/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java @@ -41,11 +41,10 @@ import org.apache.poi.util.TempFile; public final class TestDateFormatConverter extends TestCase { private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception { - Workbook workbook = new HSSFWorkbook(); - try { + try (Workbook workbook = new HSSFWorkbook()) { String sheetName; - if( dates ) { - if( times ) { + if (dates) { + if (times) { sheetName = "DateTimes"; } else { sheetName = "Dates"; @@ -62,18 +61,18 @@ public final class TestDateFormatConverter extends TestCase { header.createCell(4).setCellValue("Equals"); header.createCell(5).setCellValue("Java pattern"); header.createCell(6).setCellValue("Excel pattern"); - + int rowNum = 1; - for( Locale locale : DateFormat.getAvailableLocales() ) { + for (Locale locale : DateFormat.getAvailableLocales()) { try { Row row = sheet.createRow(rowNum++); - + row.createCell(0).setCellValue(locale.toString()); row.createCell(1).setCellValue(locale.getDisplayName(Locale.ROOT)); - + DateFormat dateFormat; - if( dates ) { - if( times ) { + if (dates) { + if (times) { dateFormat = DateFormat.getDateTimeInstance(style, style, locale); } else { dateFormat = DateFormat.getDateInstance(style, locale); @@ -81,42 +80,37 @@ public final class TestDateFormatConverter extends TestCase { } else { dateFormat = DateFormat.getTimeInstance(style, locale); } - + Cell cell = row.createCell(2); - + cell.setCellValue(date); CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle(); - - String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern(); + + String javaDateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern(); String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern); - + DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat(); cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern)); row.createCell(3).setCellValue(dateFormat.format(date)); - + cell.setCellStyle(cellStyle); - + // the formula returns TRUE is the formatted date in column C equals to the string in column D - row.createCell(4).setCellFormula("TEXT(C"+rowNum+",G"+rowNum+")=D" + rowNum); + row.createCell(4).setCellFormula("TEXT(C" + rowNum + ",G" + rowNum + ")=D" + rowNum); row.createCell(5).setCellValue(javaDateFormatPattern); row.createCell(6).setCellValue(excelFormatPattern); } catch (Exception e) { - throw new RuntimeException("Failed for locale: " + locale + ", having locales: " + + throw new RuntimeException("Failed for locale: " + locale + ", having locales: " + Arrays.toString(DateFormat.getAvailableLocales()), e); } } - + File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx"); - FileOutputStream outputStream = new FileOutputStream(outputFile); - try { + try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { workbook.write(outputStream); - } finally { - outputStream.close(); } - - System.out.println("Open " + outputFile.getAbsolutePath()+" in Excel"); - } finally { - workbook.close(); + + System.out.println("Open " + outputFile.getAbsolutePath() + " in Excel"); } } -- cgit v1.2.3