公共帮助类
1 public class CommonHelper
2 {
3 #region 类型转换
4 ///
5 /// 返回对象obj的String值,obj为null时返回空值。
6 ///
7 /// 对象。
8 /// 字符串。
9 public static string ToObjectString(object obj)
10 {
11 return null == obj ? String.Empty : obj.ToString();
12 }
13 ///
14 /// 取得Int值,如果为Null 则返回0
15 ///
16 ///
17 ///
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 ///
38 /// 取得Int值,如果不成功则返回指定exceptionvalue值
39 ///
40 /// 要计算的值
41 /// 异常时的返回值
42 ///
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 ///
56 /// 取得byte值
57 ///
58 ///
59 ///
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 ///
69 /// 获得Long值
70 ///
71 ///
72 ///
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 ///
82 /// 取得Long值,如果不成功则返回指定exceptionvalue值
83 ///
84 /// 要计算的值
85 /// 异常时的返回值
86 ///
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 ///
110 /// 取得Decimal值
111 ///
112 ///
113 ///
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 ///
123 /// 取得DateTime值
124 ///
125 ///
126 ///
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 ///
136 /// 取得DateTime值
137 ///
138 ///
139 ///
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 ///
154 /// 格式化日期 yyyy-MM-dd HH:mm
155 ///
156 ///
157 ///
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 ///
166 /// 取得bool值
167 ///
168 ///
169 ///
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 ///
183 /// 取得byte[]
184 ///
185 ///
186 ///
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 ///
198 /// 取得string值
199 ///
200 ///
201 ///
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 ///
210 /// 判断用户输入是否为日期
211 ///
212 ///
213 ///
214 ///
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 ///
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 ///
289 /// 判断文本obj是否为空值。
290 ///
291 /// 对象。
292 /// Boolean值。
293 public static bool IsEmpty(string obj)
294 {
295 return ToObjectString(obj).Trim() == String.Empty ? true : false;
296 }
297
298 ///
299 /// 判断对象是否为正确的日期值。
300 ///
301 /// 对象。
302 /// Boolean。
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 ///
317 /// 判断对象是否为正确的Int32值。
318 ///
319 /// 对象。
320 /// Int32值。
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 ///
333 /// 判断对象是否为正确的Long值。
334 ///
335 /// 对象。
336 /// Long值。
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 ///
349 /// 判断对象是否为正确的Float值。
350 ///
351 /// 对象。
352 /// Float值。
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 ///
365 /// 判断对象是否为正确的Double值。
366 ///
367 /// 对象。
368 /// Double值。
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 ///
381 /// 判断对象是否为正确的Decimal值。
382 ///
383 /// 对象。
384 /// Decimal值。
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 ///
399 /// 获取一个全球唯一码GUID字符串
400 ///
401 public static string GetGuid
402 {
403 get
404 {
405 return Guid.NewGuid().ToString().ToUpper().Replace("-", "");
406 }
407 }
408 #endregion
409
410 #region 自动生成日期编号
411 ///
412 /// 自动生成编号 201008251145409865
413 ///
414 ///
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 ///
426 /// 生成0-9随机数
427 ///
428 /// 生成长度
429 ///
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 ///
446 /// 路径转换(转换成绝对路径)
447 ///
448 ///
449 ///
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 ///
465 /// 排序字段转换
466 ///
467 /// 默认字段
468 /// 排序字段
469 ///
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 }