C#数据转换类ConvertEx,封装.Net的Convert对象

  C#数据转换类ConvertEx,封装.Net的Convert对象


C#数据转换类ConvertEx,封装.Net的Convert对象



C# Code:

/// <summary>
///C#数据转换类ConvertEx,封装.Net的Convert对象.
/// </summary>
public class ConvertEx
{
  
/// <summary>
  
/// 将日期转换为指定格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <param name="format">日期格式, 如: yyyy-MM-dd</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToDateString(DateTime dt, string format)
  {
    
return dt.ToString(format, DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年/月/日格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYYMMDDLong(DateTime dt)
  {
    
return dt.ToString("yyyy/MM/dd", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年月(YYMM)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYMM(DateTime dt)
  {
    
return dt.ToString("yyMM", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年(YY)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYY(DateTime dt)
  {
    
return dt.ToString("yy", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年(YYYY)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYY(DateTime dt)
  {
    
return dt.ToString("yyyy", DateTimeFormatInfo.CurrentInfo);
  }
  
  
  
/// <summary>
  
/// 将日期转换为年月(YYYYMM)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYYMM(DateTime dt)
  {
    
return dt.ToString("yyyyMM", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年月日(YYYYMMDD)格式的字符串,当日期为系统最小日期否返回空字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYYMMDD(DateTime dt)
  {
    
string temp = dt.ToString("yyyyMMdd", DateTimeFormatInfo.CurrentInfo);
    
if (temp == "00010101") temp = "";
    
return temp;
  }
  
  
/// <summary>
  
/// 将日期转换为年月日时分秒(YYYYMMDDHHMMSS)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYYMMDDHHMMSS(DateTime dt)
  {
    
return dt.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.CurrentInfo);
  }
  
  
public static string ToCharYYYYMMDDHHMM(DateTime dt)
  {
    
return dt.ToString("yyyyMMddHHmm", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为日/月/年 时:分:秒(DD/MM/YYYY HH:MM:SS)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharDD_MM_YYYY_HHMMSS(DateTime dt)
  {
    
return dt.ToString("dd/MM/yyyy HH:mm:ss", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年/月/日 时:分:秒(YYYY/MM/DD HH:MM:SS)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYY_MM_DD_HHMMSS(DateTime dt)
  {
    
return dt.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年月日 时:分:秒(YYYYMMDD HH:MM:SS)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYYYMMDDHHMMSS000(DateTime dt)
  {
    
return dt.ToString("yyyyMMdd HH:mm:ss:000", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 将日期转换为年月日(YYMMDD)格式的字符串
  
/// </summary>
  
/// <param name="dt">日期</param>
  
/// <returns>返回字符串类型</returns>
  
public static string ToCharYYMMDD(DateTime dt)
  {
    
return dt.ToString("yyMMdd", DateTimeFormatInfo.CurrentInfo);
  }
  
  
/// <summary>
  
/// 转换为Boolean类型
  
/// </summary>
  
/// <param name="o">必须是可转换为Boolean类型的字符串如"True","False",或其它特殊对象</param>
  
/// <returns>返回bool类型</returns>
  
public static bool ToBoolean(object o)
  {
    
if (null == o) return false;
    
try
    {
      
return Convert.ToBoolean(o.ToString());
    }
    
catch { return false; }
  }
  
  
/// <summary>
  
/// 转换为浮点类型
  
/// </summary>
  
/// <param name="o"></param>
  
/// <returns></returns>
  
public static float ToFloat(object o)
  {
    
if (null == o) return 0;
    
try
    {
      
return (float)Convert.ToDouble(o.ToString());
    }
    
catch { return 0; }
  }
  
  
/// <summary>
  
/// 转换为decimal类型(有效十进制数)
  
/// </summary>
  
/// <param name="o"></param>
  
/// <returns></returns>
  
public static decimal ToDecimal(object o)
  {
    
if (null == o) return 0;
    
try
    {
      
return Convert.ToDecimal(o.ToString());
    }
    
catch { return 0; }
  }
  
  
/// <summary>
  
/// 四舍五入
  
/// </summary>
  
/// <param name="d"></param>
  
/// <returns></returns>
  
public static object ToDecimalFormat(decimal d,int round,string format= "0.00")
  {
    
return (object)(decimal.Round(d, round).ToString(format));
  }
  
  
/// <summary>
  
/// 转换为日期对象
  
/// </summary>
  
/// <param name="o">日期对象(泛型),如为空的字段数据DBNull.NULL</param>
  
/// <returns></returns>
  
public static DateTime ToDateTime(DateTime? o)
  {
    
if (null == o) return DateTime.MinValue;
    
try
    {
      
return DateTime.Parse(o.ToString());
    }
    
catch { return DateTime.MinValue; }
  }
  
  
/// <summary>
  
/// 转换为SQL支持的日期格式, 日期范围:1753-1-1 ~ 9999-12-31
  
/// </summary>
  
/// <param name="o"></param>
  
/// <returns></returns>
  
public static DateTime ToSqlDateTime(object o)
  {
    DateTime defMinValue
= DateTime.Parse("1753-01-01 00:00:00");
    DateTime defMaxValue
= DateTime.Parse("9999-12-31 23:59:59");
    
if (null == o) return defMinValue;//传入空值,返回预设值
    

    DateTime dt;
    
if (DateTime.TryParse(o.ToString(), out dt))
    {
      
if (dt < defMinValue || dt > defMaxValue)
      
return defMinValue;//无效日期,预设返回SQL支持的最小日期
      
else
      
return dt;
    }
    
return defMinValue;
  }
  
  
/// <summary>
  
/// 转换日期字符串. 对象为空则返回'null'字符串(无单引号).
  
/// 用于动态组合SQL字符串.
  
/// </summary>
  
/// <param name="o">日期对象</param>
  
/// <param name="dateFormat">日期格式</param>
  
/// <param name="quotationMark">返回的日期字符串是否打上单引号</param>
  
public static string ToDateTimeString(object o, string dateFormat, bool quotationMark)
  {
    
try
    {
      DateTime
? dateGenType = ToDateTime(o);
      
string datestr = string.Empty;
      
if (dateGenType == null)
      
return "";
      
else
      {
        DateTime dt
= DateTime.Parse(dateGenType.ToString());
        datestr
= dt.ToString(dateFormat, DateTimeFormatInfo.CurrentInfo);
      }
      
if (quotationMark)
      
return "'" + datestr + "'";
      
else
      
return datestr;
    }
    
catch
    {
      
return "";
    }
  }
  
  
/// <summary>
  
/// 转换为日期对象
  
/// </summary>
  
/// <param name="o"></param>
  
/// <returns></returns>
  
public static DateTime? ToDateTime(object o)
  {
    
if (null == o) return null;
    
try
    {
      
return Convert.ToDateTime(o.ToString());
    }
    
catch { return null; }
  }
  
  
public static DateTime ToDateTimeEx(object o)
  {
    
if (null == o) return DateTime.MinValue;
    
try
    {
      DateTime dt
= Convert.ToDateTime(o.ToString());
      
if (dt < DateTime.Parse("1900-01-01"))
      
return DateTime.Parse("1900-01-01");
      
else
      
return dt;
    }
    
catch { return DateTime.MinValue; }
  }
  
  
/// <summary>
  
/// 转换为整数
  
/// </summary>
  
/// <param name="o"></param>
  
/// <returns></returns>
  
public static int ToInt(object o)
  {
    
if (null == o) return 0;
    
try
    {
      
return Convert.ToInt32(o.ToString());
    }
    
catch { return 0; }
  }
  
  
/// <summary>
  
/// 转换为字符串
  
/// </summary>
  
/// <param name="obj">当对象为空,返回string.Empty</param>
  
/// <returns></returns>
  
public static string ToString(object obj)
  {
    
if (obj == null) return string.Empty;
    
return obj.ToString().Trim();
  }
  
  
/// <summary>
  
/// 截取左侧指定长度字符串
  
/// </summary>
  
/// <param name="content">字符串</param>
  
/// <param name="length">长度</param>
  
/// <returns></returns>
  
public static string StrLeft(string content, int length)
  {
    
if (String.IsNullOrEmpty(content)) return "";
    
if (content.Length <= length)
    
return content;
    
else
    
return content.Substring(0, length);
  }
  
}


//来源:C/S框架网 | www.csframework.com | QQ:23404761



C/S框架网|原创精神.创造价值.打造精品

扫一扫加微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务


版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
发表评论

评论内容
昵称:
关联文章

C#数据转换ConvertEx,封装.NetConvert对象
C#.NET 对象(字符串)转换常用扩展方法
Delphi封装Novell LDAP底层API, 提供给C#.Net开发
C#扩展方法,字符串对象转换,常用扩展方法StringExtensions
文件或目录长文件名与短文件名互转换C#.Net
C#.NET 定义动态对象dynamic以及转换JSON字符串
C#.Net 同名方法重载调用顺序
C# ADO.NET根据实体对象类型转换创建DataTable对象
将SQLServer数据类型转换C#.Net类型
C#.Net 转换人民币大写金额
C#转换IList对象数组为byte数组.然后通过WebService传递
CSFramework.DB多数据底层库 - 操作对象数据DbDataUpdate接口测试
C#图片转换支持按比例缩放图片
将SQLServer数据类型转换为.Net中SqlDbType类型
DataTable与Object互相转换及相关函数转换大全(DataConvert)
【原创】C# 深度拷贝对象 使用.NET反射+递归原理实现深度克隆
标签:C#.Net组件开发 - 自定义设计器持久化对象属性
C# Model对象实例转换为URL网址参数Param形式
C# 动态对象转换为实体对象支持泛型(DynamicToEntity)
CSFramework代码生成器生成三种ORM模型(静态ORM,实体ORM,标准对象