工具類
package com.wanggs.utils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
/**
* 獲取兩個時間之間的天數(shù)
* @param maxDate 大的日期
* @param minDate 小的日期
* @return
* @throws Exception
*/
public static int getDay(Date maxDate,Date minDate) throws Exception {
int day = 0;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
minDate=sdf.parse(sdf.format(minDate));
maxDate=sdf.parse(sdf.format(maxDate));
Calendar cal = Calendar.getInstance();
cal.setTime(minDate);
long time1 = cal.getTimeInMillis();
cal.setTime(maxDate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
if (between_days>0) {
day = Integer.parseInt(String.valueOf(between_days));
}
return day;
}
/**
* 由出生日期獲得年齡
*
* @param birthDay
* @return
* @throws Exception
*/
public static int getAge(Date birthDay) throws Exception {
Calendar cal = Calendar.getInstance();
if (cal.before(birthDay)) {
throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
}
//獲取當前年
int yearNow = cal.get(Calendar.YEAR);
//獲取當前月
int monthNow = cal.get(Calendar.MONTH);
//獲取當日
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthDay);
//獲取出生那年
int yearBirth = cal.get(Calendar.YEAR);
//獲取出生月
int monthBirth = cal.get(Calendar.MONTH);
//獲取出生日
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
//年紀
int age = yearNow - yearBirth;
//如果現(xiàn)在的月份小于生日的月份,年齡-1,如果等于出生的月份,日前小于生日日期,年齡-1
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth)
age--;
} else {
age--;
}
}
return age;
}
}
測試
package com.wanggs.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
/**
* Created by wanggs on 2017/9/6.
*/
public class DateTest {
public static void main(String[] args) throws Exception {
String beginTime=new String("2014-09-15 10:22:22");
String endTime=new String("2014-09-22 11:22:22");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date bt = sdf.parse(beginTime);
Date et = sdf.parse(endTime);
// 獲取年齡
int age = DateUtil.getAge(bt);
System.out.println(age);
// 獲取兩個時間的天數(shù)
int resultAge = DateUtil.getDay(et,bt);
System.out.println(resultAge);
}
}
時間處理工具類
package com.wanggs.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
/**
* 日期處理工具,線程安全
*/
public class DateUtils {
private static Map<String, ThreadLocal<DateFormat>> thredlocalmap = new Hashtable<String, ThreadLocal<DateFormat>>();
public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
public static final String yyyyMMdd = "yyyy-MM-dd";
public static final String yyyyMM = "yyyy-MM";
public static final String yyyy = "yyyy";
public static final String HHmmss = "HH:mm:ss";
public static final String MM = "MM";
public static final String week = "E";
static {
thredlocalmap.put(yyyyMMddHHmmss, new ThreadLocal<DateFormat>());
thredlocalmap.put(yyyyMMdd, new ThreadLocal<DateFormat>());
thredlocalmap.put(yyyyMM, new ThreadLocal<DateFormat>());
thredlocalmap.put(yyyy, new ThreadLocal<DateFormat>());
thredlocalmap.put(HHmmss, new ThreadLocal<DateFormat>());
thredlocalmap.put(MM, new ThreadLocal<DateFormat>());
thredlocalmap.put(week, new ThreadLocal<DateFormat>());
}
/**
* 通過格式獲取格式化對象,線程安全
*
* @param datePattern
* @return
*/
public static DateFormat getDateFormat(String datePattern) {
ThreadLocal<DateFormat> threadlocal = getThreadLocal(datePattern);
DateFormat fmt = threadlocal.get();
if (fmt == null) {
fmt = new SimpleDateFormat(datePattern);
threadlocal.set(fmt);
}
return fmt;
}
private static ThreadLocal<DateFormat> getThreadLocal(String datePattern) {
ThreadLocal<DateFormat> theadlocal = thredlocalmap.get(datePattern);
if(theadlocal == null) {
theadlocal = new ThreadLocal<DateFormat>();
thredlocalmap.put(datePattern, theadlocal);
}
return theadlocal;
}
/**
* 將字符串日期轉(zhuǎn)換成Date對象
*
* @param dateStr 日期字符串
* @param datePattern 日期格式
* @return
*/
public static Date parse(String dateStr, String datePattern) {
try {
return getDateFormat(datePattern).parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 將Date對象轉(zhuǎn)換成字符串日期
*
* @param date 日期對象
* @param datePattern 日期格式
* @return
*/
public static String format(Date date, String datePattern) {
return getDateFormat(datePattern).format(date);
}
/**
* 取得特定時間對應的字符串,格式化為yyyy-MM-dd HH:mm:ss
*
* @param date 日期對象
* @return
*/
public static String ymdhmsFormat(Date date) {
return format(date, yyyyMMddHHmmss);
}
/**
* 取得特定時間對應的字符串,格式化為yyyy-MM-dd
*
* @param date
* @return
*/
public static String ymdFormat(Date date) {
return format(date, yyyyMMdd);
}
/**
* 根據(jù)當前日期,得到當前年月
*
* @param date
* @return str
*/
public static final String ymFormat(Date date) {
if (date == null)
return "";
return format(date, yyyyMM);
}
/**
* 根據(jù)當前日期,得到當前年份
*
* @param date
* @return str
*/
public static final String yFormat(Date date) {
if (date == null)
return "";
return format(date, yyyy);
}
/**
* 根據(jù)當前日期,得到當前月份
*
* @param date
* @return str
*/
public static final String mFormat(Date date) {
if (date == null)
return "";
return format(date, MM);
}
/**
* 返回當前的時間梁呈,格式為H:mm:ss
*
* @return 時間字符串
*/
public static final String getTimeNow() {
return format(new Date(), HHmmss);
}
/**
* 把字符串形式轉(zhuǎn)換成日期形式,字符串的格式必須為yyyy-MM-dd
*
* @param ymdStringDate
* @return date
*/
public static final Date ymdString2Date(String ymdStringDate) {
if (ymdStringDate == null)
return null;
return parse(ymdStringDate, yyyyMMdd);
}
/**
* 把字符串形式轉(zhuǎn)換成日期形式,字符串的格式必須為yyyy-MM-dd HH:mm:ss
*
* @param ymdhmsStringDate
* @return date
*/
public static final Date ymdhmsString2Date(String ymdhmsStringDate) {
if (ymdhmsStringDate == null)
return null;
return parse(ymdhmsStringDate, yyyyMMddHHmmss);
}
/**
*
* 得到當前時間,把日期后的時間歸0 變成(yyyy-MM-dd 00:00:00:000)
*
* @return date
*/
public static Date getCurrentDate() {
Date date = new Date();
return zerolizedTime(date);
}
/**
* 把日期后的時間歸0 變成(yyyy-MM-dd 00:00:00:000)
*
* @param fullDate
* @return Date
*/
public static final Date zerolizedTime(Date fullDate) {
Calendar cal = Calendar.getInstance();
cal.setTime(fullDate);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* 得到兩個時間的間隔
*
* @param bDate
* @param eDate
* @return
*/
static public long dateDiffByDay(Date bDate, Date eDate) {
if (bDate == null || eDate == null)
return 0L;
return (bDate.getTime() - eDate.getTime()) / (1000 * 3600 * 24);
}
/**
* 取得指定日期的星期數(shù)
*
* @return String
*/
public static final String getWeek(Date date) {
if (date == null)
return null;
return format(date, week);
}
/**
* 判斷兩個日期字符串是否相等,格式必需為yyyy-MM-dd
*
* @param one
* 第一個日期字符串
* @param two
* 第二個日期字符串
* @return Boolean
*/
public static final boolean isEqual(String one, String two) {
return ymdString2Date(one).equals(ymdString2Date(two));
}
/**
* 判斷兩個日期字符串是否相等
*
* @param one
* 第一個日期字符串
* @param two
* 第二個日期字符串
* @param datePattern
* 包含日期格式的字符串
* @return Boolean
*/
public static final boolean isEqual(String one, String two, String datePattern) {
return isEqual(one, two, datePattern, datePattern);
}
/**
* 判斷兩個日期字符串是否相等
*
* @param one
* 第一個日期字符串
* @param two
* 第二個日期字符串
* @param datePatternOne
* 對應第一個日期字符串的包含日期格式的字符串
* @param datePatternTwo
* 對應第二個日期字符串的包含日期格式的字符串
* @return Boolean
*/
public static final Boolean isEqual(String one, String two,
String datePatternOne, String datePatternTwo) {
return parse(one, datePatternOne).equals(parse(two, datePatternTwo));
}
/**
* 返回兩時間的時間間隔(以分計算)
*
* @param date1
* @param date2
* @return
*/
static public long spaceMinute(Date date1, Date date2) {
Long num1 = date1.getTime();
Long num2 = date2.getTime();
Long space = (num2 - num1) / (1000 * 60);
return space;
}
/**
* 返回兩時間的時間間隔(以天計算)
*
* @paramtime1
* @paramtime2
* @return
*/
static public Long spaceDay(Date date1, Date date2) {
Long num1 = date1.getTime();
Long num2 = date2.getTime();
Long space = (num2 - num1) / (1000 * 3600 * 24);
return space;
}
static public Date getDateAfterDay(Date somedate, int day) {
if (somedate == null)
return null;
Calendar cal = Calendar.getInstance();
cal.setTime(somedate);
cal.add(Calendar.DAY_OF_MONTH, day);
return new Date(cal.getTime().getTime());
}
static public Date getDateAfterDay(int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_MONTH, day);
return cal.getTime();
}
static public Date getTSAfterDay(Date somedate, int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(somedate);
cal.add(Calendar.DAY_OF_MONTH, day);
return cal.getTime();
}
// 取得本月第一天時間
static public Date getFirstDayOfMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
}
時間工具類
package com.toltech.springboot.util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 日期工具類,提供時間轉(zhuǎn)換雷绢、比較振亮、格式化等各種常用方法
* @modificationHistory.
* <ul>
* <li>gaozhanglei 2010-8-27下午04:06:10 TODO</li>
* <li>
* sunju 2012-7-31 下午03:06:05 新增getDate方法抄瑟,
* 修改getNowDate方法以及其它幾個使用了toLocaleString()的方法如绸,解決Linux下時間錯誤問題
* </li>
* </ul>
*/
public class DateUtil {
private static Logger LOG = LoggerFactory.getLogger(DateUtil.class);
/**
* 時間間隔:日
*/
public final static int DATE_INTERVAL_DAY = 1;
/**
* 時間間隔:周
*/
public final static int DATE_INTERVAL_WEEK = 2;
/**
* 時間間隔:月
*/
public final static int DATE_INTERVAL_MONTH = 3;
/**
* 時間間隔:年
*/
public final static int DATE_INTERVAL_YEAR = 4;
/**
* 時間間隔:小時
*/
public final static int DATE_INTERVAL_HOUR = 5;
/**
* 時間間隔:分鐘
*/
public final static int DATE_INTERVAL_MINUTE = 6;
/**
* 時間間隔:秒
*/
public final static int DATE_INTERVAL_SECOND = 7;
/**
* 時間格式:年月日
*/
public final static String DATE_FORMAT_MDY = "MM/dd/yyyy";
public final static String DATE_FORMAT_YMD = "yyyy-MM-dd";
public final static String DATE_FORMAT_YMD_ZH = "yyyy年MM月 ";
public final static String DATE_FORMAT_YMDHMS_ZH = "yyyy年MM月dd日 ";
public final static String DATE_FORMATE_YM="yyyyMM";
public final static String DATE_FORMATE_LC="yy年MM月";
public final static String DATE_FORMAT_MD="MM-dd";
public final static String DATE_FORMAT_YMD_NL="yyyyMMdd";
/**
* 時間格式:年月日時分秒
*/
public final static String DATE_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
public final static String DATE_FORMATE_LX_YMDHMS = "yyyyMMddHHmmss";
public static final String DATE_FORMATE_LX_YMDHMSS = "mmss";
/**
* 獲得時間
* @author sunju
* @creationDate. 2012-7-31 下午03:06:05
* @param date 時間
* @param dateFormat 時間格式
* @return 時間
*/
public static Date getDate(Date date, String dateFormat) {
return parseDate(dateFormat(date, dateFormat), dateFormat);
}
/**
* 獲得當前日期(年月日)
* @author sunju
* @creationDate. 2010-8-27 下午05:11:23
* @return 當前時間(yyyy-MM-dd)
*/
public static Date getNowDate() {
return parseDate(dateFormat(new Date(), DATE_FORMAT_YMD), DATE_FORMAT_YMD);
}
/**
* 獲取當前時間字符串(年月日)
* @author sunju
* @creationDate. 2011-5-4 下午08:22:34
* @return 時間字符串
*/
public static String getNowStringDate() {
return dateFormat(new Date(), DATE_FORMAT_YMD);
}
/**
* 獲得當前時間(年月日時分秒)
* @author sunju
* @creationDate. 2010-8-27 下午05:12:57
* @return 當前時間(yyyy-MM-dd HH:mm:ss)
*/
public static Date getNowTime() {
return parseDate(dateFormat(new Date(), DATE_FORMAT_YMDHMS), DATE_FORMAT_YMDHMS);
}
/**
* 獲取當前時間字符串(年月日時分秒)
* @author sunju
* @creationDate. 2014-3-10 下午03:16:42
* @return 時間字符串
*/
public static String getNowStringTime() {
return dateFormat(new Date(), DATE_FORMAT_YMDHMS);
}
/**
* 獲得明天的日期字符串(格式年月日)
* @author sunju
* @creationDate. 2011-5-4 下午08:19:28
* @return 明天的日期
*/
public static String getTomorrowStringDate() {
return dateFormat(getTomorrowTime(), DATE_FORMAT_YMD);
}
/**
* 獲得明天的日期(年月日)
* @author sunju
* @creationDate. 2011-5-4 下午08:19:57
* @return 明天的日期
*/
public static Date getTomorrowDate() {
return dateAdd(DATE_INTERVAL_DAY, getNowDate(), 1);
}
/**
* 獲得明天的時間(年月日時分秒)
* @author sunju
* @creationDate. 2011-5-4 下午08:20:19
* @return 明天的時間
*/
public static Date getTomorrowTime() {
return dateAdd(DATE_INTERVAL_DAY, getNowTime(), 1);
}
/**
* 獲得昨天的日期
* @author sunju
* @creationDate. 2013-10-22 下午03:54:48
* @return 昨天的日期
*/
public static Date getYesterdayDate() {
return dateAdd(DATE_INTERVAL_DAY, getNowDate(), -1);
}
/**
* 獲得昨天的時間(年月日時分秒)
* @author sunju
* @creationDate. 2013-10-22 下午03:54:48
* @return 昨天的日期
*/
public static Date getYseterdayTime(){
return dateAdd(DATE_INTERVAL_DAY, getNowTime(), -1);
}
/**
* 獲取當月第一天
* @author sunju
* @creationDate. 2013-10-22 下午03:45:53
* @return 日期
*/
public static Date getMonthFirst() {
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE, 1); // 設為當前月的1號
return getDate(lastDate.getTime(), DATE_FORMAT_YMD);
}
/**
* 獲得下個月第一天的日期
* @author sunju
* @creationDate. 2013-10-22 下午03:52:38
* @return 日期
*/
public static Date getNextMonthFirst() {
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.MONTH, 1); // 加一個月
lastDate.set(Calendar.DATE, 1); // 把日期設置為當月第一天
return getDate(lastDate.getTime(), DATE_FORMAT_YMD);
}
public static Date getMonthStartTime(Date date, int monthDiff){
Calendar calendar = GregorianCalendar.getInstance();
if(date != null){
calendar.setTime(date);
}else{
calendar.setTimeInMillis(System.currentTimeMillis());
}
if(monthDiff != 0){
calendar.add(Calendar.MONTH, monthDiff);
}
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date resultDate = calendar.getTime();
return resultDate;
}
public static Date getMonthEndTime(Date date, int monthDiff){
Calendar calendar = GregorianCalendar.getInstance();
if(date != null){
calendar.setTime(date);
}else{
calendar.setTimeInMillis(System.currentTimeMillis());
}
if(monthDiff != 0){
calendar.add(Calendar.MONTH, monthDiff);
}
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
Date resultDate = calendar.getTime();
return resultDate;
}
/**
* 取得當前星期幾
* @author sunju
* @creationDate. 2010-9-20 下午05:34:36
* @param date 時間
* @return 星期
*/
public static String getWeekOfDate(Date date) {
if (date == null) {
return null;
}
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekDays[w];
}
/**
* 時間類型轉(zhuǎn)換返回字符串
* @author sunju
* @creationDate. 2010-8-27 下午05:18:37
* @param date 時間
* @param dateFormat 時間格式
* @return 轉(zhuǎn)換后的時間字符串
*/
public static String dateFormat(Date date, String dateFormat) {
if (date == null) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
return format.format(date);
}
/**
* 字符串時間類型轉(zhuǎn)換返回Date類型
* @author sunju
* @creationDate. 2010-8-27 下午05:23:35
* @param date 字符串時間
* @param dateFormat 時間格式
* @return 轉(zhuǎn)換后的時間
*/
public static Date parseDate(String date, String dateFormat) {
if (StringUtils.isEmpty(date)) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
try {
return format.parse(date);
} catch (Exception e) {
LOG.error(e.getMessage());
return null;
}
}
public static Date getDateStartTime(Date date){
Calendar calendar = GregorianCalendar.getInstance();
if(date != null){
calendar.setTime(date);
}else{
calendar.setTimeInMillis(System.currentTimeMillis());
}
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date resultDate = calendar.getTime();
return resultDate;
}
public static Date getDateEndTime(Date date){
Calendar calendar = GregorianCalendar.getInstance();
if(date != null){
calendar.setTime(date);
}else{
calendar.setTimeInMillis(System.currentTimeMillis());
}
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
Date resultDate = calendar.getTime();
return resultDate;
}
/**
* 加時間
* @author sunju
* @creationDate. 2010-8-27 下午05:28:06
* @param interval 增加項黍匾,可以是天數(shù)赘来、月份现喳、年數(shù)、時間犬辰、分鐘嗦篱、秒
* @param date 時間
* @param num 加的數(shù)目
* @return 時間加后的時間
*/
public static Date dateAdd(int interval, Date date, int num) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
switch (interval) {
case DATE_INTERVAL_DAY:
calendar.add(Calendar.DATE, num);
break;
case DATE_INTERVAL_WEEK:
calendar.add(Calendar.WEEK_OF_MONTH, num);
break;
case DATE_INTERVAL_MONTH:
calendar.add(Calendar.MONTH, num);
break;
case DATE_INTERVAL_YEAR:
calendar.add(Calendar.YEAR, num);
break;
case DATE_INTERVAL_HOUR:
calendar.add(Calendar.HOUR, num);
break;
case DATE_INTERVAL_MINUTE:
calendar.add(Calendar.MINUTE, num);
break;
case DATE_INTERVAL_SECOND:
calendar.add(Calendar.SECOND, num);
break;
default:
}
return calendar.getTime();
}
/**
* 兩個時間時間差[前面時間和比較時間比,小于比較時間返回負數(shù)]
* @author sunju
* @creationDate. 2010-8-27 下午05:26:13
* @param interval 比較項,可以是天數(shù)幌缝、月份灸促、年數(shù)、時間涵卵、分鐘浴栽、秒
* @param date 時間
* @param compare 比較的時間
* @return 時間差(保留兩位小數(shù)點,小數(shù)點以后兩位四舍五入)
*/
public static double getDateDiff(int interval, Date date, Date compare) {
if (date == null || compare == null) {
return 0;
}
double result = 0;
double time = 0;
Calendar calendar = null;
switch (interval) {
case DATE_INTERVAL_DAY:
time = date.getTime() - compare.getTime();
result = time / 1000d / 60d / 60d / 24d;
break;
case DATE_INTERVAL_HOUR:
time = date.getTime() - compare.getTime();
result = time / 1000d / 60d / 60d;
break;
case DATE_INTERVAL_MINUTE:
time = date.getTime() / 1000d / 60d;
result = time - compare.getTime() / 1000d / 60d;
break;
case DATE_INTERVAL_MONTH:
calendar = Calendar.getInstance();
calendar.setTime(date);
time = calendar.get(Calendar.YEAR) * 12d;
calendar.setTime(compare);
time -= calendar.get(Calendar.YEAR) * 12d;
calendar.setTime(date);
time += calendar.get(Calendar.MONTH);
calendar.setTime(compare);
result = time - calendar.get(Calendar.MONTH);
break;
case DATE_INTERVAL_SECOND:
time = date.getTime() - compare.getTime();
result = time / 1000d;
break;
case DATE_INTERVAL_WEEK:
calendar = Calendar.getInstance();
calendar.setTime(date);
time = calendar.get(Calendar.YEAR) * 52d;
calendar.setTime(compare);
time -= calendar.get(Calendar.YEAR) * 52d;
calendar.setTime(date);
time += calendar.get(Calendar.WEEK_OF_YEAR);
calendar.setTime(compare);
result = time - calendar.get(Calendar.WEEK_OF_YEAR);
break;
case DATE_INTERVAL_YEAR:
calendar = Calendar.getInstance();
calendar.setTime(date);
time = calendar.get(Calendar.YEAR);
calendar.setTime(compare);
result = time - (double)calendar.get(Calendar.YEAR);
break;
default:
break;
}
return new BigDecimal(result).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 獲取時間差[前面時間和比較時間比,小于比較時間返回負數(shù)]
* @author sunju
* @creationDate. 2010-9-1 下午04:36:07
* @param level 返回時間等級(1:返回天;2:返回天-小時;3:返回天-小時-分4:返回天-小時-分-秒;)
* @param date 時間
* @param compare 比較的時間
* @return 時間差(保留兩位小數(shù)點,小數(shù)點以后兩位四舍五入)
*/
public static String getDateBetween(Integer level, Date date, Date compare) {
if (date == null || compare == null) {
return null;
}
long s = new BigDecimal(getDateDiff(DATE_INTERVAL_SECOND, date, compare)).setScale(2, BigDecimal.ROUND_HALF_UP).longValue();
int ss = 1;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = s / dd;
long hour = (s - day * dd) / hh;
long minute = (s - day * dd - hour * hh) / mi;
long second = (s - day * dd - hour * hh - minute * mi) / ss;
String flag =(day < 0 || hour < 0 || minute < 0 || second < 0) ? "-" : "";
day = Math.abs(day);
hour = Math.abs(hour);
minute = Math.abs(minute);
second = Math.abs(second);
StringBuilder result = new StringBuilder(flag);
switch (level) {
case 1:
if (day != 0) {
result.append(day).append("天");
}
break;
case 2:
if (day != 0) {
result.append(day).append("天");
}
if (hour != 0) {
result.append(hour).append("小時");
}
break;
case 3:
if (day != 0) {
result.append(day).append("天");
}
if (hour != 0) {
result.append(hour).append("小時");
}
if (minute != 0) {
result.append(minute).append("分");
}
break;
case 4:
if (day != 0) {
result.append(day).append("天");
}
if (hour != 0) {
result.append(hour).append("小時");
}
if (minute != 0) {
result.append(minute).append("分");
}
if (second != 0) {
result.append(second).append("秒");
}
break;
default:
break;
}
return result.toString();
}
/**
* 時間是否是今天
* @author sunju
* @creationDate. 2011-5-4 下午08:24:58
* @param date 時間
* @return 布爾
*/
public static boolean isToday(Date date) {
if (date == null) return false;
return getNowStringDate().equals(dateFormat(date, DATE_FORMAT_YMD));
}
/**
* 時間是否合法
* @author sunju
* @creationDate. 2012-6-19 下午01:07:59
* @param date 時間
* @param dateFormat 時間格式
* @return
*/
public static boolean isValidDate(String date, String dateFormat) {
try {
new SimpleDateFormat(dateFormat).parse(date);
return true;
} catch (Exception e) {
return false;
}
}
/**
*
* 是否大于現(xiàn)在的時間
* true 大于
* <ul>
* <li>
* <b>原因:<br/>
* <p>
* [2014-8-27]gaozhanglei<br/>
* @param date
* @param dateFormate
* @return
* TODO
* </p>
* </li>
* </ul>
*/
public static boolean isgtnow(String date,String dateFormate) {
boolean flag=false;
try {
Date nowdt=new Date();
Date compt=DateUtil.parseDate(date, dateFormate);
long nowtm=nowdt.getTime();
long comptm=compt.getTime();
if(comptm > nowtm) {
flag=true;
}
}catch (Exception e) {
flag=false;
}
return flag;
}
/**
* 得到本月的最后一天
*
* @return
*/
public static String getMonthLastDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar
.getActualMaximum(Calendar.DAY_OF_MONTH));
return dateFormat(calendar.getTime(),DATE_FORMAT_YMD);
}
/**
* 獲取時間在當月的天數(shù)
* @param date
* @return
*/
public static int getDayOfMonth(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
public static int getMaxDayOfMonth(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
//當前時間轉(zhuǎn)時間戳
public static String timeStamp(){
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YMDHMS);
String time="1970-01-06 11:45:55";
String date =null;
try {
date = format.parse(time).toString();
} catch (ParseException e) {
}
return date;
}
/**
* 計算產(chǎn)品到期時間
* @param productCycle 產(chǎn)品包周期值
* @return 產(chǎn)品包到期時間
*/
public static Date getDueTime(String productCycle){
Date payTime = new Date(); //付費成功時間
Calendar calendar = Calendar.getInstance();
calendar.setTime(payTime);
switch (productCycle) {
case "0": // 包年
calendar.add(Calendar.MONTH, 13);
break;
case "1": //包半年
calendar.add(Calendar.MONTH, 7);
break;
case "2": //包季
calendar.add(Calendar.MONTH, 4);
break;
case "3": //包月
calendar.add(Calendar.MONTH, 1);
break;
case "4": //包天
calendar.add(Calendar.DATE, 1);
break;
case "5": //單點
calendar.add(Calendar.MONTH, 1);
break;
default:
break;
}
return calendar.getTime();
}
/***
* 用戶產(chǎn)品包實際不可看時間(用于產(chǎn)品通知)
* @param productCycle
* @return
*/
public static Date getActualDueTime(String productCycle){
Date payTime = new Date(); //付費成功時間
Calendar calendar = Calendar.getInstance();
calendar.setTime(payTime);
switch (productCycle) {
case "0": // 包年
calendar.add(Calendar.MONTH, 13);
break;
case "1": //包半年
calendar.add(Calendar.MONTH, 7);
break;
case "2": //包季
calendar.add(Calendar.MONTH, 4);
break;
case "3": //包月
calendar.add(Calendar.MONTH, 1);
break;
case "4": //包天
calendar.add(Calendar.DATE, 1);
break;
case "5": //單點
calendar.add(Calendar.MONTH, 1);
break;
default:
break;
}
return calendar.getTime();
}
}