代码语言:javascript复制
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import org.joda.time.DateTime;
/**
* <p>日期工具类</p>
*/
public class DateUtils {
/**定义常量**/
/**yyyyMMdd*/
public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
/**yyyy-MM-dd*/
public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
/**yyyy-MM-dd HH:mm:ss*/
public static final String DATE_FORMAT_FULL_STR = "yyyy-MM-dd HH:mm:ss";
/**yyyy-MM-dd HH:mm:ss SSS*/
public static final String DATE_FULL_STR_S = "yyyy-MM-dd HH:mm:ss SSS";
public DateUtils() {
}
/**
* <p>获得当前时间<日期格式>
* @return yyyy-MM-dd HH:mm:ss
*/
public static Date getCurrDateTime() {
return new Date(System.currentTimeMillis());
}
/**
* 取得当前系统时间<字符格式>
* @return yyyy-MM-dd HH:mm:ss
*/
public static String getCurrTime() {
Date date_time = new Date();
return FormatDate(date_time, "yyyy-MM-dd HH:mm:ss");
}
/**
* 取得当前系统时间<字符格式>
* @return HH:mm
*/
public static String getCurrTimeHm() {
Date date_time = new Date();
return FormatDate(date_time, "HH:mm");
}
/**
* 取得当前系统日期<字符格式>
* @return yyyy-MM-dd
*/
public static String getCurrDate() {
Date date_time = new Date();
return FormatDate(date_time, "yyyy-MM-dd");
}
/**
* 取得日期的年份
* @param date 日期
* @return yyyy 年份字符串
*/
public static String getYear(Date date) {
return FormatDate(date, "yyyy");
}
/**
* 取得日期的月份
* @param date 日期
* @return mm 月份字符串
*/
public static String getMonth(Date date) {
return FormatDate(date, "MM");
}
/**
* 取得日期的天份
* @param date 日期
* @return dd 天字符串
*/
public static String getDay(Date date) {
return FormatDate(date, "dd");
}
/**
* 取得日期的小时
* @param date 日期
* @return hh 小时字符串
*/
public static String getHour(Date date) {
return FormatDate(date, "HH");
}
/**
* 取得日期的分钟
* @param date 时间
* @return mm 分钟字符串
*/
public static String getMinute(Date date) {
return FormatDate(date, "mm");
}
/**
* 取得时间的秒
* @param date 时间
* @return ss 秒字符串
*/
public static String getSecond(Date date) {
return FormatDate(date, "ss");
}
/**
* 返回明天日期<日期型>
* <pre>
* DateTime added = dt.plusDays(1);
* DateTime added = dt.plus(Period.days(1));
* DateTime added = dt.withFieldAdded(DurationFieldType.days(), 1);
* </pre>
* @return
*/
public static DateTime getTomorrow() {
return new DateTime().plusDays(1);
}
/**
* 返回今天的日期(几号)
*
* @return
*/
public static int getTodayDay() {
return new DateTime().getDayOfMonth();
}
/**
* 取得日以上粒度起始时间
* @param granularity 粒度
* @param statisticDate 结束时间
* @return 起始时间
*/
public static String getBeginDate(String granularity, String statisticDate) {
String beginDate = "";
Date date = stringToDateShort(statisticDate);
Date beginDateTemp = null;
if (granularity.equals("1")) {// 日
beginDateTemp = date;
}
if (granularity.equals("2")) {// 周
beginDateTemp = getWeekBegin(date);
}
if (granularity.equals("3")) {// 旬
beginDateTemp = getPeriodBegin(date);
} else if (granularity.equals("4")) {// 月
beginDateTemp = getMonthBegin(date);
} else if (granularity.equals("5")) {// 季
beginDateTemp = getSeasonBegin(date);
} else if (granularity.equals("6")) {// 半年
beginDateTemp = getHalfYearBegin(date);
} else if (granularity.equals("7")) {// 年
beginDateTemp = getYearBegin(date);
}
beginDate = dateToStringShort(beginDateTemp);
return beginDate;
}
/**
* 取得日以上粒度结束时间
* @param granularity 粒度
* @param statisticDate 结束时间
* @return 起始时间
*/
public static String getEndDate(String granularity, String statisticDate) {
String beginDate = "";
Date date = stringToDateShort(statisticDate);
Date beginDateTemp = null;
if (granularity.equals("1")) {// 日
beginDateTemp = date;
}
if (granularity.equals("2")) {// 周
beginDateTemp = getWeekEnd(date);
}
if (granularity.equals("3")) {// 旬
beginDateTemp = getPeriodEnd(date);
} else if (granularity.equals("4")) {// 月
beginDateTemp = getMonthEnd(date);
} else if (granularity.equals("5")) {// 季
beginDateTemp = getSeasonEnd(date);
} else if (granularity.equals("6")) {// 半年
beginDateTemp = getHalfYearEnd(date);
} else if (granularity.equals("7")) {// 年
beginDateTemp = getYearEnd(date);
}
beginDate = dateToStringShort(beginDateTemp);
return beginDate;
}
/**
* 使用预设格式提取字符串日期
* @param strDate 日期字符串
* @return
*/
public static Date parse(String strDate) {
return parse(strDate,DATE_FORMAT_FULL_STR);
}
/**
* 使用用户格式提取字符串日期
* @param strDate 日期字符串
* @param pattern 日期格式
* @return
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 两个时间比较
* @param date
* @return
*/
public static int compareDateWithNow(Date date1){
Date date2 = new Date();
int rnum =date1.compareTo(date2);
return rnum;
}
/**
* 两个时间比较(时间戳比较)
* @param date
* @return
*/
public static int compareDateWithNow(long date1){
long date2 = dateToUnixTimestamp();
if(date1>date2){
return 1;
}else if(date1<date2){
return -1;
}else{
return 0;
}
}
/**
* 获取系统当前时间
* @return
*/
public static String getNowTime(String type) {
SimpleDateFormat df = new SimpleDateFormat(type);
return df.format(new Date());
}
/**
* 将指定的日期转换成Unix时间戳
* @param String date 需要转换的日期 yyyy-MM-dd HH:mm:ss
* @return long 时间戳
*/
public static long dateToUnixTimestamp(String date) {
long timestamp = 0;
try {
timestamp = new SimpleDateFormat(DATE_FORMAT_FULL_STR).parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
/**
* 将指定的日期转换成Unix时间戳
* @param String date 需要转换的日期 yyyy-MM-dd
* @return long 时间戳
*/
public static long dateToUnixTimestamp(String date, String dateFormat) {
long timestamp = 0;
try {
timestamp = new SimpleDateFormat(dateFormat).parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
/**
* 将当前日期转换成Unix时间戳
* @return long 时间戳
*/
public static long dateToUnixTimestamp() {
long timestamp = new Date().getTime();
return timestamp;
}
/**
* 将Unix时间戳转换成日期
* @param long timestamp 时间戳
* @return String 日期字符串
*/
public static String unixTimestampToDate(long timestamp) {
SimpleDateFormat sd = new SimpleDateFormat(DATE_FORMAT_FULL_STR);
sd.setTimeZone(TimeZone.getTimeZone("GMT 8"));
return sd.format(new Date(timestamp));
}
/**
* 返回前n天的日期
*
* @return
*/
public static DateTime getOffsetDay(int days) {
return new DateTime().minusDays(days);
}
/**
* 取得时间描述
* 日 yyyy-mm-dd
* 月 yyyy年mm月
* 季 yyyy年第×季度
* 年 yyyy年
* @param granularity 粒度
* @param statisticDate 时间
* @return 时间对应粒度的描述
*/
public static String getTimedes(String granularity, String statisticDate) {
String timedes = "";
Date date = stringToDateShort(statisticDate);
String year = "", month = "01", day = "01";
year = getYear(date);
month = getMonth(date);
day = getDay(date);
if (granularity.equals("1")) {// 日
timedes = statisticDate;
} else if (granularity.equals("4")) {// 月
timedes = year "年" month "月";
} else if (granularity.equals("8")) {// 季
String quarter = month "-" day;
if (quarter.equals("03-31")) {
timedes = year "年 第1季度";
} else if (quarter.equals("06-30")) {
timedes = year "年 第2季度";
} else if (quarter.equals("09-30")) {
timedes = year "年 第3季度";
} else if (quarter.equals("12-31")) {
timedes = year "年 第4季度";
}
} else if (granularity.equals("32")) {// 年
timedes = year "年";
}
return timedes;
}
/**
* 日期转化为字符串
* @param date 时间
* @return yyyy-MM-dd HH:mm:ss 格式化的时间字符串
*/
public static String dateToString(Date date) {
if (date == null)
return "";
return FormatDate(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* 日期转化为字符串
* @param date 时间
* @return yyyy-MM-dd 格式化的时间字符串
*/
public static String dateToStringShort(Date date) {
if (date == null)
return "";
return FormatDate(date, "yyyy-MM-dd");
}
/**
* 字符串转换为日期
* @param dateString yyyy-MM-dd HH:mm:ss
* @return 日期
*/
public static Date stringToDate(String dateString) {
String sf = "yyyy-MM-dd HH:mm:ss";
Date dt = stringToDate(dateString, sf);
return dt;
}
/**
* 字符串转换为日期
* @param dateString yyyy-MM-dd
* @return 日期
*/
public static Date stringToDateShort(String dateString) {
String sf = "yyyy-MM-dd";
Date dt = stringToDate(dateString, sf);
return dt;
}
/**
* 主要在日期区间查询中使用 例:查询区间2011-10-09 - 2011-10-09
* 为了也可以查询出2011-10-09 00:00:00 - 2011-10-09 23:59:59 间的数据
* 字符串转换字符串
*
* @param dateString yyyy-MM-dd
* @return yyyy-MM-dd HH:mm:ss
*/
public static String stringToString(String dateString) {
return dateString " 23:59:59";
}
/**
* 对日期进行格式化
* @param date 日期
* @param sf 日期格式
* @return 字符串
*/
public static String FormatDate(Date date, String sf) {
if (date == null)
return "";
SimpleDateFormat dateformat = new SimpleDateFormat(sf);
return dateformat.format(date);
}
/** 字符串转换为日期
* @param dateString 日期格式字符串
* @param sf 日期格式化定义
* @return 转换后的日期
*/
public static Date stringToDate(String dateString, String sf) {
ParsePosition pos = new ParsePosition(0);
SimpleDateFormat sdf = new SimpleDateFormat(sf);
Date dt = sdf.parse(dateString, pos);
return dt;
}
/**
* 计算两个日期差(毫秒)
* @param date1 时间1
* @param date2 时间2
* @return 相差毫秒数
*/
public static long diffTwoDate(Date date1, Date date2) {
long l1 = date1.getTime();
long l2 = date2.getTime();
return (l1 - l2);
}
/**
* 计算两个日期差(天)
* @param date1 时间1
* @param date2 时间2
* @return 相差天数
*/
public static int diffTwoDateDay(Date date1, Date date2) {
long l1 = date1.getTime();
long l2 = date2.getTime();
int diff = Integer.parseInt("" (l1 - l2) / 3600 / 24 / 1000);
return diff;
}
/**
*
* @param currentTime 计算的日期: 格式为:yyyy-MM-dd HH:mm:ss
* @param type 偏移的类别
* @param iQuantity 偏移数量
* @return 偏移后的时间串
*/
public static String getDateChangeTime(String currentTime, String type, int iQuantity) {
Date curr = stringToDate(currentTime);
curr = getDateChangeTime(curr, type, iQuantity);
return dateToString(curr);
}
/**
* 获取偏移的日期 shortdate
* @param dateString : 格式为:yyyy-MM-dd
* @param type
* @param iQuantity
* @return
*/
public static String getDateChangeDate(String dateString, String type, int iQuantity) {
Date curr = stringToDateShort(dateString);
curr = getDateChangeTime(curr, type, iQuantity);
return dateToStringShort(curr);
}
/**
*
* @param currentTime 计算的日期
* @param type 偏移的类别
* @param iQuantity 偏移数量
* @return 偏移后的时间
*/
public static Date getDateChangeTime(Date currentTime, String type, int iQuantity) {
int year = Integer.parseInt(FormatDate(currentTime, "yyyy"));
int month = Integer.parseInt(FormatDate(currentTime, "MM"));
// 月份修正
month = month - 1;
int day = Integer.parseInt(FormatDate(currentTime, "dd"));
int hour = Integer.parseInt(FormatDate(currentTime, "HH"));
int mi = Integer.parseInt(FormatDate(currentTime, "mm"));
int ss = Integer.parseInt(FormatDate(currentTime, "ss"));
GregorianCalendar gc = new GregorianCalendar(year, month, day, hour, mi, ss);
// 月份修正
// gc.add(GregorianCalendar.MONTH, -1);
if (type.equalsIgnoreCase("y")) {
gc.add(GregorianCalendar.YEAR, iQuantity);
} else if (type.equalsIgnoreCase("m")) {
gc.add(GregorianCalendar.MONTH, iQuantity);
} else if (type.equalsIgnoreCase("d")) {
gc.add(GregorianCalendar.DATE, iQuantity);
} else if (type.equalsIgnoreCase("h")) {
gc.add(GregorianCalendar.HOUR, iQuantity);
} else if (type.equalsIgnoreCase("mi")) {
gc.add(GregorianCalendar.MINUTE, iQuantity);
} else if (type.equalsIgnoreCase("s")) {
gc.add(GregorianCalendar.SECOND, iQuantity);
}
return gc.getTime();
}
/**
*根据年、月取得月末的日期
* @param year 年
* @parm month 月
* @return time 返回日期格式"yyyy-mm-dd"
*/
public static String getMonthEnd(String year, String month) {
String time = "";
int len = 31;
int iYear = Integer.parseInt(year);
int iMonth = Integer.parseInt(month);
if (iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11)
len = 30;
if (iMonth == 2) {
len = 28;
if ((iYear % 4 == 0 && iYear % 100 == 0 && iYear % 400 == 0) || (iYear % 4 == 0 && iYear % 100 != 0)) {
len = 29;
}
}
time = year "-" month "-" String.valueOf(len);
return time;
}
/**
* 取月初
* @param date
* @return
*/
public static Date getMonthBegin(Date date) {
String newDateStr = FormatDate(date, "yyyy-MM") "-01";
// FormatDate(date, "yyyy-MM-dd");
return stringToDateShort(newDateStr);
}
/**
* 取月末时间
* @param date 日期
* @return date
*/
public static Date getMonthEnd(Date date) {
int year = Integer.parseInt(FormatDate(date, "yyyy"));
int month = Integer.parseInt(FormatDate(date, "MM"));
int day = Integer.parseInt(FormatDate(date, "dd"));
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day, 0, 0, 0);
int monthLength = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
String newDateStr = FormatDate(date, "yyyy") "-" FormatDate(date, "MM") "-";
if (monthLength < 10)
newDateStr = "0" monthLength;
else
newDateStr = "" monthLength;
return stringToDateShort(newDateStr);
}
/**
* 取季初
* @param date
* @return
*/
public static Date getSeasonBegin(Date date) {
int month = Integer.parseInt(FormatDate(date, "MM"));
String newDateStr = FormatDate(date, "yyyy") "-";
if (month >= 1 && month <= 3) {
newDateStr = "01-01";
} else if (month >= 4 && month <= 6) {
newDateStr = "04-01";
} else if (month >= 7 && month <= 9) {
newDateStr = "07-01";
} else if (month >= 10 && month <= 12) {
newDateStr = "10-01";
}
return stringToDateShort(newDateStr);
}
/**
* 取半年初
* @param date
* @return
*/
public static Date getHalfYearBegin(Date date) {
int month = Integer.parseInt(FormatDate(date, "MM"));
String newDateStr = FormatDate(date, "yyyy") "-";
if (month <= 6) {
newDateStr = "01-01";
} else {
newDateStr = "07-01";
}
return stringToDateShort(newDateStr);
}
/**
* 取旬初
* @param date
* @return
*/
public static Date getPeriodBegin(Date date) {
int days = Integer.parseInt(FormatDate(date, "dd"));
String newDateStr = FormatDate(date, "yyyy-MM") "-";
if (days <= 10) {
newDateStr = "01";
} else if (days <= 20) {
newDateStr = "11";
} else {
newDateStr = "21";
}
return stringToDateShort(newDateStr);
}
/**
* 取周初
* @param date
* @return
*/
public static Date getWeekBegin(Date date) {
int year = Integer.parseInt(FormatDate(date, "yyyy"));
int month = Integer.parseInt(FormatDate(date, "MM"));
// 月份修正
month = month - 1;
int day = Integer.parseInt(FormatDate(date, "dd"));
GregorianCalendar gc = new GregorianCalendar(year, month, day);
int week = gc.get(GregorianCalendar.DAY_OF_WEEK) - 1;
if (week == 0) {
week = 7;
}
gc.add(GregorianCalendar.DATE, 0 - week 1);
return gc.getTime();
}
/**
* 取周末
* @param date
* @return
*/
public static Date getWeekEnd(Date date) {
int year = Integer.parseInt(FormatDate(date, "yyyy"));
int month = Integer.parseInt(FormatDate(date, "MM"));
// 月份修正
month = month - 1;
int day = Integer.parseInt(FormatDate(date, "dd"));
GregorianCalendar gc = new GregorianCalendar(year, month, day);
int week = gc.get(GregorianCalendar.DAY_OF_WEEK) - 1;
if (week == 0) {
week = 7;
}
gc.add(GregorianCalendar.DATE, 7 - week);
return gc.getTime();
}
/**
* 取旬末
* @param date
* @return
*/
public static Date getPeriodEnd(Date date) {
int days = Integer.parseInt(FormatDate(date, "dd"));
String newDateStr = FormatDate(date, "yyyy-MM") "-";
if (days <= 10) {
newDateStr = "10";
} else if (days <= 20) {
newDateStr = "20";
} else {
newDateStr = FormatDate(getMonthEnd(date), "yyyy-MM-dd");
}
return stringToDateShort(newDateStr);
}
/**
* 取半年末
* @param date
* @return
*/
public static Date getHalfYearEnd(Date date) {
int month = Integer.parseInt(FormatDate(date, "MM"));
String newDateStr = FormatDate(date, "yyyy") "-";
if (month <= 6) {
newDateStr = "06-30";
} else {
newDateStr = "12-31";
}
return stringToDateShort(newDateStr);
}
/**
* 取季度末时间
* @param date 日期
* @return date
*/
public static Date getSeasonEnd(Date date) {
int month = Integer.parseInt(FormatDate(date, "MM"));
String newDateStr = FormatDate(date, "yyyy") "-";
if (month >= 1 && month <= 3) {
newDateStr = "03-31";
} else if (month >= 4 && month <= 6) {
newDateStr = "06-30";
} else if (month >= 7 && month <= 9) {
newDateStr = "09-30";
} else if (month >= 10 && month <= 12) {
newDateStr = "12-31";
}
return stringToDateShort(newDateStr);
}
/**
* 取得年初
* @param date
* @return
*/
public static Date getYearBegin(Date date) {
String newDateStr = FormatDate(date, "yyyy") "-01-01";
return stringToDateShort(newDateStr);
}
/**
* 是否为年末
* @param date 时间
* @return
*/
public static Date getYearEnd(Date date) {
String newDateStr = FormatDate(date, "yyyy") "-12-31";
return stringToDateShort(newDateStr);
}
/**
* 是否为旬末
* @param date 时间
* @return 是或否
*/
public static boolean IsXperiodEnd(Date date) {
boolean flag = false;
String day = getDay(date);
if (day.equalsIgnoreCase("10")) {
flag = true;
} else if (day.equalsIgnoreCase("20")) {
flag = true;
}
return flag;
}
/**
* 获取一个月后的同一天的String类型日期
* @return String
*/
public static String getDateAfterAMonth(String date) {
Date iDate = stringToDateShort(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(iDate);
calendar.add(Calendar.MONTH, 1);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String s = df.format(calendar.getTime());
return s;
}
/**
* 比较日期大小
* @author gaofeng
* @param dateString1
* @param dateString2
* @return rslt: dateString1>dateString2 = 1;dateString1<dateString2 = -1;dateString1=dateString2 = 0;
*/
public static int compareDateString(String dateString1, String dateString2) {
int rslt = 0;
java.util.Date date1 = stringToDate(dateString1, "yyyy-MM-dd");
java.util.Date date2 = stringToDate(dateString2, "yyyy-MM-dd");
int intdate1 = Integer.parseInt(FormatDate(date1, "yyyyMMdd"));
int intdate2 = Integer.parseInt(FormatDate(date2, "yyyyMMdd"));
if (intdate1 > intdate2) {
rslt = 1;
} else if (intdate1 < intdate2) {
rslt = -1;
} else {
rslt = 0;
}
return rslt;
}
/**
* 比较日期大小
* @author gaofeng
* @param dateString1
* @param dateString2
* @return
*/
public static int compareDate(Date date1, Date date2) {
int rslt = 0;
int intdate1 = Integer.parseInt(FormatDate(date1, "yyyyMMdd"));
int intdate2 = Integer.parseInt(FormatDate(date2, "yyyyMMdd"));
if (intdate1 > intdate2) {
rslt = 1;
} else if (intdate1 < intdate2) {
rslt = -1;
} else {
rslt = 0;
}
return rslt;
}
/**
* 取得时间描述
* 日 mm月dd日
* 月 yy年mm月
* 季 yy年×季度(x=1/2/3/4)
* 年 yy年
* @param granularity 粒度
* @param statisticDate 时间
* @return 时间对应粒度的描述
*/
public static String getTimedes2(String granularity, String statisticDate) {
String timedes = "";
Date date = stringToDateShort(statisticDate);
String year = "", month = "01", day = "01";
year = getYear(date).substring(2, 4);
month = getMonth(date);
day = getDay(date);
if (granularity.equals("1")) {// 日
timedes = month "月" day "日";
;
} else if (granularity.equals("4")) {// 月
timedes = year "年" month "月";
} else if (granularity.equals("8")) {// 季
String quarter = month "-" day;
if (quarter.equals("03-31")) {
timedes = year "年1季度";
} else if (quarter.equals("06-30")) {
timedes = year "年2季度";
} else if (quarter.equals("09-30")) {
timedes = year "年3季度";
} else if (quarter.equals("12-31")) {
timedes = year "年4季度";
}
} else if (granularity.equals("32")) {// 年
timedes = year "年";
}
return timedes;
}
/**
* 返回当天日期的yyyyMMdd hh:mm:ssssss表示
*
* @return
*/
public static String getNowTimeMillisecond() {
return new DateTime().toString(DATE_FULL_STR_S);
}
/**
* 判断日期是否是周末日期
* @param date
* @return
*/
public static boolean isWeekEndDay(Date date){
return getWeekEnd(date).compareTo(date) == 0;
}
/**
* 判断日期是否为月末日期
* @param date
* @return
*/
public static boolean isMonthEndDay(Date date){
return getMonthEnd(date).compareTo(date) == 0;
}
/**
* 判断日期是否为季末日期
* @param date
* @return
*/
public static boolean isSeasonEndDay(Date date){
return getSeasonEnd(date).compareTo(date) == 0;
}
/**
* 判断是日期是否为半年末
* @param date
* @return
*/
public static boolean isHalfYearEndDay(Date date){
return getHalfYearEnd(date).compareTo(date) == 0;
}
/**
* 判断日期是否为年末日期
* @param date
* @return
*/
public static boolean isYearEndDay(Date date){
return getYearEnd(date).compareTo(date) == 0;
}
/**
*
* @param currentTime 计算日期
* @param type 偏移的类别
* @param iQuantity 偏移数量
* @return 偏移后的时间串
*/
public static String getDateChangeALL(String currentTime, String type,
int iQuantity) {
Date curr = null;
String newtype = "";
if(currentTime.length()==10){
curr = stringToDateShort(currentTime);
}
if(currentTime.length()>10){
curr = stringToDate(currentTime);
}
// 日
if(type.equals("1")){
iQuantity = iQuantity;
newtype = "d";
}
// 周,按照7天计算
else if(type.equals("2")){
iQuantity = iQuantity*7;
newtype = "d";
}
// 旬,按照10天计算
else if(type.equals("3")){
iQuantity = iQuantity*10;
newtype = "d";
String month= getMonth(curr);
if(month.equals("02")||month.equals("2")){
String day= getDay(curr);
if(day.equals("20")&&iQuantity>0){
iQuantity =8;
}
}
}
//月
else if(type.equals("4")){
iQuantity = iQuantity;
newtype = "m";
}
// 旬,按照3个月计算
else if(type.equals("5")){
iQuantity = iQuantity*3;
newtype = "m";
}
//半年,按照六个月计算
else if(type.equals("6")){
iQuantity = iQuantity*6;
newtype = "m";
}
//年
else if(type.equals("7")){
newtype = "y";
}
else{
iQuantity = iQuantity;
newtype = "d";
}
Date change = getDateChangeTime(curr, newtype, iQuantity);
// if(!type.equals("d")){
// change = this.getMonthEnd(change);
// }
return dateToStringShort(change);
}
public static String getPreEndDate(String granularity, String statisticDate)
{
Date date = stringToDateShort(statisticDate);
String year = ""; String month = "01"; String day = "01";
year = getYear(date);
month = getMonth(date);
day = getDay(date);
int preM = Integer.parseInt(month) - 1;
if (preM == 0) {
preM = 12;
}
String newdate=getDateChangeALL(statisticDate, granularity, -1);
// return getEndDate(granularity, dateToStringShort(stringToDateShort(year "-" preM "-" day)));
return getEndDate(granularity, newdate);
}
public static String getBeginDateFromDateRange(String dataRange) {
return dataRange.split(" - ")[0];
}
public static String getEndDateFromDateRange(String dataRange) {
return dataRange.split(" - ")[1];
}
public static String getBeginDateTimeFromDateRange(String dataRange) {
return getBeginDateFromDateRange(dataRange) " 00:00:00";
}
public static String getEndDateTimeFromDateRange(String dataRange) {
return getEndDateFromDateRange(dataRange) " 23:59:59";
}
/**
* 字符串转换为数据库时间java.sql.Date格式
* @param dateStr
* @return
*/
public static java.sql.Date stringToSqlDateShort(String dateStr) {
java.util.Date javaDate = stringToDateShort(dateStr);
java.sql.Date d = new java.sql.Date(javaDate.getTime());
return d;
}
}