Sprint之java.util.Time

49 篇文章 0 订阅
18 篇文章 0 订阅
参考资料
1 在Spring中使用JDK Timer进行任务调度
[url]http://www.builder.com.cn/2007/0529/404717.shtml[/url]
小结
JDK Timer可以满足一些简单的任务调度需求,使用JDK Timer的任务对执行时间点应该没有严格的要求,因为JDK Timer只能做到近似的时间安排.
[b]Spring在org.springframework.scheduling.timer中提供了几个JDK Timer的支持类,主要在以下三方面对JDK Timer提供了支持:
1) ScheduledTimerTask,它对TimerTask提供封装并或配置调度信息;
2)通过MethodInvokingTimerTaskFactoryBean类可以将一个Bean的方法封装为TimerTask;
3)通过TimerFactoryBean可以更方便地配置Timer,此外让Timer的生命周期和Spring容器的生命周期相关,在初始化TimerFactoryBean后,启动Timer,在Spring容器关闭前取消Timer。[/b]
[color=green]在Spring之Email<封装了常用的四种发送Email的方法(TEXT,HTML,IMG,FILE)> 基础之上加的任务调度^_^[/color]
一 工程图片
[img]http://dl.iteye.com/upload/attachment/510900/68a61f1d-65a1-3587-9fe1-1ae1d69b1226.jpg[/img]
二 具体代码
1 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:mail.properties</value>
</property>
</bean>

<bean id="sendMessageTime" class="test.time.SendMessageTime">
<property name="subject">
<value>${mail.subject}</value>
</property>
<property name="text">
<value>${mail.text}</value>
</property>
</bean>

<bean id="scheduledHelloTask"
class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask">
<ref bean="sendMessageTime" />
</property>

<property name="period">
<value>2000</value>
</property>
<property name="delay">
<value>1000</value>
</property>
</bean>

<bean
class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="scheduledHelloTask" />
</list>
</property>
</bean>

</beans>

2 任务类:SendMessageTime.java

public class SendMessageTime extends TimerTask{
//消息计数器
private static int messageCount = 0;
//发送标题
private String subject;
private String text;
private MailMessageFactory mms = new MailMessageFactory();

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

@Override
public void run() {
System.out.println("开始发送消息...");
mms.setSubject(getSubject())
.setText(getText())
.send();
System.out.println("发送消息条数: "+(++messageCount));
}
}

3 启动类

public class SpringTimeTest {


public static void main(String[] args) {

AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//休眠30秒后结束进程
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
context.close();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取当天日期 获取本周一日期 获取本周日的日期 获取上周一日期:" + tt.getPreviousWeekday("yyyy-MM-dd")); 获取上周日日期:" + tt.getPreviousWeekSunday("yyyy-MM-dd")); 获取上周一日期:" + tt.getWeekday(-1, "yyyy-MM-dd")); 获取上周日日期:" + tt.getWeekSunday(-1, "yyyy-MM-dd")); 获取下周一日期:" + tt.getNextMonday("yyyy-MM-dd")); 获取下周日日期:" + tt.getNextSunday("yyyy-MM-dd")); 获取本月第一天日期:" + tt.getFirstDayOfMonth()); 获取本月最后一天日期:" + tt.getDefaultDay()); 获取上月第一天日期:" + tt.getPreviousMonthFirst("yyyy-MM-dd")); 获取上月最后一天的日期:" + tt.getPreviousMonthEnd("yyyy-MM-dd")); 获取某月第一天日期:" + tt.getMonthFirst(0, "yyyy-MM-dd")); 获取某月最后一天的日期:" + tt.getMonthEnd(0, "yyyy-MM-dd")); 获取下月第一天日期:" + tt.getNextMonthFirst("yyyy-MM-dd")); 获取下月最后一天日期:" + tt.getNextMonthEnd("yyyy-MM-dd")); 获取本年的第一天日期:" + tt.getCurrentYearFirst()); 获取本年最后一天日期:" + tt.getCurrentYearEnd()); 获取去年的第一天日期:" + tt.getPreviousYearFirst()); 获取去年的最后一天日期:" + tt.getPreviousYearEnd()); 获取明年第一天日期:" + tt.getNextYearFirst()); 获取明年最后一天日期:" + tt.getNextYearEnd()); 获取本季度第一天:" + tt.getThisSeasonFirstTime(11)); 获取本季度最后一天:" + tt.getThisSeasonFinallyTime(11)); 获取两个日期之间间隔天数 获取当前月的第几周:" + tt.getWeekOfMonth()); 获取当前年份:" + tt.getYear()); 获取当前月份:" + tt.getMonth()); 获取今天在本年的第几天:" + tt.getDayOfYear()); 获得今天在本月的第几天(获得当前日):" + tt.getDayOfMonth()); 获得今天在本周的第几天:" + tt.getDayOfWeek()); 字符串转时间 获得一个日期所在周的星期几的日期
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值