Exemple #1
0
 public static DateTime CastDateTime(Object obj, DateTime defaultValue)
 {
     try
     {
         DateTime DateTimeValue = defaultValue;
         if (obj != null)
         {
             string str = CastUtil.CastString(obj);
             if (!string.IsNullOrEmpty(str))
             {
                 if (str.Contains("-"))
                 {
                     DateTimeValue = DateTime.ParseExact(str, "yyyy-MM-dd", null);
                 }
                 else
                 {
                     DateTimeValue = DateTime.ParseExact(str, "yyyyMMdd", null);
                 }
             }
         }
         return(DateTimeValue);
     }
     catch
     {
         return(DateTime.Now);
     }
 }
Exemple #2
0
        public static Decimal CastDecimal(Object obj, Decimal defaultValue)
        {
            Decimal decimalValue = defaultValue;

            if (obj != null)
            {
                string str = CastUtil.CastString(obj);
                if (!string.IsNullOrEmpty(str))
                {
                    decimalValue = Convert.ToDecimal(str);
                }
            }
            return(decimalValue);
        }
Exemple #3
0
 /// <summary>
 /// 将 Object 类型转换为 string 类型
 /// 默认值为""
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static string CastString(Object obj)
 {
     return(CastUtil.CastString(obj, ""));
 }