Date转Cron
//cron表达式格式
private static final String cron = "ss mm HH dd MM ? yyyy";
/**
* 时间转换成Cron表达式
* "1/5 * * * * ?";
*/
public static String date2cron(Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(cron);
return simpleDateFormat.format(date);
}
获取spring容器上下文
@Component
public class GetSpringBean implements ApplicationContextAware {
// 声明一个静态变量用于保存spring容器上下文
@Autowired
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
public static <T> T get(Class<T> clazz) {
return context.getBean(clazz);
}
}