日期转换类

   1:   
   2:  // 创建日期: 2008-04-23
   3:  // 说    明: 日期,时间处理相关的类,大多转换为字符串。
   4:  using System;
   5:  using System.Collections.Generic;
   6:  using System.Text;
   7:   
   8:  namespace AnkeEdu.Tools
   9:  {
  10:      /// <summary>
  11:      /// 日期,时间处理
  12:      /// </summary>
  13:      public static class TimeHelper
  14:      {
  15:          /// <summary>
  16:          /// 返回当前时间的字符串表示 yyyy-MM-dd HH:mm:ss
  17:          /// </summary>
  18:          /// <returns>当前时间的字符串表示 yyyy-MM-dd HH:mm:ss</returns>
  19:          public static string GetNowStr()
  20:          {
  21:              return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  22:          }
  23:   
  24:          #region GetTimeFlag 根据时间标记返回时间字符表示形式(一天分为48段,0-47)
  25:          /// <summary>
  26:          /// 获到时间标记
  27:          /// </summary>
  28:          /// <param name="flag">时间标记对象</param>
  29:          /// <returns>返回时间字符表示</returns>
  30:          public static string GetTimeFlag(object flag)
  31:          {
  32:              if (flag == null) return "";
  33:              int nflag = Convert.ToInt32(flag.ToString());
  34:              return GetTimeFlag(nflag);
  35:          }
  36:          /// <summary>
  37:          /// 根据时间标记返回时间字符表示形式
  38:          /// 一天分为48段
  39:          /// 0:00-0:30 为0
  40:          /// </summary>
  41:          /// <param name="flag">时间标记</param>
  42:          /// <returns>返回标记后的时间字符串</returns>
  43:          public static string GetTimeFlag(int flag)
  44:          {
  45:              int hh = flag / 2;
  46:              int mm = ((flag % 2) != 0) ? 30 : 0;
  47:              DateTime d = new DateTime(1, 1, 1, hh, mm, 0);
  48:   
  49:              return d.ToString("HH:mm-") + d.AddMinutes(30).ToString("HH:mm");
  50:          }
  51:          #endregion
  52:          #region GetTimeFlagStartTime 根据时间标记返回时间段开始时间表示形式(一天分为48段,0-47)
  53:          /// <summary>
  54:          /// 根据时间标记返回时间段开始时间表示形式
  55:          /// 一天分为48段
  56:          /// 0:00-0:30 为0
  57:          /// </summary>
  58:          /// <param name="flag">时间标记</param>
  59:          /// <returns>返回标记后的时间</returns>
  60:          public static DateTime GetTimeFlagStartTime(int flag)
  61:          {
  62:              int hh = flag / 2;
  63:              int mm = ((flag % 2) != 0) ? 30 : 0;
  64:              DateTime d = new DateTime(1, 1, 1, hh, mm, 0);
  65:              return d;
  66:          }
  67:          #endregion
  68:   
  69:          #region GetCnDate 格式化日期为 yyyy-MM-dd 星期一
  70:          /// <summary>
  71:          /// 获取中午日期名 格式化日期为 yyyy-MM-dd 星期一
  72:          /// </summary>
  73:          /// <param name="dt">日期</param>
  74:          /// <returns>格式化的中午日期</returns>
  75:          public static string GetCnDate(DateTime dt)
  76:          {
  77:              return dt.ToString("yyyy-MM-dd") + " " + GetCnWeek(dt);
  78:          }
  79:          #endregion
  80:          #region GetMonthStr 转换月份为英文格式
  81:          /// <summary>
  82:          /// 转换月份为英文格式
  83:          /// </summary>
  84:          /// <param name="month">月份</param>
  85:          /// <returns>英文月份</returns>
  86:          public static string GetMonthStr(int month)
  87:          {
  88:              switch (month)
  89:              {
  90:                  case 1: return "January";
  91:                  case 2: return "February";
  92:                  case 3: return "March";
  93:                  case 4: return "April";
  94:                  case 5: return "May";
  95:                  case 6: return "June";
  96:                  case 7: return "July";
  97:                  case 8: return "August";
  98:                  case 9: return "September";
  99:                  case 10: return "October";
 100:                  case 11: return "November";
 101:                  case 12: return "December";
 102:                  default: return "January";
 103:              }
 104:   
 105:          }
 106:          #endregion
 107:          #region FormatDate 格式化日期为yyyy-MM-dd
 108:          /// <summary>
 109:          /// 格式化日期为2006-12-22
 110:          /// </summary>
 111:          /// <param name="dTime">日期</param>
 112:          /// <returns>返回格式化后的日期字符串</returns>
 113:          public static string FormatDate(DateTime? dTime)
 114:          {
 115:              string rStr;
 116:              rStr = dTime.Value.Year + "-" + dTime.Value.Month + "-" + dTime.Value.Day;
 117:              return rStr;
 118:          }
 119:          /// <summary>
 120:          /// 格式化日期为2006-12-22
 121:          /// </summary>
 122:          /// <param name="dTime">日期</param>
 123:          /// <returns>返回格式化后的日期字符串</returns>
 124:          public static string FormatDateYMD(DateTime? dTime)
 125:          {
 126:              if (dTime == null) return "1970-01-01";
 127:   
 128:              return dTime.Value.ToString("yyyy-MM-dd");
 129:          }
 130:          #endregion
 131:          #region GetCnWeek 获取中文星期名
 132:          /// <summary>
 133:          /// 获取中文星期名
 134:          /// </summary>
 135:          /// <param name="d">传入的整数</param>
 136:          /// <returns>返回中文星期名</returns>
 137:          public static string GetCnWeek(int d)
 138:          {
 139:              string rStr = "";
 140:              switch (d)
 141:              {
 142:                  case 7:
 143:                      rStr = "星期日";
 144:                      break;
 145:                  case 1:
 146:                      rStr = "星期一";
 147:                      break;
 148:                  case 2:
 149:                      rStr = "星期二";
 150:                      break;
 151:                  case 3:
 152:                      rStr = "星期三";
 153:                      break;
 154:                  case 4:
 155:                      rStr = "星期四";
 156:                      break;
 157:                  case 5:
 158:                      rStr = "星期五";
 159:                      break;
 160:                  case 6:
 161:                      rStr = "星期六";
 162:                      break;
 163:              }
 164:              return rStr;
 165:          }
 166:          #endregion
 167:          #region GetCnWeek 获取中文星期名
 168:          /// <summary>
 169:          /// 获取中文星期名
 170:          /// </summary>
 171:          /// <param name="sDate">日期</param>
 172:          /// <returns>返回对应的中文星期名</returns>
 173:          public static string GetCnWeek(DateTime sDate)
 174:          {
 175:              string rStr = "";
 176:              switch (sDate.DayOfWeek)
 177:              {
 178:                  case DayOfWeek.Sunday:
 179:                      rStr = "星期日";
 180:                      break;
 181:                  case DayOfWeek.Monday:
 182:                      rStr = "星期一";
 183:                      break;
 184:                  case DayOfWeek.Tuesday:
 185:                      rStr = "星期二";
 186:                      break;
 187:                  case DayOfWeek.Wednesday:
 188:                      rStr = "星期三";
 189:                      break;
 190:                  case DayOfWeek.Thursday:
 191:                      rStr = "星期四";
 192:                      break;
 193:                  case DayOfWeek.Friday:
 194:                      rStr = "星期五";
 195:                      break;
 196:                  case DayOfWeek.Saturday:
 197:                      rStr = "星期六";
 198:                      break;
 199:              }
 200:              return rStr;
 201:          }
 202:          #endregion
 203:          #region GetEngWeek 获取英文星期名
 204:          /// <summary>
 205:          /// 获取英文星期名
 206:          /// </summary>
 207:          /// <param name="sDate">日期</param>
 208:          /// <returns>返回星期名</returns>
 209:          public static string GetEngWeek(DateTime sDate)
 210:          {
 211:              string rStr = "";
 212:              switch (sDate.DayOfWeek)
 213:              {
 214:                  case DayOfWeek.Sunday:
 215:                      rStr = "Sunday";
 216:                      break;
 217:                  case DayOfWeek.Monday:
 218:                      rStr = "Monday";
 219:                      break;
 220:                  case DayOfWeek.Tuesday:
 221:                      rStr = "Tuesday";
 222:                      break;
 223:                  case DayOfWeek.Wednesday:
 224:                      rStr = "Wednesday";
 225:                      break;
 226:                  case DayOfWeek.Thursday:
 227:                      rStr = "Thursday";
 228:                      break;
 229:                  case DayOfWeek.Friday:
 230:                      rStr = "Friday";
 231:                      break;
 232:                  case DayOfWeek.Saturday:
 233:                      rStr = "Saturday";
 234:                      break;
 235:              }
 236:              return rStr;
 237:          }
 238:          #endregion
 239:          #region GetCnWeek 获取DayOfWeek
 240:          /// <summary>
 241:          /// 获取DayOfWeek
 242:          /// </summary>
 243:          /// <param name="sDate">日期</param>
 244:          /// <returns>返回星期名称对应的整数</returns>
 245:          public static int GetDayOfWeek(DateTime sDate)
 246:          {
 247:              int dayOfWeek = 0;
 248:              switch (sDate.DayOfWeek)
 249:              {
 250:                  case DayOfWeek.Sunday:
 251:                      dayOfWeek = 0;
 252:                      break;
 253:                  case DayOfWeek.Monday:
 254:                      dayOfWeek = 1;
 255:                      break;
 256:                  case DayOfWeek.Tuesday:
 257:                      dayOfWeek = 2;
 258:                      break;
 259:                  case DayOfWeek.Wednesday:
 260:                      dayOfWeek = 3;
 261:                      break;
 262:                  case DayOfWeek.Thursday:
 263:                      dayOfWeek = 4;
 264:                      break;
 265:                  case DayOfWeek.Friday:
 266:                      dayOfWeek = 5;
 267:                      break;
 268:                  case DayOfWeek.Saturday:
 269:                      dayOfWeek = 6;
 270:                      break;
 271:              }
 272:              return dayOfWeek;
 273:          }
 274:          #endregion
 275:          #region GetDataTimeStr 获取字符串格式的日期时间 yyyy-MM-dd HH:mm:ss
 276:          /// <summary>
 277:          /// 获取字符串格式的日期时间 yyyy-MM-dd HH:mm:ss
 278:          /// </summary>
 279:          /// <param name="dateTime">要格式的时间</param>
 280:          /// <returns>返回格式化后的时间字符串</returns>
 281:          public static string GetDataTimeStr(DateTime dateTime)
 282:          {
 283:              return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
 284:          }
 285:          #endregion
 286:          #region GetDataStr 获取字符串格式的日期 yyyy-MM-dd
 287:          /// <summary>
 288:          /// 获取字符串格式的日期 yyyy-MM-dd
 289:          /// </summary>
 290:          /// <param name="dateTime">要格式的日期</param>
 291:          /// <returns>返回格式化后的时间字符串</returns>
 292:          public static string GetDataStr(DateTime dateTime)
 293:          {
 294:              return dateTime.ToString("yyyy-MM-dd");
 295:          }
 296:          #endregion
 297:          #region GetDateFormString 从一个字符串类型配合日期模板获取表示的日期
 298:          /// <summary>
 299:          /// 系统空值时间
 300:          /// </summary>
 301:          static public readonly DateTime SYS_NULL_TIME = new DateTime(1000, 1, 1);
 302:          /// <summary>
 303:          /// 从一个字符串类型配合日期模板获取表示的日期
 304:          /// </summary>
 305:          /// <param name="strDate">字符串表示的日期</param>
 306:          /// <param name="dateFormat">日期格式</param>        
 307:          /// <returns>返回的日期(如果异常返回默认值)</returns>
 308:          public static DateTime GetDateFormString(string strDate, string dateFormat)
 309:          {
 310:              if (string.IsNullOrEmpty(strDate))
 311:              {
 312:                  return SYS_NULL_TIME;
 313:              }
 314:   
 315:              try
 316:              {
 317:                  int year = GetDateIntFormString(dateFormat, strDate, ‘y‘);
 318:                  int month = GetDateIntFormString(dateFormat, strDate, ‘M‘);
 319:                  int day = GetDateIntFormString(dateFormat, strDate, ‘d‘);
 320:   
 321:                  return new DateTime(year, month, day);
 322:              }
 323:              catch
 324:              {
 325:                  return new DateTime();
 326:              }
 327:          }
 328:          /// <summary>
 329:          /// 从日期中获取日期
 330:          /// </summary>
 331:          /// <param name="dateForamt">时间格式</param>
 332:          /// <param name="dateStr">字符串日期</param>
 333:          /// <param name="ch">字符串类型:‘y’,‘m‘,‘d‘</param>
 334:          /// <returns>返回格式化后的整数</returns>
 335:          private static int GetDateIntFormString(string dateForamt, string dateStr, char ch)
 336:          {
 337:              //模板可能是变动的,从模板根据最后的ch值确定表示时间的区间
 338:              int indStart = dateForamt.IndexOf(ch);
 339:              int indEnd = dateForamt.LastIndexOf(ch);
 340:              if (dateStr.Length < indStart + 1)
 341:              {
 342:                  return 1;
 343:              }
 344:   
 345:              string tmpStr = "";
 346:   
 347:              if (dateStr.Length < indEnd + 1)
 348:              {
 349:                  tmpStr = dateStr.Substring(indStart);
 350:              }
 351:              else
 352:              {
 353:                  tmpStr = dateStr.Substring(indStart, indEnd - indStart + 1);
 354:              }
 355:   
 356:              return int.Parse(tmpStr);
 357:          }
 358:          #endregion
 359:   
 360:          #region ConvertDateStrToDateTime 转换一个字符串(yyyy-MM-dd)为时间格式
 361:          /// <summary>
 362:          /// 转换一个字符串(yyyy-MM-dd)为时间格式
 363:          /// </summary>
 364:          /// <param name="yyyy_MM_dd">年月日,应为yyyy-MM-dd</param>
 365:          /// <returns>返回转换后的时间格式</returns>
 366:          public static DateTime ConvertDateStrToDateTime(string yyyy_MM_dd)
 367:          {
 368:              return ConvertDateTimeStrToDateTime(yyyy_MM_dd, "00:00:00");
 369:          }
 370:          #endregion
 371:          #region ConvertDateTimeStrToDateTime 转换一个字符串(yyyy-MM-dd HH:mm:ss)为时间格式
 372:          /// <summary>
 373:          /// 转换一个字符串(yyyy-MM-dd HH:mm:ss)为时间格式
 374:          /// </summary>
 375:          /// <param name="yyyy_MM_dd">年月日,应为yyyy-MM-dd</param>
 376:          /// <param name="hh_mm_ss">时分秒,应为hh:mm:ss</param>
 377:          /// <returns>返回转换后的时间格式</returns>
 378:          public static DateTime ConvertDateTimeStrToDateTime(string yyyy_MM_dd, string hh_mm_ss)
 379:          {
 380:              if (yyyy_MM_dd.Length != 10 || hh_mm_ss.Length != 8)
 381:              {
 382:                  return new DateTime();
 383:              }
 384:              string[] datestr = yyyy_MM_dd.Split(‘-‘);
 385:              string[] timestr = hh_mm_ss.Split(‘:‘);
 386:   
 387:              return new DateTime(Int32.Parse(datestr[0]),
 388:                  Int32.Parse(datestr[1]),
 389:                  Int32.Parse(datestr[2]),
 390:                  Int32.Parse(timestr[0]),
 391:                  Int32.Parse(timestr[1]),
 392:                  Int32.Parse(timestr[2])
 393:                  );
 394:          }
 395:          #endregion
 396:   
 397:          #region FormatTimeByMillisecond 格式化毫秒时间长度为友好时间长度
 398:          /// <summary>
 399:          /// 格式化毫秒时间长度为友好时间长度
 400:          /// </summary>
 401:          /// <param name="pMillisecond">要格式化的毫秒</param>
 402:          /// <returns>天,小时,分,秒等</returns>
 403:          public static string FormatTimeByMillisecond(long pMillisecond)
 404:          {
 405:              StringBuilder builder = new StringBuilder();
 406:              pMillisecond = int.Parse(string.Format("{0:F0}", pMillisecond / 0x3e8L));
 407:              if (pMillisecond >= 0x15180L)
 408:              {
 409:                  builder.Append(string.Format("{0:F0}", pMillisecond / 0x15180L));
 410:                  builder.Append("天");
 411:                  pMillisecond = pMillisecond % 0x15180L;
 412:              }
 413:              if ((pMillisecond > 0xe10L) || (pMillisecond == 0L))
 414:              {
 415:                  builder.Append(string.Format("{0:F0}", pMillisecond / 0xe10L));
 416:                  builder.Append("小时");
 417:                  pMillisecond = pMillisecond % 0xe10L;
 418:              }
 419:              if ((pMillisecond > 60L) || (pMillisecond == 0L))
 420:              {
 421:                  builder.Append(string.Format("{0:F0}", pMillisecond / 60L));
 422:                  builder.Append("分");
 423:                  pMillisecond = pMillisecond % 60L;
 424:              }
 425:              if (pMillisecond < 60L)
 426:              {
 427:                  builder.Append(pMillisecond.ToString());
 428:                  builder.Append("秒");
 429:              }
 430:              return builder.ToString();
 431:          }
 432:          #endregion
 433:   
 434:          #region GetTwoDateSeconds 获得d1和d2之间的时间秒数
 435:          /// <summary>
 436:          /// 获得d1和d2之间的时间秒数
 437:          /// </summary>
 438:          /// <param name="d1">时间前</param>
 439:          /// <param name="d2">时间后</param>
 440:          /// <returns>返回d1和d2之间的时间秒数</returns>
 441:          public static long GetTwoDateSeconds(DateTime d1, DateTime d2)
 442:          {
 443:              TimeSpan ts = d2.Subtract(d1).Duration();
 444:              return Convert.ToInt64(ts.TotalSeconds);
 445:          }
 446:          #endregion
 447:   
 448:          
 449:   
 450:      }
 451:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

日期转换类

时间: 2024-10-11 07:52:24

日期转换类的相关文章

日期转换类 DateConverter.java

package com.util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter; /** * 日期转换类 * */ public class DateConverter extends De

PHP日期操作类代码-农历-阳历转换、闰年、计算天数等

这是一个实用的PHP日期时间操作类,里面包括了公历-农历转换.转换成中文日期格式.计算农历相隔天数.根据阴历年获取生肖.获取阴历月份的天数.获取农历每年的天数.获取闰月.计算阴历日期与正月初一相隔的天数.计算2个公历(阳历)日期之间的天数.根据距离正月初一的天数计算阴历日期.获取天干地支纪年等,PHP日期操作类:Lunar.class.php代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

java笔记--String类格式化当天日期转换符文档

String类格式化当天日期 转换符:%tb : 格式化为月份的英文缩写%tB : 格式化为月份的英文全写%ta : 格式化为星期%tA : 格式化为星期%ty : 格式化为两位年份值%tY : 格式化为四位年份值%tm : 格式化为两位月份值%td : 格式化为两位日期值%te : 格式化为星期一位日期值%tH : 格式化为两位24时制小时值(00-23)%tk : 格式化为两位24时制小时值(0-23)%tI : 格式化为两位12时制小时值(01-12)%tl : 格式化为两位12时制小时值

dateUtil 日期工具类

package com.lbi.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.slf4j.Logg

atitit.设计模式(1)--—职责链模式(chain of responsibility)最佳实践O7 日期转换

atitit.设计模式(1)---职责链模式(chain of responsibility)最佳实践O7 日期转换 1. 需求:::日期转换 1 2. 可以选择的模式: 表格模式,责任链模式 1 3. 调用代码 2 4. 责任链链的特性: 2 5. 模式结构 4 6. 职责链模式包含如下角色:Handler,ConcreteHandler: 具体处理者,HandlerChainUtil ,Client 4 7. 设置哈一个handler,,两个法:排序法,指定法 5 1. 指定法 5 2. 排

springMvc的日期转换之二

方式一:使用@InitBinder注解实现日期转换 前台页面: 后台打印: 方式二:处理多种日期格式类型之间的转换 采用方式:由于binder.registerCustomEditor(Date.class,需要传一个PropertiesEditor类型的参数); 所以我们可以自定义一个日期格式,继承PropertiesEditor类 package cn.happy.controller; import java.sql.Date; import java.text.ParseExceptio

Java日期格式化之将String类型的GMT,GST日期转换成Date类型

请尊重他人的劳动成果,转载请注明出处:Java日期格式化之将String类型的GMT,GST日期转换成Date类型 http://blog.csdn.net/fengyuzhengfan/article/details/40164721 在实际开发过程中经常会需要将Date类型的数据封装成XML或Json格式在网络上进行传输,另外在将Date类型的数据存到Sqlite数据库中后再取出来的时候只能获取String类型的日期了,这是因为SQLite是无类型的.这样不得不面对将String 类型的日期

JavaScriptSerializer的日期转换方案

1.转换后过滤替换(通用) /// <summary> /// 日期转换 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string JsonDateTime(string str) {    str = Regex.Replace(str, @"\\/Date\((\d+)\)\\/

Struts2中Date日期转换的问题

今天跑程序的时候莫名其妙的出现了下面的一个异常: java.lang.NoSuchMethodException:com.ca.agent.model.mybatis.ApprovalInforCangra.setSubDate([Ljava.lang.String;) 这类异常信息在以前是处理过的,按照以前的思路在eclipse的调试模式下看看具体的情况,不过这次比较奇怪,根本没进入到对应的Action类中就抛出了此异常信息,进一步查找发现,在调用拦截器方法的时候就抛出了此异常信息,还有没有想