package com.cp.report;
import java.util.*;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
* 时间处理类
*
* @author 成平
*
*/
public class DateTimeHelper {
private static Locale localeChina = Locale.CHINA;
private final static int FMT_CHINA_DATE = 100; //中文方式 xxxx年xx月xx日
private final static int FMT_CHINA_DATETIME = 101; //中文方式 xxxx年xx月xx日 xx时xx分xx秒
private final static int FMT_CHINA_TIME = 102; //中文方式 xx时xx分xx秒
private final static int FMT_CHINA_MOTH_DAY = 103; //中文方式 xx月xx日
private final static int FMT_CHINA_MOTH_DAY_TIME = 103; //中文方式 xx月xx日 xx时xx分xx秒
private final static int FMT_CHINA_MOTH_DAY_SHORT_TIME = 103; //中文方式 xx月xx日 xx时xx分
public DateTimeHelper() {
}
private static String day_Of_The_Week[] = { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday" };
private static String day_Of_The_Week_China[] = { "星期日", "星期一", "星期二",
"星期三", "星期四", "星期五", "星期六" };
/**
* 获得两个日期之间的天数
*
* @param dStart
* Date
* @param dEnd
* Date
* @return int
*/
public static int afterDay(Date dStart, Date dEnd) {
return after(dStart, dEnd, 86400);
}
/**
* 获得两个日期之间的天数
*
* @param sStart
* @param sEnd
* @return
*/
public static int afterDay(String sStart, String sEnd) {
try {
return afterDay(toDate(sStart), toDate(sEnd));
} catch (Exception e) {
return 0;
}
}
/**
* 获得两个日期之间的天数
*
* @param sStartEnd
* 例如 2000-01-01~2000-12-01
* @return
*/
public static int afterDay(String sStartEnd) {
String[] sDate = sStartEnd.split("~");
if (sDate == null || sDate[0] == null || sDate[1] == null) {
return 0;
}
try {
return afterDay(toDate(sDate[0]), toDate(sDate[1]));
} catch (Exception e) {
return 0;
}
}
/**
* 获得两个日期之间的小时数
*
* @param dStart
* Date
* @param dEnd
* Date
* @return int
*/
public static int afterHour(Date dStart, Date dEnd) {
return after(dStart, dEnd, 3600);
}
/**
* 获得两个日期之间的分钟数
*
* @param dStart
* Date
* @param dEnd
* Date
* @return int
*/
public static int afterMinute(Date dStart, Date dEnd) {
return after(dStart, dEnd, 60);
}
/**
* 获得两个日期之间的秒数
*
* @param dStart
* Date
* @param dEnd
* Date
* @return int
*/
public static int aftersecond(Date dStart, Date dEnd) {
return after(dStart, dEnd, 1);
}
/**
* 把日期类型转换成中文类型的日期
*
* @param dDate
* Date
* @return String
*/
public static String chinaDataFormat(Date dDate) {
if (dDate == null) {
return null;
}
String sDate = toString(dDate, "yyyy-MM-dd");
String tmpArr[] = sDate.split("-");
String dArr[] = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
for (int i = 0; i < 10; i++) {
Integer x = new Integer(i);
String temp = x.toString();
tmpArr[0] = tmpArr[0].replaceAll(temp, dArr[i]);
}
tmpArr[0] = tmpArr[0] + "年";
if (tmpArr[1].length() == 1) {
tmpArr[1] = dArr[Integer.parseInt(tmpArr[1])] + "月";
} else {
if (tmpArr[1].substring(0, 1).equals("0")) {
tmpArr[1] = dArr[Integer.parseInt(tmpArr[1].substring(tmpArr[1]
.length() - 1, tmpArr[1].length()))]
+ "月";
} else {
tmpArr[1] = "十"
+ dArr[Integer.parseInt(tmpArr[1].substring(tmpArr[1]
.length() - 1, tmpArr[1].length()))] + "月";
tmpArr[1] = tmpArr[1].replaceAll("零", "");
}
}
if (tmpArr[2].length() == 1) {
tmpArr[2] = dArr[Integer.parseInt(tmpArr[2])] + "日";
} else {
if (tmpArr[2].substring(0, 1).equals("0")) {
tmpArr[2] = dArr[Integer.parseInt(tmpArr[2].substring(tmpArr[2]
.length() - 1, tmpArr[2].length()))]
+ "日";
} else {
tmpArr[2] = dArr[Integer.parseInt(tmpArr[2].substring(0, 1))]
+ "十"
+ dArr[Integer.parseInt(tmpArr[2].substring(tmpArr[2]
.length() - 1, tmpArr[2].length()))] + "日";
tmpArr[2] = tmpArr[2].replaceAll("零", "");
}
}
return tmpArr[0] + tmpArr[1] + tmpArr[2];
}
/**
* 把日期转换成中文汉字的日期格式(2000-10-01:二零零年十二月一号)
*
* @param sDate
* String
* @return String
*/
public static String chinaDataFormat(String sDate) throws Exception {
return chinaDataFormat(toDate(sDate));
}
/**
* 把日期转换成中文汉字的日期格式(2000-10-01:二零零年十二月一号)
*
* @param dDate
* Date
* @return int
*/
public static int getDay(Date dDate) {
return getDateNumber(dDate, Calendar.DAY_OF_MONTH);
}
/**
*
* @param dDate
* Date
* @return int
*/
public static int getDate(Date dDate) {
return getDateNumber(dDate, Calendar.DATE);
}
/**
*
* @param dDate
* Date
* @return int
*/
public static int getHours(Date dDate) {
return getDateNumber(dDate, Calendar.HOUR);
}
/**
*
* @param dDate
* Date
* @return int
*/
public static int getMinutes(Date dDate) {
return getDateNumber(dDate, Calendar.MINUTE);
}
/**
*
* @param dDate
* Date
* @return int
*/
public static int getMonth(Date dDate) {
return getDateNumber(dDate, Calendar.MONTH) + 1;
}
/**
*
* @param dDate
* Date
* @return int
*/
public static int getSeconds(Date dDate) {
return getDateNumber(dDate, Calendar.SECOND);
}
/**
*
* @param dDate
* Date
* @return int
*/
public static int getYear(Date dDate) {
return getDateNumber(dDate, Calendar.YEAR);
}
/**
*
* @param sDate
* String
* @return int
*/
public static int getYear(String sDate) throws Exception {
return getDateNumber(toDate(sDate), Calendar.YEAR);
}
/**
* 获取日期在当前日期的月份中的位置
*
* @param dDate
* Date
* @return String
*/
public static int getDayInMonth(Date dDate) {
return dDate == null ? -8888 : Integer.parseInt(getFormatDate(dDate,
"d"));
}
/**
* 获取日期在当前日期的月份中的位置
*
* @param sDate
* String
* @return String
*/
public static int getDayInMonth(String sDate) throws Exception {
return sDate == null || sDate.trim() == "" ? -8888 : Integer
.parseInt(getFormatDate(sDate, "d"));
}
/**
* 获取日期在当前日期对应的星期在当前月份中的位置
*
* @param dDate
* Date
* @return String
*/
public static int getDayOfWeekInMouth(Date dDate) {
return dDate == null ? -8888 : Integer.parseInt(getFormatDate(dDate,
"F"));
}
/**
* 获取日期在当前日期对应的星期在当前月份中的位置
*
* @param sDate
* String
* @return String
*/
public static int getDayOfWeekInMouth(String sDate) throws Exception {
return sDate == null || sDate.trim() == "" ? -8888 : Integer
.parseInt(getFormatDate(sDate, "F"));
}
/**
* 获取日期对应的星期
*
* @param dDate
* Date
* @return String
*/
public static String getDayInWeek(Date dDate) {
return getFormatDate(dDate, "E");
}
/**
* 获取日期对应的星期
*
* @param sDate
* String
* @return String
*/
public static String getDayInWeek(String sDate) throws Exception {
return getFormatDate(sDate, "E");
}
/**
* 获取日期在当前日期的年份中的位置
*
* @param dDate
* Date
* @return String
*/
public static int getDayInYear(Date dDate) {
retur