栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现Oralce的TO

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现Oralce的TO

//一个参数的情况

public static String toChar(Object o) {
    if (o == null) {
        return null;
    }

    String returnValue = String.valueOf(o);
    return StringUtils.isEmpty(returnValue) ? null : returnValue;
}

//两个参数的情况

public static  String toChar(T data, String format) {
    if (data == null) {
        return null;
    }

    if (data instanceof Date date) {
        return formatDate(date, format);
    }

    return formatFm(toChar(data), format);
}

//日期格式
public static String formatDate(Date data, String format) {
    LocalDateTime localDateTime;

    localDateTime = LocalDateTime.ofInstant((data).toInstant(), ZoneId.systemDefault());

    Locale locale = Locale.JAPANESE;

    //Year
    String year = String.valueOf(localDateTime.getYear());

    //Month e.g. Nov
    String month_short = localDateTime.getMonth().getDisplayName(TextStyle.SHORT, locale);
    //Month e.g. 11
    String month_num = String.valueOf(localDateTime.getMonthValue() > 9 ? localDateTime.getMonthValue() : "0" + localDateTime.getMonthValue());

    //月の日,1~31
    String dayOfMonth = String.valueOf(localDateTime.getDayOfMonth() > 9 ? localDateTime.getDayOfMonth() : "0" + localDateTime.getDayOfMonth());

    //HH24
    String hour24 = String.valueOf(localDateTime.getHour() > 9 ? localDateTime.getHour() : "0" + localDateTime.getHour());
    //HH12
    int hour12_temp = localDateTime.getHour();
    if (localDateTime.getHour() > 12) {
        hour12_temp -= 12;
    }
    String hour12 = String.valueOf(hour12_temp > 9 ? hour12_temp : "0" + hour12_temp);

    //minute
    String minute = String.valueOf(localDateTime.getMinute() > 9 ? localDateTime.getMinute() : "0" + localDateTime.getMinute());

    //second
    String second = String.valueOf(localDateTime.getSecond() > 9 ? localDateTime.getSecond() : "0" + localDateTime.getSecond());

    String date_format = format
            .replaceAll("(?i)YYYY", year)
            .replaceAll("(?i)YY", year.substring(2))
            .replaceAll("MON", month_short.toUpperCase())
            .replaceAll("Mon", month_short)
            .replaceAll("mon", month_short.toLowerCase())
            .replaceAll("(?i)MM", month_num)
            .replaceAll("(?i)DD", dayOfMonth)
            .replaceAll("(?i)HH24", hour24)
            .replaceAll("(?i)HH12", hour12)
            .replaceAll("(?i)HH", hour12)
            .replaceAll("(?i)MI", minute)
            .replaceAll("(?i)SS", second);

    return date_format;
}

//数字格式
public static String formatFm(String data, String format) {
    format = format.toUpperCase();
    String format_str = format.replace("FM", "");

    BigDecimal bigDecimal = new BigDecimal(data);
    String pattern = "";
    if (format_str.startsWith("0")) {
        pattern = format_str.replace("9", "0");
    } else if (format_str.contains("0.") && format_str.split("\.")[1].endsWith("0")) {
        pattern = format_str.split("\.")[0].replace("9", "#") + "." + format_str.split("\.")[1].replace("9", "0");
    } else {
        pattern = format_str.replace("9", "#");
    }

    DecimalFormat decimalFormat = new DecimalFormat(pattern);
    decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
    String data_str = decimalFormat.format(bigDecimal);

//超出最大位后显示#
    if (data_str.length() > format_str.length()) {
        String result = "";
        for (int i = 0; i <= format_str.length(); i++) {
            result += "#";
        }
        return result;
    }
    return data_str;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/933103.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号