123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Text;
- using Vinno.vCloud.Protocol.Infrastructures;
- namespace ReportDataResumeTool.Common
- {
- public class ConvertHelper
- {
- /// <summary>
- /// list转化为table
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="entitys"></param>
- /// <returns></returns>
- public static DataTable ListToDataTable<T>(IList<T> dataList)
- {
- //检查实体集合不能为空
- if (dataList == null || dataList.Count < 1)
- {
- return new DataTable();
- }
- //取出第一个实体的所有Propertie
- Type entityType = dataList[0].GetType();
- PropertyInfo[] entityProperties = entityType.GetProperties();
- //生成DataTable的structure
- //生产代码中,应将生成的DataTable结构Cache起来,此处略
- DataTable dt = new DataTable("dt");
- for (int i = 0; i < entityProperties.Length; i++)
- {
- //dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
- dt.Columns.Add(entityProperties[i].Name);
- }
- //将所有entity添加到DataTable中
- foreach (object entity in dataList)
- {
- //检查所有的的实体都为同一类型
- if (entity.GetType() != entityType)
- {
- throw new Exception("要转换的集合元素类型不一致");
- }
- object[] entityValues = new object[entityProperties.Length];
- for (int i = 0; i < entityProperties.Length; i++)
- {
- entityValues[i] = entityProperties[i].GetValue(entity, null);
- }
- dt.Rows.Add(entityValues);
- }
- return dt;
- }
- /// <summary>
- /// datatable转list
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="table"></param>
- /// <returns></returns>
- public static IList<T> DataTableToList<T>(DataTable table) where T : class, new()
- {
- IList<T> list = new List<T>();
- foreach (DataRow row in table.Rows)
- {
- T obj = new T();
- foreach (PropertyInfo info in obj.GetType().GetProperties())
- {
- if (table.Columns.Contains(info.Name))
- {
- if (row[info.Name] != DBNull.Value)
- {
- info.SetValue(obj, row[info.Name], null);
- }
- }
- }
- list.Add(obj);
- }
- return list;
- }
- /// <summary>
- /// utc转bj时间
- /// </summary>
- public static DateTime UTCToBeijing(DateTime utcTime)
- {
- // 获取中国标准时间 (CST) 的时区信息
- TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
- // 将UTC时间转换为北京时间
- DateTime beijingTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, cstZone);
- return beijingTime;
- }
- /// <summary>
- /// 时间戳转UTC时间
- /// </summary>
- public static DateTime GetUTCTimeFromUnixTime(long timestamp)
- {
- DateTime utcDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
- DateTime beijingTime = utcDateTime.AddMilliseconds(timestamp).ToUniversalTime();
- return beijingTime;
- }
- /// <summary>
- /// 时间戳转北京时间
- /// </summary>
- public static DateTime GetBeiJingTimeFromUnixTime(long timestamp)
- {
- DateTime utcDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
- DateTime beijingTime = utcDateTime.AddMilliseconds(timestamp).ToUniversalTime().AddHours(8);
- return beijingTime;
- }
- /// <summary>
- /// 通过身份证查询出生日期
- /// </summary>
- /// <param name="b_car_code"></param>
- /// <returns></returns>
- public static string GetBirthdayByCode(string b_car_code)
- {
- string birthday = "";
- //处理18位的身份证号码从号码中得到生日和性别代码
- if (b_car_code.Length == 18)
- {
- //110101199001011234
- birthday = b_car_code.Substring(6, 4) + "-" + b_car_code.Substring(10, 2) + "-" + b_car_code.Substring(12, 2);
- }
- //处理15位的身份证号码从号码中得到生日和性别代码
- if (b_car_code.Length == 15)
- {
- birthday = "19" + b_car_code.Substring(6, 2) + "-" + b_car_code.Substring(8, 2) + "-" + b_car_code.Substring(10, 2);
- }
- return birthday;
- }
- /// <summary>
- /// 通过身份证查询出生日期
- /// </summary>
- /// <param name="b_car_code"></param>
- /// <returns></returns>
- public static GenderType GetSexByCode(string b_car_code)
- {
- GenderType genderType = GenderType.Unassigned;
- //处理18位的身份证号码从号码中得到生日和性别代码
- if (b_car_code.Length == 18)
- {
- int genderDigit = int.Parse(b_car_code.Substring(16, 1));
- genderType = genderDigit % 2 == 0 ? GenderType.Female : GenderType.Male;
- }
- //处理15位的身份证号码从号码中得到生日和性别代码
- if (b_car_code.Length == 15)
- {
- int genderDigit = int.Parse(b_car_code.Substring(14, 1));
- genderType = genderDigit % 2 == 0 ? GenderType.Female : GenderType.Male;
- }
- return genderType;
- }
- /// <summary>
- /// 获取计算年龄
- /// </summary>
- /// <param name="birthdate"></param>
- /// <returns></returns>
- public static int CalculateAge(DateTime birthdate)
- {
- DateTime now = DateTime.Today;
- int age = now.Year - birthdate.Year;
- if (now < birthdate.AddYears(age))
- age--;
- return age;
- }
- }
- }
|