1.DateOf、ToDayAt、TomorrowAt

DateOf:指定年月日时分秒

        public static DateTimeOffset DateOf(int hour, int minute, int second)
{
ValidateSecond(second);
ValidateMinute(minute);
ValidateHour(hour); DateTimeOffset c = SystemTime.Now();
DateTime dt = new DateTime(c.Year, c.Month, c.Day, hour, minute, second);
return new DateTimeOffset(dt, TimeZoneUtil.GetUtcOffset(dt, TimeZoneInfo.Local));
} public static DateTimeOffset DateOf(int hour, int minute, int second,
int dayOfMonth, int month)
{
ValidateSecond(second);
ValidateMinute(minute);
ValidateHour(hour);
ValidateDayOfMonth(dayOfMonth);
ValidateMonth(month); DateTimeOffset c = SystemTime.Now();
DateTime dt = new DateTime(c.Year, month, dayOfMonth, hour, minute, second);
return new DateTimeOffset(dt, TimeZoneUtil.GetUtcOffset(dt, TimeZoneInfo.Local));
} public static DateTimeOffset DateOf(int hour, int minute, int second,
int dayOfMonth, int month, int year)
{
ValidateSecond(second);
ValidateMinute(minute);
ValidateHour(hour);
ValidateDayOfMonth(dayOfMonth);
ValidateMonth(month);
ValidateYear(year); DateTime dt = new DateTime(year, month, dayOfMonth, hour, minute, second);
return new DateTimeOffset(dt, TimeZoneUtil.GetUtcOffset(dt, TimeZoneInfo.Local));
}

TodayAt:DateOf的一个封装

        public static DateTimeOffset TodayAt(int hour, int minute, int second)
{
return DateOf(hour, minute, second);
}

TomorrowAt:AddDays的一次操作

        public static DateTimeOffset TomorrowAt(int hour, int minute, int second)
{
ValidateSecond(second);
ValidateMinute(minute);
ValidateHour(hour); DateTimeOffset now = DateTimeOffset.Now;
DateTimeOffset c = new DateTimeOffset(
now.Year,
now.Month,
now.Day,
hour,
minute,
second,
,
now.Offset); // advance one day
c = c.AddDays(); return c;
}

2.EvenHourDate、EvenHourDateAfterNow、EvenHourDateBefore

小时四舍五入操作

EvenHourDate:AddHours(1)操作,分、秒抹零操作 比如8:12变成9:00

        /// <summary>
/// Returns a date that is rounded to the next even hour above the given date.
/// </summary>
/// <remarks>
/// For example an input date with a time of 08:13:54 would result in a date
/// with the time of 09:00:00. If the date's time is in the 23rd hour, the
/// date's 'day' will be promoted, and the time will be set to 00:00:00.
/// </remarks>
/// <param name="date">the Date to round, if <see langword="null" /> the current time will
/// be used</param>
/// <returns>the new rounded date</returns>
public static DateTimeOffset EvenHourDate(DateTimeOffset? date)
{
if (!date.HasValue)
{
date = SystemTime.Now();
}
DateTimeOffset d = date.Value.AddHours();
return new DateTimeOffset(d.Year, d.Month, d.Day, d.Hour, , , d.Offset);
}

EvenHourDateAfterNow:当前时间加一小时,EvenHourDate(null)的封装

EvenHourDateBefore:分秒抹零操作

        /// <summary>
/// Returns a date that is rounded to the previous even hour below the given date.
/// </summary>
/// <remarks>
/// For example an input date with a time of 08:13:54 would result in a date
/// with the time of 08:00:00.
/// </remarks>
/// <param name="date">the Date to round, if <see langword="null" /> the current time will
/// be used</param>
/// <returns>the new rounded date</returns>
public static DateTimeOffset EvenHourDateBefore(DateTimeOffset? date)
{
if (!date.HasValue)
{
date = SystemTime.Now();
}
return new DateTimeOffset(date.Value.Year, date.Value.Month, date.Value.Day, date.Value.Hour, , , date.Value.Offset);
}

3.EvenMinuteDate、EvenMinuteDateAfterNow、EvenMinuteDateBefore

分钟操作,和前面的小时操作方法差不多

EvenMinuteDate:AddMinutes(1)操作

EvenMinuteDateAfterNow:DateTimeOffset.Now.AddMinutes(1)操作

EvenMinuteDateBefore:分钟抹零操作

4.EvenSecondDate、EvenSecondDateAfterNow、EvenSecondDateBefore

分钟操作,和前面的小时和分钟操作方法差不多

EvenSecondDate:AddSeconds(1)操作

EvenSecondDateAfterNow:DateTimeOffset.Now.AddSeconds(1)操作

EvenSecondDateBefore:秒钟抹零操作

5.NextGivenMinuteDate、NextGivenSecondDate

返回一个日期,该日期四舍五入到给定分秒的下一个偶数倍。

