Knowledge Point: Learn to use time class and time class conversion between time format.
Requirements: 1. Execute business logic at a time of day in a thread
2. Update the time in the database table "take the time in the database and then update to the table"
1) timed execution of business logic
A service is required to perform a business every day one o'clock in the morning or several days on the thread:
Check execution create next year table name
int hour = this.gettiming (calendar.hour_of_day);
if (hour = = 1) {//daily 1 o'clock in the morning execution
//Business logic code
}
/**
* Get current point of year, month, day, time, minute
*
* @return
* * private static int gettiming (int timeflag) {
//Add calendar for the current year of the year now
= Calendar.getinstance ();
Return Now.get (Timeflag);
}
Conversion class for TIME: "Very important"
public class Getsystemdate {/** * get system Current time (year/month) * @return/public static String GetDate () {Date date = new D
Ate ();
SimpleDateFormat format = new SimpleDateFormat ("YyyyMMdd");
return Format.format (date);
/** * Get system Current time (year-month-day) * @return/public static String Getdateforten () {date = new date ();
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
return Format.format (date);
/** * Get system Current time (minutes and seconds) * @return/public static String GetTime () {date = new date ();
SimpleDateFormat format = new SimpleDateFormat ("Yyyymmddhhmmss");
return Format.format (date);
/** * Get system Current time (year-month-day: minutes: SEC) * @return/public static String GetDateTime () {date = new date ();
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
return Format.format (date); /** * Get current point year, month, day, time, minute * * @return/public static int gettiming (int timeflag) {//calendar.year//Add Calendar of the current year of the year now = Calendar.getinstance ();
Return Now.get (Timeflag); /** * Time format conversion (minutes and seconds) * @return/public static String Getformattime (Timestamp Timestamp) {SimpleDateFormat
format = new SimpleDateFormat ("Yyyymmddhhmmss");
return Format.format (timestamp);
}
}
2 Update the time in the database table: the statement "select Sysdate ti from dual" for database time
Timestamp Timestamp = This.baseDao.getCurrentDateTime ();
String timestr = Getsystemdate.getformattime (timeStamp);
This.baseDao.execute (Update_time, New object[]{timestr});
The following are related classes and methods:
GetCurrentDateTime ()
/**
* Access System (database service) Time
* @return/public
Timestamp getcurrentdatetime () {
return Dbfeaturefactory.getfeature (). Getsystime ();
}
Database Fit classes: 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 = Properties
Hander.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 {//default is Oracle Dbfeature = new Oracledbfeature ();
} return dbfeature; }
}
Getsystime () method
@Override public
Timestamp getsystime () {
String sql= ' select Sysdate ti from dual ';
Return (Timestamp) this.getjdbctemplate (). queryForObject (SQL, timestamp.class);