Quartz.Net系列(十三):DateBuilder中的API详解
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详解的更多相关文章
- VB中的API详解
一.API是什么? 这个我本来不想说的,不过也许你知道其它人不知道,这里为了照顾一下新手,不得不说些废话,请大家谅解. Win32 API即为Microsoft 32位平台的应用程序编程接口(Appl ...
- netty系列之:netty中的Channel详解
目录 简介 Channel详解 异步IO和ChannelFuture Channel的层级结构 释放资源 事件处理 总结 简介 Channel是连接ByteBuf和Event的桥梁,netty中的Ch ...
- netty系列之:netty中的ByteBuf详解
目录 简介 ByteBuf详解 创建一个Buff 随机访问Buff 序列读写 搜索 其他衍生buffer方法 和现有JDK类型的转换 总结 简介 netty中用于进行信息承载和交流的类叫做ByteBu ...
- Quartz.Net系列(十四):详解Job中两大特性(DisallowConcurrentExecution、PersistJobDataAfterExecution)
1.DisallowConcurrentExceution 从字面意思来看也就是不允许并发执行 简单的演示一下 [DisallowConcurrentExecution] public class T ...
- 转:VB中的API详解
在接下来的这篇文章中,我将向大家介绍.NET中的线程API,怎么样用C#创建线程,启动和停止线程,设置优先级和状态. 在.NET中编写的程序将被自动的分配一个线程.让我们来看看用C#编程语言创建线程并 ...
- ElasticSearch 中 REST API 详解
本文主要内容: 1 ElasticSearch常用的操作 2 ElasticSearchbulk命令 ES REST API elasticsearch支持多种通讯,其中包括http请求响应服务,因此 ...
- 【SignalR学习系列】6. SignalR Hubs Api 详解(C# Server 端)
如何注册 SignalR 中间件 为了让客户端能够连接到 Hub ,当程序启动的时候你需要调用 MapSignalR 方法. 下面代码显示了如何在 OWIN startup 类里面定义 SignalR ...
- 【SignalR学习系列】8. SignalR Hubs Api 详解(.Net C# 客户端)
建立一个 SignalR 连接 var hubConnection = new HubConnection("http://www.contoso.com/"); IHubProx ...
- 【SignalR学习系列】7. SignalR Hubs Api 详解(JavaScript 客户端)
SignalR 的 generated proxy 服务端 public class ContosoChatHub : Hub { public void NewContosoChatMessage( ...
随机推荐
- .NETCore微服务探寻(一) - 网关
前言 一直以来对于.NETCore微服务相关的技术栈都处于一个浅尝辄止的了解阶段,在现实工作中也对于微服务也一直没有使用的业务环境,所以一直也没有整合过一个完整的基于.NETCore技术栈的微服务项目 ...
- 使用SSH远程管理时本地文件被修改了
背景: 有两个网段:1段作为工作网段即员工办公用:2段作为专用网段配置了一系列需要的环境. 在Ubuntu 16.04用Python的SSH工具在对这两个网段远程管理,我写了一个检测环境的脚本,用SF ...
- 001.OpenShift介绍
一 OpenShift特性 1.1 OpenShift概述 Red Hat OpenShijft Container Platform (OpenShift)是一个容器应用程序平台,它为开发人员和IT ...
- 7.kubernetes集群版本升级
1.查看原集群的Node节点的版本号 [root@hdss7-22 opt]# kubectl get node -o wide 2.将要升级的kubernetes版本上传到node节点上并解压(v1 ...
- Linux上TCP的几个内核参数调优
Linux作为一个强大的操作系统,提供了一系列内核参数供我们进行调优.光TCP的调优参数就有50多个.在和线上问题斗智斗勇的过程中,笔者积累了一些在内网环境应该进行调优的参数.在此分享出来,希望对大家 ...
- js中.bind()和.call()用法讲解
var option = { ti : 8, it(){ return this.ti; } } 这里又一个option对象,it()方法里的this指的就是option对象的上下文. console ...
- SpringCloud 入门(一)
本人也是刚接触springcloud,现在先将创建项目的过程记录下来,springcloud的理解日后再慢慢补齐,最好还是自己参考官方文档的介绍 使用工具:IDEA IDEA创建springcloud ...
- ArrayList、Vector、LinkedList 区别及底层实现
一.ArrayList.Vector.LinkedList 三者区别 1.ArrayList 底层:是数组结构,查询快,增删慢,线程不安全,效率高.2.LinkedList底层:是链表数据结构,查询 ...
- android自定义控件onMeasure方法
1.自定义控件首先定义一个类继承View 有时,Android系统控件无法满足我们的需求,因此有必要自定义View.具体方法参见官方开发文档:http://developer.android.com/ ...
- SpringBoot——项目启动时读取配置及初始化资源
介绍 在开发过程中,我们有时候会遇到非接口调用而出发程序执行任务的一些场景,比如我们使用quartz定时框架通过配置文件来启动定时任务时,或者一些初始化资源场景等触发的任务执行场景. 方法一:注解 ...