概述
官方API:
http://www.quartz-scheduler.org/api/2.2.1/index.html
示例
todayAt / tomorrowAt 今天/明天的某个时间
Date org.quartz.DateBuilder.todayAt(int hour, int minute, int second) Get a Date object that represents the given time, on today's date (equivalent to dateOf(int, int, int)). Parameters: second The value (0-59) to give the seconds field of the date minute The value (0-59) to give the minutes field of the date hour The value (0-23) to give the hours field of the date Returns: the new date
例子
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // todayAt Date runTime = DateBuilder.todayAt(17, 30, 45); System.out.println("当前时间:" + sdf.format(new Date())); System.out.println("runTime:" + sdf.format(runTime));
输出
当前时间:2017-11-16 00:39:06 runTime:2017-11-16 17:30:45
对应的方法有
- todayAt:今天的某个时间
- tomorrowAt:明天的某个时间
evenMinuteDate 返回下一分钟的开始时间
// evenMinuteDate runTime = DateBuilder.evenMinuteDate(new Date()); System.out.println("当前时间:" + sdf.format(new Date())); System.out.println("runTime:" + sdf.format(runTime));
输出
当前时间:2017-11-16 00:40:02 runTime:2017-11-16 00:41:00
对应的方法有
- evenSecondDate(new Date()):下一秒
- evenMinuteDate(new Date()):下一分钟
- evenHourDate(new Date()):下一小时
- evenSecondDateAfterNow():当前时间的下一秒
- evenMinuteDateAfterNow():当前时间的下一分钟
- evenHourDateAfterNow():当前时间的下一小时
evenMinuteDateBefore 获取开始时间
// evenMinuteDateBefore runTime = DateBuilder.evenMinuteDateBefore(new Date()); System.out.println("当前时间:" + sdf.format(new Date())); System.out.println("runTime:" + sdf.format(runTime));
输出
当前时间:2017-11-16 00:44:06 runTime:2017-11-16 00:44:00
对应的方法有
- evenSecondDateBefore(new Date()):秒的开始执行时间,例如:2017-11-16 15:07:55
- evenMinuteDateBefore(new Date()):分钟的开始执行时间,例如:2017-11-16 15:41:00
- evenHourDateBefore(new Date()):小时的开始执行时间,例如:2017-11-16 15:00:00
dateOf 指定日期
// dateof Date dateOf_1 = DateBuilder.dateOf(13, 10, 10); Date dateOf_2 = DateBuilder.dateOf(13, 10, 10, 1, 1); Date dateOf_3 = DateBuilder.dateOf(13, 10, 10, 1, 1, 2016); System.out.println("当前时间:" + sdf.format(new Date())); System.out.println("dateOf_1:" + sdf.format(dateOf_1)); System.out.println("dateOf_2:" + sdf.format(dateOf_2)); System.out.println("dateOf_3:" + sdf.format(dateOf_3));
输出
当前时间:2017-11-16 00:45:39 dateOf_1:2017-11-16 13:10:10 dateOf_2:2017-01-01 13:10:10 dateOf_3:2016-01-01 13:10:10
nextGivenMinuteDate 下一个给定分钟日期
// nextGivenMinuteDate runTime = DateBuilder.nextGivenMinuteDate(new Date(), 1); System.out.println("当前时间:" + sdf.format(new Date())); System.out.println("runTime:" + sdf.format(runTime));
输出
当前时间:2017-11-16 00:47:31 runTime:2017-11-16 00:50:00
对应的方法有
- nextGivenSecondDate:下一个给定秒的时间
- nextGivenMinuteDate:下一个给定分钟的时间
translateTime 转换时间
translateTime方法有三个参数(date,src,dest),返回转换后的时间
- date:需要转换的时间
- src:原始时区
- dest:目标时区
// 转换时区 “美国/洛杉矶” runTime = DateBuilder.translateTime(new Date(), TimeZone.getDefault(), TimeZone.getTimeZone("America/Los_Angeles")); System.out.println("当前时间:" + sdf.format(new Date())); System.out.println("runTime:" + sdf.format(runTime));
输出
当前时间:2017-11-16 00:51:56 runTime:2017-11-16 04:51:56
人在南美洲玻利维亚…,如果是国内的话,肯定不是这个输出了。
futureDate 未来时间
futureDate(int interval,DateBuilder.IntervalUnit unit)
- interval:间隔
- unit:间隔单位,毫秒、秒、分钟、小时、天、周、月、年
// futureDate runTime = DateBuilder.futureDate(10, DateBuilder.IntervalUnit.MINUTE); System.out.println("当前时间:" + sdf.format(new Date())); System.out.println("runTime:" + sdf.format(runTime));
输出
当前时间:2017-11-16 00:54:10 runTime:2017-11-16 01:04:10
除了上面返回Date类型的方法之外还有另外几个方法 :4个返回DateBuilder类型方法
- newDate()
- newDateInLocale(Locale)
- newDateInTimezone(Timezone)
- newDateInTimeZoneAndLocale(Timezone, Locale)
7个void验证方法:
- validateSecond(int)
- validateMinute(int)
- validateHour(int)
- validateDayOfMonth(int)
- validateDayOfWeek(int)
- validateMonth(int)
- validateYear(int)
示例源码
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster