All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gitee.ludii.excel.utils.WriteFormatUtils Maven / Gradle / Ivy

package io.gitee.ludii.excel.utils;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author 陆迪
 * @date 2022/4/25
 */
public class WriteFormatUtils {

    public final static String DEFAULT_DATE_PATTER = "yyyy-MM-dd HH:mm:ss";

    public final static String DEFAULT_LOCAL_DATE_PATTER = "yyyy-MM-dd";

    public final static String DEFAULT_LOCAL_DATE_TIME_PATTER = "yyyy-MM-dd HH:mm:ss";

    public static String formatToString(Number value, String numberFormat) {
        if (value == null) {
            return null;
        }
        if (CommonUtils.isBlank(numberFormat)) {
            return value.toString();
        }
        DecimalFormat decimalFormat = new DecimalFormat(numberFormat);
        decimalFormat.setRoundingMode(RoundingMode.HALF_UP);

        return decimalFormat.format(value);
    }

    public static String formatToString(Date value, String dateFormat) {
        if (value == null) {
            return null;
        }
        if (CommonUtils.isBlank(dateFormat)) {
            dateFormat = DEFAULT_DATE_PATTER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);

        return simpleDateFormat.format(value);
    }

    public static String formatToString(LocalDate value, String dateFormat) {
        if (value == null) {
            return null;
        }
        if (CommonUtils.isBlank(dateFormat)) {
            dateFormat = DEFAULT_LOCAL_DATE_PATTER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);

        return simpleDateFormat.format(value);
    }

    public static String formatToString(LocalDateTime value, String dateFormat) {
        if (value == null) {
            return null;
        }
        if (CommonUtils.isBlank(dateFormat)) {
            dateFormat = DEFAULT_LOCAL_DATE_TIME_PATTER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);

        return simpleDateFormat.format(value);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy