常用Java对象转换工具类

package com.leng.demo.utils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 常用Java对象转换工具类,包括以下几种类型:
 * 1. 字符串与数字之间的转换
 * 2. 字符串与日期时间之间的转换
 */
public final class ConvertUtil {
    private ConvertUtil() {
    }

    /**
     * 将字符串转换成整型数据
     *
     * @param str 需要转换的字符串
     * @return 返回转换后的整型数据,如果无法转换则返回0
     */
    public static int strToInt(String str) {
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    /**
     * 将字符串转换成长整型数据
     *
     * @param str 需要转换的字符串
     * @return 返回转换后的长整型数据,如果无法转换则返回0
     */
    public static long strToLong(String str) {
        try {
            return Long.parseLong(str);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    /**
     * 将字符串转换成浮点型数据
     *
     * @param str 需要转换的字符串
     * @return 返回转换后的浮点型数据,如果无法转换则返回0
     */
    public static float strToFloat(String str) {
        try {
            return Float.parseFloat(str);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    /**
     * 将字符串转换成双精度浮点型数据
     *
     * @param str 需要转换的字符串
     * @return 返回转换后的双精度浮点型数据,如果无法转换则返回0
     */
    public static double strToDouble(String str) {
        try {
            return Double.parseDouble(str);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    /**
     * 将字符串转换成BigDecimal型数据,精度为2位小数
     *
     * @param str 需要转换的字符串
     * @return 返回转换后的BigDecimal型数据,如果无法转换则返回0
     */
    public static BigDecimal strToDecimal(String str) {
        try {
            return new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP);
        } catch (NumberFormatException e) {
            return BigDecimal.ZERO;
        }
    }

    /**
     * 将字符串转换成日期时间类型
     *
     * @param str       需要转换的字符串
     * @param formatStr 字符串格式,例如:yyyy-MM-dd HH:mm:ss
     * @return 返回转换后的日期时间类型,如果无法转换则返回null
     */
    public static Date strToDate(String str, String formatStr) {
        DateFormat df = new SimpleDateFormat(formatStr);
        try {
            return df.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 把日期时间类型转换成指定格式的字符串
     *
     * @param date      要转换的日期时间类型
     * @param formatStr 字符串格式,例如:yyyy-MM-dd HH:mm:ss
     * @return 返回指定格式的字符串,如果参数不合法则返回空串
     */
    public static String dateToStr(Date date, String formatStr) {
        if (date == null || formatStr == null || "".equals(formatStr.trim())) {
            return "";
        }
        DateFormat df = new SimpleDateFormat(formatStr);
        try {
            return df.format(date);
        } catch (Exception e) {
            return "";
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值