1 public class CommonHelper
2 {
3 #region 类型转换
4 /// <summary>
5 /// 返回对象obj的String值,obj为null时返回空值。
6 /// </summary>
7 /// <param name="obj">对象。</param>
8 /// <returns>字符串。</returns>
9 public static string ToObjectString(object obj)
10 {
11 return null == obj ? String.Empty : obj.ToString();
12 }
13 /// <summary>
14 /// 取得Int值,如果为Null 则返回0
15 /// </summary>
16 /// <param name="obj"></param>
17 /// <returns></returns>
18 public static int GetInt(object obj)
19 {
20 if (obj != null)
21 {
22 int i;
23 int.TryParse(obj.ToString(), out i);
24 return i;
25 }
26 else
27 return 0;
28 }
29
30 public static float GetFloat(object obj)
31 {
32 float i;
33 float.TryParse(obj.ToString(), out i);
34 return i;
35 }
36
37 /// <summary>
38 /// 取得Int值,如果不成功则返回指定exceptionvalue值
39 /// </summary>
40 /// <param name="obj">要计算的值</param>
41 /// <param name="exceptionvalue">异常时的返回值</param>
42 /// <returns></returns>
43 public static int GetInt(object obj, int exceptionvalue)
44 {
45 if (obj == null)
46 return exceptionvalue;
47 if (string.IsNullOrEmpty(obj.ToString()))
48 return exceptionvalue;
49 int i = exceptionvalue;
50 try { i = Convert.ToInt32(obj); }
51 catch { i = exceptionvalue; }
52 return i;
53 }
54
55 /// <summary>
56 /// 取得byte值
57 /// </summary>
58 /// <param name="obj"></param>
59 /// <returns></returns>
60 public static byte Getbyte(object obj)
61 {
62 if (obj != null && obj.ToString() != "")
63 return byte.Parse(obj.ToString());
64 else
65 return 0;
66 }
67
68 /// <summary>
69 /// 获得Long值
70 /// </summary>
71 /// <param name="obj"></param>
72 /// <returns></returns>
73 public static long GetLong(object obj)
74 {
75 if (obj != null && obj.ToString() != "")
76 return long.Parse(obj.ToString());
77 else
78 return 0;
79 }
80
81 /// <summary>
82 /// 取得Long值,如果不成功则返回指定exceptionvalue值
83 /// </summary>
84 /// <param name="obj">要计算的值</param>
85 /// <param name="exceptionvalue">异常时的返回值</param>
86 /// <returns></returns>
87 public static long GetLong(object obj, long exceptionvalue)
88 {
89 if (obj == null)
90 {
91 return exceptionvalue;
92 }
93 if (string.IsNullOrEmpty(obj.ToString()))
94 {
95 return exceptionvalue;
96 }
97 long i = exceptionvalue;
98 try
99 {
100 i = Convert.ToInt64(obj);
101 }
102 catch
103 {
104 i = exceptionvalue;
105 }
106 return i;
107 }
108
109 /// <summary>
110 /// 取得Decimal值
111 /// </summary>
112 /// <param name="obj"></param>
113 /// <returns></returns>
114 public static decimal GetDecimal(object obj)
115 {
116 if (obj != null && obj.ToString() != "")
117 return decimal.Parse(obj.ToString());
118 else
119 return 0;
120 }
121
122 /// <summary>
123 /// 取得DateTime值
124 /// </summary>
125 /// <param name="obj"></param>
126 /// <returns></returns>
127 public static DateTime GetDateTime(object obj)
128 {
129 if (obj != null && obj.ToString() != "")
130 return DateTime.Parse(obj.ToString());
131 else
132 return DateTime.Now;
133 //return DateTime.MinValue;
134 }
135 /// <summary>
136 /// 取得DateTime值
137 /// </summary>
138 /// <param name="obj"></param>
139 /// <returns></returns>
140 public static DateTime? ToDateTime(object obj)
141 {
142 DateTime dt = DateTime.MinValue;
143 if (obj != null && obj.ToString() != "")
144 {
145 if (DateTime.TryParse(obj.ToString(), out dt))
146 return dt;
147 else
148 return null;
149 }
150 else
151 return null;
152 }
153 /// <summary>
154 /// 格式化日期 yyyy-MM-dd HH:mm
155 /// </summary>
156 /// <param name="obj"></param>
157 /// <returns></returns>
158 public static string GetFormatDateTime(object obj, string Format)
159 {
160 if (obj.ToString() != null && obj.ToString() != "")
161 return DateTime.Parse(obj.ToString()).ToString(Format);
162 else
163 return "";
164 }
165 /// <summary>
166 /// 取得bool值
167 /// </summary>
168 /// <param name="obj"></param>
169 /// <returns></returns>
170 public static bool GetBool(object obj)
171 {
172 if (obj != null)
173 {
174 bool flag;
175 bool.TryParse(obj.ToString(), out flag);
176 return flag;
177 }
178 else
179 return false;
180 }
181
182 /// <summary>
183 /// 取得byte[]
184 /// </summary>
185 /// <param name="obj"></param>
186 /// <returns></returns>
187 public static Byte[] GetByte(object obj)
188 {
189 if (obj.ToString() != null && obj.ToString() != "")
190 {
191 return (Byte[])obj;
192 }
193 else
194 return null;
195 }
196
197 /// <summary>
198 /// 取得string值
199 /// </summary>
200 /// <param name="obj"></param>
201 /// <returns></returns>
202 public static string GetString(object obj)
203 {
204 if (obj != null && obj != DBNull.Value)
205 return obj.ToString();
206 else
207 return "";
208 }
209 /// <summary>
210 /// 判断用户输入是否为日期
211 /// </summary>
212 /// <param ></param>
213 /// <returns></returns>
214 /// <remarks>
215 /// 可判断格式如下(其中-可替换为.,不影响验证)
216 /// YYYY | YYYY-MM |YYYY.MM| YYYY-MM-DD|YYYY.MM.DD | YYYY-MM-DD HH:MM:SS | YYYY.MM.DD HH:MM:SS | YYYY-MM-DD HH:MM:SS.FFF | YYYY.MM.DD HH:MM:SS:FF (年份验证从1000到2999年)
217 /// </remarks>
218 public static bool IsDateTime(string strValue)
219 {
220 if (strValue == null || strValue == "")
221 {
222 return false;
223 }
224 string regexDate = @"[1-2]{1}[0-9]{3}((-|[.]){1}(([0]?[1-9]{1})|(1[0-2]{1}))((-|[.]){1}((([0]?[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1})))( (([0-1]{1}[0-9]{1})|2[0-3]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{3})?)?)?)?$";
225 if (Regex.IsMatch(strValue, regexDate))
226 {
227 //以下各月份日期验证,保证验证的完整性
228 int _IndexY = -1;
229 int _IndexM = -1;
230 int _IndexD = -1;
231 if (-1 != (_IndexY = strValue.IndexOf("-")))
232 {
233 _IndexM = strValue.IndexOf("-", _IndexY + 1);
234 _IndexD = strValue.IndexOf(":");
235 }
236 else
237 {
238 _IndexY = strValue.IndexOf(".");
239 _IndexM = strValue.IndexOf(".", _IndexY + 1);
240 _IndexD = strValue.IndexOf(":");
241 }
242 //不包含日期部分,直接返回true
243 if (-1 == _IndexM)
244 {
245 return true;
246 }
247 if (-1 == _IndexD)
248 {
249 _IndexD = strValue.Length + 3;
250 }
251 int iYear = Convert.ToInt32(strValue.Substring(0, _IndexY));
252 int iMonth = Convert.ToInt32(strValue.Substring(_IndexY + 1, _IndexM - _IndexY - 1));
253 int iDate = Convert.ToInt32(strValue.Substring(_IndexM + 1, _IndexD - _IndexM - 4));
254 //判断月份日期
255 if ((iMonth < 8 && 1 == iMonth % 2) || (iMonth > 8 && 0 == iMonth % 2))
256 {
257 if (iDate < 32)
258 { return true; }
259 }
260 else
261 {
262 if (iMonth != 2)
263 {
264 if (iDate < 31)
265 { return true; }
266 }
267 else
268 {
269 //闰年
270 if ((0 == iYear % 400) || (0 == iYear % 4 && 0 < iYear % 100))
271 {
272 if (iDate < 30)
273 { return true; }
274 }
275 else
276 {
277 if (iDate < 29)
278 { return true; }
279 }
280 }
281 }
282 }
283 return false;
284 }
285 #endregion
286
287 #region 数据判断
288 /// <summary>
289 /// 判断文本obj是否为空值。
290 /// </summary>
291 /// <param name="obj">对象。</param>
292 /// <returns>Boolean值。</returns>
293 public static bool IsEmpty(string obj)
294 {
295 return ToObjectString(obj).Trim() == String.Empty ? true : false;
296 }
297
298 /// <summary>
299 /// 判断对象是否为正确的日期值。
300 /// </summary>
301 /// <param name="obj">对象。</param>
302 /// <returns>Boolean。</returns>
303 public static bool IsDateTime(object obj)
304 {
305 try
306 {
307 DateTime dt = DateTime.Parse(ToObjectString(obj));
308 if (dt > DateTime.MinValue && DateTime.MaxValue > dt)
309 return true;
310 return false;
311 }
312 catch
313 { return false; }
314 }
315
316 /// <summary>
317 /// 判断对象是否为正确的Int32值。
318 /// </summary>
319 /// <param name="obj">对象。</param>
320 /// <returns>Int32值。</returns>
321 public static bool IsInt(object obj)
322 {
323 try
324 {
325 int.Parse(ToObjectString(obj));
326 return true;
327 }
328 catch
329 { return false; }
330 }
331
332 /// <summary>
333 /// 判断对象是否为正确的Long值。
334 /// </summary>
335 /// <param name="obj">对象。</param>
336 /// <returns>Long值。</returns>
337 public static bool IsLong(object obj)
338 {
339 try
340 {
341 long.Parse(ToObjectString(obj));
342 return true;
343 }
344 catch
345 { return false; }
346 }
347
348 /// <summary>
349 /// 判断对象是否为正确的Float值。
350 /// </summary>
351 /// <param name="obj">对象。</param>
352 /// <returns>Float值。</returns>
353 public static bool IsFloat(object obj)
354 {
355 try
356 {
357 float.Parse(ToObjectString(obj));
358 return true;
359 }
360 catch
361 { return false; }
362 }
363
364 /// <summary>
365 /// 判断对象是否为正确的Double值。
366 /// </summary>
367 /// <param name="obj">对象。</param>
368 /// <returns>Double值。</returns>
369 public static bool IsDouble(object obj)
370 {
371 try
372 {
373 double.Parse(ToObjectString(obj));
374 return true;
375 }
376 catch
377 { return false; }
378 }
379
380 /// <summary>
381 /// 判断对象是否为正确的Decimal值。
382 /// </summary>
383 /// <param name="obj">对象。</param>
384 /// <returns>Decimal值。</returns>
385 public static bool IsDecimal(object obj)
386 {
387 try
388 {
389 decimal.Parse(ToObjectString(obj));
390 return true;
391 }
392 catch
393 { return false; }
394 }
395 #endregion
396
397 #region "全球唯一码GUID"
398 /// <summary>
399 /// 获取一个全球唯一码GUID字符串
400 /// </summary>
401 public static string GetGuid
402 {
403 get
404 {
405 return Guid.NewGuid().ToString().ToUpper().Replace("-", "");
406 }
407 }
408 #endregion
409
410 #region 自动生成日期编号
411 /// <summary>
412 /// 自动生成编号 201008251145409865
413 /// </summary>
414 /// <returns></returns>
415 public static string CreateNo()
416 {
417 Random random = new Random();
418 string strRandom = random.Next(1000, 10000).ToString(); //生成编号
419 string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如
420 return code;
421 }
422 #endregion
423
424 #region 生成0-9随机数
425 /// <summary>
426 /// 生成0-9随机数
427 /// </summary>
428 /// <param name="codeNum">生成长度</param>
429 /// <returns></returns>
430 public static string RndNum(int codeNum)
431 {
432 StringBuilder sb = new StringBuilder(codeNum);
433 Random rand = new Random();
434 for (int i = 1; i < codeNum + 1; i++)
435 {
436 int t = rand.Next(9);
437 sb.AppendFormat("{0}", t);
438 }
439 return sb.ToString();
440
441 }
442 #endregion
443
444 #region 路径转换(转换成绝对路径)
445 /// <summary>
446 /// 路径转换(转换成绝对路径)
447 /// </summary>
448 /// <param name="path"></param>
449 /// <returns></returns>
450 public static string WebPathTran(string path)
451 {
452 try
453 {
454 return HttpContext.Current.Server.MapPath(path);
455 }
456 catch
457 {
458 return path;
459 }
460 }
461 #endregion
462
463 #region 排序字段转换
464 /// <summary>
465 /// 排序字段转换
466 /// </summary>
467 /// <param name="acquiesce">默认字段</param>
468 /// <param name="orderField">排序字段</param>
469 /// <returns></returns>
470 public static string ToOrderField(string acquiesce, string orderField)
471 {
472 if (!string.IsNullOrEmpty(orderField))
473 {
474 return orderField;
475 }
476 return acquiesce;
477 }
478 #endregion
479 }