Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
import org.apache.fesod.sheet.converters.localdatetime.LocalDateTimeDateConverter;
import org.apache.fesod.sheet.converters.localdatetime.LocalDateTimeNumberConverter;
import org.apache.fesod.sheet.converters.localdatetime.LocalDateTimeStringConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeDateConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeNumberConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeStringConverter;
import org.apache.fesod.sheet.converters.longconverter.LongBooleanConverter;
import org.apache.fesod.sheet.converters.longconverter.LongNumberConverter;
import org.apache.fesod.sheet.converters.longconverter.LongStringConverter;
Expand Down Expand Up @@ -117,6 +120,9 @@ private static void initAllConverter() {
putAllConverter(new LocalDateTimeNumberConverter());
putAllConverter(new LocalDateTimeStringConverter());

putAllConverter(new LocalTimeNumberConverter());
putAllConverter(new LocalTimeStringConverter());

putAllConverter(new DoubleBooleanConverter());
putAllConverter(new DoubleNumberConverter());
putAllConverter(new DoubleStringConverter());
Expand Down Expand Up @@ -153,6 +159,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new DateDateConverter());
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new LocalDateDateConverter());
putWriteConverter(new LocalTimeDateConverter());
putWriteConverter(new DoubleNumberConverter());
putWriteConverter(new FloatNumberConverter());
putWriteConverter(new IntegerNumberConverter());
Expand All @@ -173,6 +180,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new DateStringConverter());
putWriteStringConverter(new LocalDateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new LocalTimeStringConverter());
putWriteStringConverter(new DoubleStringConverter());
putWriteStringConverter(new FloatStringConverter());
putWriteStringConverter(new IntegerStringConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converters.localtime;

import java.time.LocalDateTime;
import java.time.LocalTime;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;
import org.apache.fesod.sheet.util.WorkBookUtil;

/**
* LocalTime and date converter
*/
public class LocalTimeDateConverter implements Converter<LocalTime> {
@Override
public Class<?> supportJavaTypeKey() {
return LocalTime.class;
}

@Override
public WriteCellData<?> convertToExcelData(
LocalTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
throws Exception {
LocalDateTime localDateTime = value == null ? null : value.atDate(DateUtils.EPOCH);
WriteCellData<?> cellData = new WriteCellData<>(localDateTime);
String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
format = contentProperty.getDateTimeFormatProperty().getFormat();
}
WorkBookUtil.fillDataFormat(
cellData, format == null || format.isEmpty() ? null : format, DateUtils.DEFAULT_LOCAL_TIME_FORMAT);
return cellData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converters.localtime;

import java.math.BigDecimal;
import java.time.LocalTime;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;
import org.apache.poi.ss.usermodel.DateUtil;

/**
* LocalTime and number converter
*/
public class LocalTimeNumberConverter implements Converter<LocalTime> {

@Override
public Class<?> supportJavaTypeKey() {
return LocalTime.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}

@Override
public LocalTime convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtils.getLocalTime(
cellData.getNumberValue().doubleValue(), globalConfiguration.getUse1904windowing());
} else {
return DateUtils.getLocalTime(
cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing());
}
}

@Override
public WriteCellData<?> convertToExcelData(
LocalTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(BigDecimal.valueOf(
DateUtil.getExcelDate(value.atDate(DateUtils.EPOCH), globalConfiguration.getUse1904windowing())));
} else {
return new WriteCellData<>(BigDecimal.valueOf(DateUtil.getExcelDate(
value.atDate(DateUtils.EPOCH),
contentProperty.getDateTimeFormatProperty().getUse1904windowing())));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converters.localtime;

import java.time.LocalTime;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;

/**
* LocalTime and string converter
*/
public class LocalTimeStringConverter implements Converter<LocalTime> {
@Override
public Class<?> supportJavaTypeKey() {
return LocalTime.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}

@Override
public LocalTime convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtils.parseLocalTime(cellData.getStringValue(), null, globalConfiguration.getLocale());
} else {
return DateUtils.parseLocalTime(
cellData.getStringValue(),
contentProperty.getDateTimeFormatProperty().getFormat(),
globalConfiguration.getLocale());
}
}

@Override
public WriteCellData<?> convertToExcelData(
LocalTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(DateUtils.format(value, null, globalConfiguration.getLocale()));
} else {
return new WriteCellData<>(DateUtils.format(
value, contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -100,12 +101,16 @@ public class DateUtils {
public static final String DATE_FORMAT_17 = "yyyyMMdd HH:mm:ss";
public static final String DATE_FORMAT_19 = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_19_FORWARD_SLASH = "yyyy/MM/dd HH:mm:ss";
public static final String TIME_FORMAT_5 = "HH:mm";
public static final String TIME_FORMAT_8 = "HH:mm:ss";
private static final String MINUS = "-";

public static String defaultDateFormat = DATE_FORMAT_19;

public static String defaultLocalDateFormat = DATE_FORMAT_10;

public static final String DEFAULT_LOCAL_TIME_FORMAT = TIME_FORMAT_8;

public static final int SECONDS_PER_MINUTE = 60;
public static final int MINUTES_PER_HOUR = 60;
public static final int HOURS_PER_DAY = 24;
Expand Down Expand Up @@ -162,6 +167,21 @@ public static LocalDate parseLocalDate(String dateString, String dateFormat, Loc
return LocalDate.parse(dateString, getCacheDateTimeFormat(dateFormat, local));
}

/**
* convert string to time
*
* @param timeString
* @param timeFormat
* @param local
* @return
*/
public static LocalTime parseLocalTime(String timeString, String timeFormat, Locale local) {
if (StringUtils.isEmpty(timeFormat)) {
timeFormat = switchTimeFormat(timeString);
}
return LocalTime.parse(timeString, getCacheDateTimeFormat(timeFormat, local));
}

/**
* convert string to date
*
Expand Down Expand Up @@ -205,6 +225,24 @@ public static String switchDateFormat(String dateString) {
}
}

/**
* switch time-only format
*
* @param timeString
* @return
*/
public static String switchTimeFormat(String timeString) {
int length = timeString.length();
switch (length) {
case 8:
return TIME_FORMAT_8;
case 5:
return TIME_FORMAT_5;
default:
throw new IllegalArgumentException("can not find time format for:" + timeString);
}
}

/**
* Format date
* <p>
Expand Down Expand Up @@ -279,6 +317,35 @@ public static String format(LocalDate date, String dateFormat, Locale local) {
return date.format(getCacheDateTimeFormat(dateFormat, local));
}

/**
* Format time
*
* @param time LocalTime
* @param timeFormat time format
* @return format string
*/
public static String format(LocalTime time, String timeFormat) {
return format(time, timeFormat, null);
}

/**
* Format time
*
* @param time LocalTime
* @param timeFormat time format
* @param local local
* @return format string
*/
public static String format(LocalTime time, String timeFormat, Locale local) {
if (time == null) {
return null;
}
if (StringUtils.isEmpty(timeFormat)) {
timeFormat = DEFAULT_LOCAL_TIME_FORMAT;
}
return time.format(getCacheDateTimeFormat(timeFormat, local));
}

/**
* Format date
*
Expand Down Expand Up @@ -458,6 +525,24 @@ public static LocalDate getLocalDate(double date, boolean use1904windowing) {
return localDateTime == null ? null : localDateTime.toLocalDate();
}

/**
* Given an Excel date with either 1900 or 1904 date windowing,
* converts it to a java.time.LocalTime.
*
* Excel Dates and Times are stored without any timezone
* information. The date component is discarded; only the
* wall-clock time of day is returned.
*
* @param date The Excel date.
* @param use1904windowing true if date uses 1904 windowing,
* or false if using 1900 date windowing.
* @return Java representation of the time, or null if date is not a valid Excel date
*/
public static LocalTime getLocalTime(double date, boolean use1904windowing) {
LocalDateTime localDateTime = getLocalDateTime(date, use1904windowing);
return localDateTime == null ? null : localDateTime.toLocalTime();
}

/**
* Determine if it is a date format.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void readAndWrite(ExcelFormat format) throws Exception {
Assertions.assertEquals(TestUtil.TEST_DATE, row.getDate());
Assertions.assertEquals(TestUtil.TEST_LOCAL_DATE, row.getLocalDate());
Assertions.assertEquals(TestUtil.TEST_LOCAL_DATE_TIME, row.getLocalDateTime());
Assertions.assertEquals(TestUtil.TEST_LOCAL_TIME, row.getLocalTime());
Assertions.assertEquals(Boolean.TRUE, row.getBooleanData());
Assertions.assertEquals(row.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
Assertions.assertEquals(row.getBigInteger().intValue(), BigInteger.ONE.intValue(), 0.0);
Expand Down
Loading
Loading