NextGivenMinuteDate

        public static DateTimeOffset NextGivenMinuteDate(DateTimeOffset? date, int minuteBase)
{
if (minuteBase < || minuteBase > )
{
throw new ArgumentException("minuteBase must be >=0 and <= 59");
} DateTimeOffset c = date ?? SystemTime.Now(); if (minuteBase == )
{
return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, , , , c.Offset).AddHours();
} int minute = c.Minute; int arItr = minute/minuteBase; int nextMinuteOccurance = minuteBase*(arItr + ); if (nextMinuteOccurance < )
{
return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, nextMinuteOccurance, , , c.Offset);
}
return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, , , , c.Offset).AddHours();
}

NextGivenSecondDate

        public static DateTimeOffset NextGivenSecondDate(DateTimeOffset? date, int secondBase)
{
if (secondBase < || secondBase > )
{
throw new ArgumentException("secondBase must be >=0 and <= 59");
} DateTimeOffset c = date ?? SystemTime.Now(); if (secondBase == )
{
return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, c.Minute, , , c.Offset).AddMinutes();
} int second = c.Second; int arItr = second/secondBase; int nextSecondOccurance = secondBase*(arItr + ); if (nextSecondOccurance < )
{
return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, c.Minute, nextSecondOccurance, , c.Offset);
}
return new DateTimeOffset(c.Year, c.Month, c.Day, c.Hour, c.Minute, , , c.Offset).AddMinutes();
}

6.FutureDate

指定年、月、周、日、时、分、秒加一操作

        public static DateTimeOffset FutureDate(int interval, IntervalUnit unit)
{
return TranslatedAdd(SystemTime.Now(), unit, interval);
}
private static DateTimeOffset TranslatedAdd(DateTimeOffset date, IntervalUnit unit, int amountToAdd)
{
switch (unit)
{
case IntervalUnit.Day:
return date.AddDays(amountToAdd);
case IntervalUnit.Hour:
return date.AddHours(amountToAdd);
case IntervalUnit.Minute:
return date.AddMinutes(amountToAdd);
case IntervalUnit.Month:
return date.AddMonths(amountToAdd);
case IntervalUnit.Second:
return date.AddSeconds(amountToAdd);
case IntervalUnit.Millisecond:
return date.AddMilliseconds(amountToAdd);
case IntervalUnit.Week:
return date.AddDays(amountToAdd*);
case IntervalUnit.Year:
return date.AddYears(amountToAdd);
default:
throw new ArgumentException("Unknown IntervalUnit");
}
}

7.NewDate、NewDateInTimeZone、InMonth、InYear、InTimeZone、InMonthOnDay、AtMinute、AtSecond、AtHourMinuteAndSecond、OnDay

实例化DateBuilder指定年月日时分秒时区

        public static DateBuilder NewDate()
{
return new DateBuilder();
} /// <summary>
/// Create a DateBuilder, with initial settings for the current date and time in the given timezone.
/// </summary>
/// <param name="tz">Time zone to use.</param>
/// <returns></returns>
public static DateBuilder NewDateInTimeZone(TimeZoneInfo tz)
{
return new DateBuilder(tz);
}
    public class DateBuilder
{
private int month;
private int day;
private int year;
private int hour;
private int minute;
private int second;
private TimeZoneInfo tz; /// <summary>
/// Create a DateBuilder, with initial settings for the current date
/// and time in the system default timezone.
/// </summary>
private DateBuilder()
{
DateTime now = DateTime.Now; month = now.Month;
day = now.Day;
year = now.Year;
hour = now.Hour;
minute = now.Minute;
second = now.Second;
} /// <summary>
/// Create a DateBuilder, with initial settings for the current date and time in the given timezone.
/// </summary>
/// <param name="tz"></param>
private DateBuilder(TimeZoneInfo tz)
{
DateTime now = DateTime.Now; month = now.Month;
day = now.Day;
year = now.Year;
hour = now.Hour;
minute = now.Minute;
second = now.Second; this.tz = tz;
}
}

8.ValidateYear、ValidateMonth、ValidateDay、ValidateHour、ValidateMinute、ValidateSecond

验证年月日时分秒

        public static void ValidateHour(int hour)
{
if (hour < || hour > )
{
throw new ArgumentException("Invalid hour (must be >= 0 and <= 23).");
}
} public static void ValidateMinute(int minute)
{
if (minute < || minute > )
{
throw new ArgumentException("Invalid minute (must be >= 0 and <= 59).");
}
} public static void ValidateSecond(int second)
{
if (second < || second > )
{
throw new ArgumentException("Invalid second (must be >= 0 and <= 59).");
}
} public static void ValidateDayOfMonth(int day)
{
if (day < || day > )
{
throw new ArgumentException("Invalid day of month.");
}
} public static void ValidateMonth(int month)
{
if (month < || month > )
{
throw new ArgumentException("Invalid month (must be >= 1 and <= 12).");
}
} public static void ValidateYear(int year)
{
if (year < || year > )
{
throw new ArgumentException("Invalid year (must be >= 1970 and <= 2099).");
}
}

9.Build

生成DateTimeOffset

        public DateTimeOffset Build()
{
DateTime dt = new DateTime(year, month, day, hour, minute, second);
TimeSpan offset = TimeZoneUtil.GetUtcOffset(dt, tz ?? TimeZoneInfo.Local);
return new DateTimeOffset(dt, offset);
}

