定时执行业务逻辑+更新数据库表中的时间
来源:互联网 发布:一直很安静 知乎 编辑:程序博客网 时间:2024/05/16 11:21
知识点:学会使用时间类及时间类的时间格式间的转换。
需求:1.在线程中每天的某个时间定时的执行业务逻辑
2.更新数据库表中的时间【取数据库中的时间然后更新到表中去】
1)定时执行业务逻辑
在线程中要求每天凌晨一点或者几点执行某项业务:
// 检查执行创建下年度表名
int hour = this.getTiming(Calendar.HOUR_OF_DAY);if (hour == 1) { // 每天凌晨1点的时候执行//业务逻辑代码}/** * 获取当前时间点 年度、月份、日、时、分 * * @return */private static int getTiming(int timeFlag) { //添加年度 当前年度Calendar now = Calendar.getInstance();return now.get(timeFlag);}
public class GetSystemDate {/** * 获取系统当前时间(年月日) * @return */public static String getDate(){Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");return format.format(date);}/** * 获取系统当前时间(年-月-日) * @return */public static String getDateForTen(){Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");return format.format(date);}/** * 获取系统当前时间(年月日时分秒) * @return */public static String getTime(){Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");return format.format(date);}/** * 获取系统当前时间(年-月-日 时:分:秒) * @return */public static String getDateTime(){Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return format.format(date);}/** * 获取当前时间点 年度、月份、日、时、分 * * @return */public static int getTiming(int timeFlag) { //Calendar.YEAR//添加年度 当前年度Calendar now = Calendar.getInstance();return now.get(timeFlag);}/** * 时间格式转换(年月日时分秒) * @return */public static String getFormatTime(Timestamp timestamp){SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");return format.format(timestamp);}}
2)更新数据库表中的时间:取数据库时间的语句【select sysdate ti from dual】
Timestamp timeStamp = this.baseDao.getCurrentDateTime();String timeStr = GetSystemDate.getFormatTime(timeStamp);this.baseDao.execute(UPDATE_TIME, new Object[]{timeStr});
getCurrentDateTime()
/** * 取得系统(数据库服务)时间 * @return */public Timestamp getCurrentDateTime(){return DbFeatureFactory.GetFeature().getSysTime();}数据库适配的类:DbFeatureFactory
public class DbFeatureFactory {private static DbFeature dbFeature = null;private static String productName = null;public static DbFeature GetFeature() {if (dbFeature == null) {if (productName == null)productName = PropertiesHander.getEVConfValue(BIZConstant.DATABASE_SERVICE).toLowerCase();if (productName.indexOf("postgre") > -1) {dbFeature = new PostgreDbFeature();} else if (productName.indexOf("oracle") > -1) {dbFeature = new OracleDbFeature();} else if (productName.indexOf("db2") > -1) {dbFeature = new DB2DbFeature();} else if (productName.indexOf("mssql") > -1) {dbFeature = new MSSQLDbFeature();} else if (productName.indexOf("mysql") > -1) {dbFeature = new MSSQLDbFeature();} else if (productName.indexOf("sybase") > -1) {dbFeature = new SybaseDbFeature();} else {// 默认为ORACLEdbFeature = new OracleDbFeature();}}return dbFeature;}}
@Overridepublic Timestamp getSysTime() {String sql="select sysdate ti from dual";return (Timestamp)this.getJdbcTemplate().queryForObject( sql, Timestamp.class );}
0 0
- 定时执行业务逻辑+更新数据库表中的时间
- 配置监听器,创建线程定时执行业务逻辑
- java中的业务逻辑
- 项目中碰到的一些比较经典的业务逻辑案例,不定时更新
- 定时更新NTP时间
- 数据库中的SELECT语句逻辑执行顺序分析
- MySQL中的定时执行
- [转]以数据库为中心的系统中的业务逻辑组织方式
- crontab 奇数时间定时执行
- mysql定时时间不执行
- 性能优化--数据库优化--业务逻辑优化
- 连接数据库操作业务逻辑层
- Windows任务计划 定时执行PHP代码及扩展(对数据库进行更新操作)
- mysql定时执行数据库操作
- 执行数据库更新操作
- 业务逻辑
- 业务逻辑
- 业务逻辑
- 图像特征提取
- Android ListView之setEmptyView的问题
- ios日期格式和转换
- 堆栈实现中缀表达式转后缀表达式及计算表达式的值
- asp中存储过程各方法详解
- 定时执行业务逻辑+更新数据库表中的时间
- 1237: 排名
- adb连接智能电视
- LeetCode7——Reverse Integer
- iOS中单例的创建方式
- IOS开源项目
- SDJZU_新生_排序_sort(sort方法)
- ajax用例
- c++中new的三种用法详细解析