JAVA 轉換問題

來源:互聯網
上載者:User

  /**
     * 將字串時間轉換成java.util.Date類型
     * @param str 要轉換的字元
     * @param format 時間格式
     * @return 如果轉換失敗,返回null
     */
    public static Date string2Date(String str, String format)
    {
        if (FwUtil.isEmpty(str) || FwUtil.isEmpty(format))
        {
            return null;
        }
        
        // 定義日期/時間格式
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date;
        try
        {
            // 轉換日期/時間格式
            date = sdf.parse(str);
            
            if (!str.equals(sdf.format(date)))
            {
                date = null;
            }
        }
        catch (ParseException e)
        {
            date = null;
        }
        
        return date;
    }
    /**
     * 如果傳入的字串為null,則傳出""
     * 
     * @param inStr 傳入字串
     * @return outStr
     */
    public static String changeNullToSpace(Object inStr) {

        return inStr == null ? "" : inStr.toString();
    }

    /**
     * 去除右邊全半形空格
     * 
     * @param strValue String
     * @return String String
     */
    public static String rTrim(String strValue) {

        if ((strValue != null) && (!strValue.equals(""))) {
            char[] cValue = strValue.toCharArray();
            int nCur = 0;

            for (int i = cValue.length - 1; i > -1; i--) {
                if ((cValue[i] != '\u0020') && (cValue[i] != '\u3000')) {
                    nCur = i;
                    break;
                }
            }

            if ((nCur == 0) && ((cValue[0] == '\u0020') || (cValue[0] == '\u3000'))) {
                return "";
            }

            return strValue.substring(0, nCur + 1);
        } else {
            return "";
        }
    }

    /**
     * 去除半形空格
     * 
     * @param strValue String
     * @return String String
     */
    public static String trim(String strValue) {

        String strReturn = strValue;
        if ((strValue != null)) {
            strReturn = strValue.trim();
        }
        return strReturn;
    }

    /**
     * <PRE>
     * 
     * 轉換null為"0".
     * 
     * </PRE>.
     * 
     * @param str String
     * @return String 字串為null返回"0";否則返回原字串.
     */
    public static String zeroChangeNull(String str) {
        String result = str;

        if (null == str) {
            result = "0";
        }

        return result;
    }

    /**
     * <PRE>
     * 
     * 轉換字串為Integer
     * 
     * </PRE>
     * 
     * @param value String
     * @return 轉換後的Integer,失敗是返回0
     */
    public static Integer toInteger(String value) {

        try {
            return Integer.valueOf(value.trim());
        } catch (NumberFormatException e) {
            return new Integer(0);
        }
    }

    /**
     * <PRE>
     * 
     * 轉換字串為Long
     * 
     * </PRE>
     * 
     * @param value String
     * @return 轉換後的Long,失敗是返回0
     */
    public static Long toLong(String value) {

        try {
            return Long.valueOf(value);
        } catch (NumberFormatException e) {
            return new Long(0);
        }
    }

    /**
     * <PRE>
     * 
     * 轉換字串為Short
     * 
     * </PRE>
     * 
     * @param value String
     * @return 轉換後的Short,失敗是返回0
     */
    public static Short toShort(String value) {

        try {
            return Short.valueOf(value);
        } catch (NumberFormatException e) {
            return new Short("0");
        }
    }

    /**
     * <PRE>
     * 
     * 轉換字串為Long
     * 
     * </PRE>
     * 
     * @param value String
     * @return 轉換後的Long,失敗是返回null
     */
    public static Long toLongWithNull(String value) {

        try {
            return Long.valueOf(value);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * <PRE>
     * 
     * 轉換字串為Double
     * 
     * </PRE>
     * 
     * @param value String
     * @return 轉換後的Double,失敗是返回0
     */
    public static Double toDouble(String value) {

        try {
            return Double.valueOf(value);
        } catch (NumberFormatException e) {
            return new Double(0);
        }
    }

    /**
     * <PRE>
     * 
     * 轉換字串為Boolean
     * 
     * </PRE>
     * 
     * @param value 0,1)
     * @return Boolean 1-True/非1-False
     */
    public static Boolean toBoolean(String value) {

        if ("1".equals(value)) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }

    }

    /**
     * 4舍5入,整型來表示
     * 
     * @param value value
     * @return String 4舍5入後的值
     */
    public static String toInt(String value) {
        if (CheckFun.isBlankOrNull(value)) {
            return "";
        } else {
            return String.valueOf(new java.math.BigDecimal(value).setScale(0, java.math.BigDecimal.ROUND_HALF_UP).intValue()).toString();
        }

    }

    /**
     * 4舍5入,保留小數點後2位
     * 
     * @param value value
     * @param nScal nScal
     * @return String 4舍5入後的值
     */
    public static String toDouble(double value, int nScal) {

        return String.valueOf(new java.math.BigDecimal(value).setScale(nScal, java.math.BigDecimal.ROUND_HALF_UP).doubleValue());
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.