java把日期转化为cron表达式

简介:

Java 中,如何把日期(时间点,不是时间段)转化为cron表达式呢?

我觉得这个功能是很常用的,结果在网上竟然没有找到,真实奇怪了?!

直接给代码:

 

Java代码   收藏代码
  1. /*** 
  2.      *  
  3.      * @param date 
  4.      * @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss 
  5.      * @return 
  6.      */  
  7.     public static String formatDateByPattern(Date date,String dateFormat){  
  8.         SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);  
  9.         String formatTimeStr = null;  
  10.         if (date != null) {  
  11.             formatTimeStr = sdf.format(date);  
  12.         }  
  13.         return formatTimeStr;  
  14.     }  
  15. /*** 
  16.      * convert Date to cron ,eg.  "0 06 10 15 1 ? 2014" 
  17.      * @param date  : 时间点 
  18.      * @return 
  19.      */  
  20.     public static String getCron(java.util.Date  date){  
  21.         String dateFormat="ss mm HH dd MM ? yyyy";  
  22.         return formatDateByPattern(date, dateFormat);  
  23.     }  

 测试:

 

 

Java代码   收藏代码
  1. @Test  
  2.     public void test_getCron(){  
  3.         String cron=TimeHWUtil.getCron(new Date());  
  4.         System.out.println(cron);  
  5.     }  

 运行结果:

 

 

关于cron,请参阅:http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/crontrigger

Format

A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are as follows:

Field Name Mandatory Allowed Values Allowed Special Characters
Seconds YES 0-59 , - * /
Minutes YES 0-59 , - * /
Hours YES 0-23 , - * /
Day of month YES 1-31 , - * ? / L W
Month YES 1-12 or JAN-DEC , - * /
Day of week YES 1-7 or SUN-SAT , - * ? / L #
Year NO empty, 1970-2099 , - * /

So cron expressions can be as simple as this: * * * * ? *

or more complex, like this: 0/5 14,18,3-39,52 * ? JAN,MAR,SEP MON-FRI 2002-2010

 

Examples

Here are some full examples:

**Expression** **Meaning**
0 0 12 * * ? Fire at 12pm (noon) every day
0 15 10 ? * * Fire at 10:15am every day
0 15 10 * * ? Fire at 10:15am every day
0 15 10 * * ? * Fire at 10:15am every day
0 15 10 * * ? 2005 Fire at 10:15am every day during the year 2005
0 * 14 * * ? Fire every minute starting at 2pm and ending at 2:59pm, every day
0 0/5 14 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
0 0/5 14,18 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
0 0-5 14 * * ? Fire every minute starting at 2pm and ending at 2:05pm, every day
0 10,44 14 ? 3 WED Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
0 15 10 ? * MON-FRI Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ? Fire at 10:15am on the 15th day of every month
0 15 10 L * ? Fire at 10:15am on the last day of every month
0 15 10 L-2 * ? Fire at 10:15am on the 2nd-to-last last day of every month
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L 2002-2005 Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
0 15 10 ? * 6#3 Fire at 10:15am on the third Friday of every month
0 0 12 1/5 * ? Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
0 11 11 11 11 ? Fire every November 11th at 11:11am.
Pay attention to the effects of '?' and '*' in the day-of-week and day-of-month fields!
相关文章
|
1天前
|
Java 编译器 API
Java基础教程(17)-Java8中的lambda表达式和Stream、Optional
【4月更文挑战第17天】Lambda表达式是Java 8引入的函数式编程特性,允许函数作为参数或返回值。它有简洁的语法:`(parameters) -> expression 或 (parameters) ->{ statements; }`。FunctionalInterface注解用于标记单方法接口,可以用Lambda替换。
|
1天前
|
人工智能 安全 Java
Java8 - LocalDateTime时间日期类使用详解
Java8 - LocalDateTime时间日期类使用详解
|
2天前
|
Java 程序员 编译器
JavaSE&Java8 Lambda 表达式
JavaSE&Java8 Lambda 表达式
|
3天前
|
安全 Java API
Java日期与时间
Java日期与时间
25 1
|
6天前
|
并行计算 Java 编译器
Java Lambda表达式简介
Java Lambda表达式简介
12 0
|
8天前
|
Java 开发者
探索 Java 的函数式接口和 Lambda 表达式
【4月更文挑战第19天】Java 中的函数式接口和 Lambda 表达式提供了简洁、灵活的编程方式。函数式接口有且仅有一个抽象方法,用于与 Lambda(一种匿名函数语法)配合,简化代码并增强可读性。Lambda 表达式的优点在于其简洁性和灵活性,常用于事件处理、过滤和排序等场景。使用时注意兼容性和变量作用域,它们能提高代码效率和可维护性。
|
11天前
|
分布式计算 Java API
Java 8新特性之Lambda表达式与Stream API
【4月更文挑战第16天】本文将介绍Java 8中的两个重要新特性:Lambda表达式和Stream API。Lambda表达式是Java 8中引入的一种新的编程语法,它允许我们将函数作为参数传递给其他方法,从而使代码更加简洁、易读。Stream API是Java 8中引入的一种新的数据处理方式,它允许我们以声明式的方式处理数据,从而使代码更加简洁、高效。本文将通过实例代码详细讲解这两个新特性的使用方法和优势。
|
14天前
|
Java
Java 14 强势登场:Switch 表达式的进化之路
Java 14 强势登场:Switch 表达式的进化之路
18 0
|
3天前
|
安全 Java 调度
Java线程:深入理解与实战应用
Java线程:深入理解与实战应用
23 0