private static final Logger log = Logger.getLogger(ElectricContractServiceImpl.class);
private ElectricContractDao electricContractDao;
private InquiryContractDAO inquiryContractDAO;
@SuppressWarnings("unchecked")
public void AutoExcute() throws Exception {
String fileRealPath = "";// 附件的完整路径
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//格式化对象
String strD = sdf.format(new Date());
//当天执行,获取前一天数据(现为同步前一周数据)
String beginTime = DateConvertor.dateToString(DateConvertor.offsetDate(new Date(), -7, "day"), "yyyy-MM-dd HH:mm:ss");
String endTime = DateConvertor.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
List<Contract> list = electricContractDao.findContract(beginTime, endTime);
// 根路径
String base = FtpPropertiesReader.getProperty("ftpElectricLocalDir");
String filename = "ContractInfo_" + strD; // 文件名
fileRealPath = base + filename + ".csv";
File file = new File(fileRealPath);// 新建文件
if (!file.exists()) {
file.createNewFile();
}
if (log.isDebugEnabled()) {
log.debug("创建文件-开始");
}
FileOutputStream outStr = new FileOutputStream(file);
BufferedOutputStream buff = new BufferedOutputStream(outStr);
建议使用Quartz来管理定时任务。
如果是你的项目用了Spring,那就可以通过配置来实现定时任务。
我这里用了Spring4.2.6,Quartz2.3.0(2.0.2以及更新版本),slf4j-api-1.7.25.jar、slf4j-log4j12-1.7.25.jar、log4j1.2.17、commons-logging1.2等。
1、写个任务执行类
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyJobExecutor {
private static final Log log = LogFactory.getLog(MyJobExecutor.class);
private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void autoExcute() {
String now = datetimeFormat.format(new java.util.Date());
log.info("当前时刻:" + now + ",开始执行定时任务...");
// 具体的操作...
}
}
2、配置定时任务的触发器
<?xml version="1.0" encoding="UTF-8"?>
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
spring quartz beans
<!-- 任务执行类的Bean配置,路径自定义-->
<!-- 1、配置需要定时任务类和执行方法 -->
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定任务类 -->
<property name="targetObject" ref="myExecutor" />
<!-- 执行任务的方法 -->
<property name="targetMethod" value="autoExcute" />
</bean>
<!-- 2、配置触发器 -->
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myJobDetail" />
<!-- 每隔10秒钟触发一次 -->
<!-- <property name="cronExpression" value="0/10 * * * * ?" /> -->
<!-- 每周日上午7点30分触发一次。表达式网上可查找相关资料 。-->
<property name="cronExpression" value="0 30 7 ? * SUN"/>
</bean>
<!-- 3、触发定时任务 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 可配置多个触发器 -->
<ref bean="myTrigger" />
</list>
</property>
</bean>
public static int dayForWeek(String pTime) throws Exception {
format = new SimpleDateFormat("yyyy-MM-dd" );
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0 ;
if (c.get(Calendar.DAY_OF_WEEK) == 1 ){
dayForWeek = 7 ;
}else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1 ;
}
if( dayForWeek==7)//周日
{
//调用定时任务
}
}
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY) {
System.out.println("今天是星期天");
}
@Component
public class TimeTask {
private static final Logger logger = LoggerFactory.getLogger(TimeTask.class);
@Autowired
private serviceImpl service;
@Scheduled(cron = "0 0 12 ? * SUN")//每周日中午12点执行
public void doTask() {
try {
logger.info("定时任务开始执行........");
//在此处调用你的AutoExcute方法
//
logger.info("定时任务执行完成........");
}
catch (Exception e) {
logger.info("定时任务执行失败" + e);
}
}
}
用sping定时任务啊,在配置文件配置执行时间,比如: cron="0 15 10 ? * SUN" />,就是在每个周日的早上10点15执行某个方法
操作时间不要那么麻烦/ jdk 1.8 以后提供了一个 LocalDate 用起来很方便.直接使用 LocalDate.now().dayOfWeek 就是当前时间是周几,直接做判断逻辑就可以了