Quartz.Net系列(十三):DateBuilder中的API详解的更多相关文章

  1. VB中的API详解

    一.API是什么? 这个我本来不想说的,不过也许你知道其它人不知道,这里为了照顾一下新手,不得不说些废话,请大家谅解. Win32 API即为Microsoft 32位平台的应用程序编程接口(Appl ...

  2. netty系列之:netty中的Channel详解

    目录 简介 Channel详解 异步IO和ChannelFuture Channel的层级结构 释放资源 事件处理 总结 简介 Channel是连接ByteBuf和Event的桥梁,netty中的Ch ...

  3. netty系列之:netty中的ByteBuf详解

    目录 简介 ByteBuf详解 创建一个Buff 随机访问Buff 序列读写 搜索 其他衍生buffer方法 和现有JDK类型的转换 总结 简介 netty中用于进行信息承载和交流的类叫做ByteBu ...

  4. Quartz.Net系列(十四):详解Job中两大特性(DisallowConcurrentExecution、PersistJobDataAfterExecution)

    1.DisallowConcurrentExceution 从字面意思来看也就是不允许并发执行 简单的演示一下 [DisallowConcurrentExecution] public class T ...

  5. 转:VB中的API详解

    在接下来的这篇文章中,我将向大家介绍.NET中的线程API,怎么样用C#创建线程,启动和停止线程,设置优先级和状态. 在.NET中编写的程序将被自动的分配一个线程.让我们来看看用C#编程语言创建线程并 ...

  6. ElasticSearch 中 REST API 详解

    本文主要内容: 1 ElasticSearch常用的操作 2 ElasticSearchbulk命令 ES REST API elasticsearch支持多种通讯,其中包括http请求响应服务,因此 ...

  7. 【SignalR学习系列】6. SignalR Hubs Api 详解(C# Server 端)

    如何注册 SignalR 中间件 为了让客户端能够连接到 Hub ,当程序启动的时候你需要调用 MapSignalR 方法. 下面代码显示了如何在 OWIN startup 类里面定义 SignalR ...

  8. 【SignalR学习系列】8. SignalR Hubs Api 详解(.Net C# 客户端)

    建立一个 SignalR 连接 var hubConnection = new HubConnection("http://www.contoso.com/"); IHubProx ...

  9. 【SignalR学习系列】7. SignalR Hubs Api 详解(JavaScript 客户端)

    SignalR 的 generated proxy 服务端 public class ContosoChatHub : Hub { public void NewContosoChatMessage( ...

随机推荐

  1. salesforce零基础学习(九十八)Type浅谈

    在Salesforce的世界,凡事皆Metadata. 先通过一句经常使用的代码带入一下: Account accountItem = (Account)JSON.deserialize(accoun ...

  2. PN532模块连接-读卡失败原因

    第一步:点击发现NFC设备 第二步:点击读整卡:读取卡片内容. 若不成功,把UID卡移开,再放一次.再点第一步,显示发现NFC,再点第二步.反复操作,直到读取到为止.2-3次一般都会成功 . 相关软件 ...

  3. 找到了两个联想的OEM XP镜像文件

    今天在收拾移动硬盘的时候发现了两个XP镜像 还都是联想的,一个有OOBE,另一个无OOBE,全传网盘里了,需要的自取 有个疑问 2020年还有多少家庭电脑和ATM机器还在用XP??? link:htt ...

  4. 用OpenPyXL处理Excel表格 - 向sheet读取、写入数据

    假设一个名叫"模板"的excel表格里有四个sheet,名字分别是['平台', '制冷', '洗衣机', '空调'] 1.读取 from openpyxl import load_ ...

  5. 初步了解Windows7下部署Sonar

    1.准备工具: (1)Sonar 8.3版本. (2)PostgresSql 11版本. (3)Java 11. 详细获取地址可参考文章https://www.pianshen.com/article ...

  6. WeChair项目Alpha冲刺(9/10)

    团队项目进行情况 1.昨日进展    Alpha冲刺第九天 昨日进展: 前端:安排页面美化,设计实名认证 后端:更新dao层代码 数据库:修改数据表属性,与后端部署数据库交互 2.今日安排 前端:继续 ...

  7. MongoDB设计方法及技巧

    MongoDB是一种流行的数据库,可以在不受任何表格schema模式的约束下工作.数据以类似JSON的格式存储,并且可以包含不同类型的数据结构.例如,在同一集合collection 中,我们可以拥有以 ...

  8. 一分钟开始持续集成之旅系列之:C 语言 + Makefile

    作者:CODING - 朱增辉 前言 make 工具非常强大,配合 makefile 文件可以实现软件的自动化构建,但是执行 make 命令依然需要经历手动输入执行.等待编译完成.将目标文件转移到合适 ...

  9. springMvc接口开发--对访问的restful api接口进行拦截实现功能扩展

    1.视频参加Spring Security开发安全的REST服务\PART1\PART1 3-7 使用切片拦截REST服务三通it学院-www.santongit.com-.mp4 讲的比较的经典,后 ...

  10. 7-3 树的同构(25 分) JAVA

    给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的. 例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树 ...