在做统计报表时,须将时间格式化为所需格式存入数据库……
package com.sgcc.kpi.rs.entity.sample;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.time.DateUtils;
/**
* 向数据库插入指标信息时各种格式化时间字符串——公共类,静态调用
* @author wu_wenqi
* @version 2012-06-25
*/
public class Pubsysdate {
private static Calendar c = Calendar.getInstance();
/**
* 按年统计时所需系统时间
* @see 例如:to_date('2012-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
*/
public static String SYSDATE_FOR_YEAR = getSysdateForYear();
/**
* 按月统计时所需系统时间
* @see 例如:to_date('2012-6-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
*/
public static String SYSDATE_FOR_MONTH = getSysdateForMonth();
/**
* 按周统计
*/
public static String SYSDATE_FOR_WEEK = getSysdateForWeek();
/**
* 按天统计时所需系统时间
* @see 例如:to_date('2012-6-25 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
*/
public static String SYSDATE_FOR_DAY = getSysdateForDay();
/**
* 按年统计时所需系统时间方法
* @return 例如:to_date('2012-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
*/
private static String getSysdateForYear(){
String re = "to_date('";
re += c.get(Calendar.YEAR) - 1;
re += "-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')";
return re;
}
/**
* 按月统计时所需系统时间方法
* @return 例如:to_date('2012-6-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
*/
private static String getSysdateForMonth(){
String re = "to_date('";
if(c.get(Calendar.MONTH) == 0){
re += c.get(Calendar.YEAR) - 1;
re += "-12";
}else{
re += c.get(Calendar.YEAR);
re += "-";
re += c.get(Calendar.MONTH);
}
re += "-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')";
// System.out.println(re);
return re;
}
/**
* 按周统计
* @return
*/
private static String getSysdateForWeek(){
DateFormat df = DateFormat.getDateInstance();
String re = "to_date('";
Date datetemp = c.getTime();
Date date = DateUtils.addWeeks(datetemp, -1);
int temp = date.getDay();
if(temp == 1){//周一
date = DateUtils.addDays(date, -0);
}else if(temp == 2){//周二
date = DateUtils.addDays(date, -1);
}else if(temp == 3){//周三
date = DateUtils.addDays(date, -2);
}else if(temp == 4){//周四
date = DateUtils.addDays(date, -3);
}else if(temp == 5){//周五
date = DateUtils.addDays(date, -4);
}else if(temp == 6){//周六
date = DateUtils.addDays(date, -5);
}else if(temp == 0){
date = DateUtils.addDays(date, -6);
}
re += df.format(date);
re += " 00:00:00', 'yyyy-mm-dd hh24:mi:ss')";
return re;
}
/**
* 按天统计时所需系统时间
* @return 例如:to_date('2012-6-25 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
*/
private static String getSysdateForDay(){
String re = "to_date('";
if(c.get(Calendar.DAY_OF_MONTH) == 1){
if(c.get(Calendar.MONTH) == 0){ //一月份要返回上年的最后一天
re += c.get(Calendar.YEAR) - 1;
re += "-12-31";
}else if(c.get(Calendar.MONTH) == 2){ //二月要判断是28天还是29天
re += c.get(Calendar.YEAR);
re += "-";
re += c.get(Calendar.MONTH);
re += "-" + getLastDayFor2Month(c.get(Calendar.YEAR));
}else if(c.get(Calendar.MONTH) == 1
|| c.get(Calendar.MONTH) == 3
|| c.get(Calendar.MONTH) == 5
|| c.get(Calendar.MONTH) == 7
|| c.get(Calendar.MONTH) == 8
|| c.get(Calendar.MONTH) == 10
|| c.get(Calendar.MONTH) == 12){ //每月31天
re += c.get(Calendar.YEAR);
re += "-";
re += c.get(Calendar.MONTH);
re += "-31";
}else{ //每月30天
re += c.get(Calendar.YEAR);
re += "-";
re += c.get(Calendar.MONTH);
re += "-30";
}
}else{
re += c.get(Calendar.YEAR);
re += "-";
re += c.get(Calendar.MONTH) + 1;
re += "-";
re += c.get(Calendar.DAY_OF_MONTH) - 1;
}
re += " 00:00:00', 'yyyy-mm-dd hh24:mi:ss')";
return re;
}
/**
* 判断当年是闰年还是平年并返回二月的最后一天
*/
private static int getLastDayFor2Month(int year){
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 29;
}else{
return 28;
}
}
}