下載網址:https://download.csdn.net/my
實現代碼:
package com.winste.common.util;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public static String getToday() {
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
return fmt.format(today);
}
/**
* 獲取當前年月日時分秒毫秒
* @return
*/
public static String getCuurentTimeAndMillisecond() {
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return fmt.format(today);
}
/**
* 獲取當前爲當前年度的週數
* @return
*/
public static String getCurrentWeek() {
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy");
return fmt.format(today);
}
public static String getToday(String format) {
Date dt = new Date(System.currentTimeMillis());
return format(dt, format);
}
public static String format(Date dt, String format) {
if (dt == null)
return null;
SimpleDateFormat fmt = new SimpleDateFormat(format);
return fmt.format(dt);
}
public static Date parse(final String dateStr, final String pattern) {
DateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public static Date parseDate(String dateStr, String format) {
if (dateStr == null)
return null;
try {
return org.apache.commons.lang.time.DateUtils.parseDate(dateStr,
new String[] { format });
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 得到傳入日期n月後的日期,如果傳入日期爲null,則表示當前日期n月後的日期
* @param dt
* @return
*/
public static Date getAddMonthDate(Date dt, int months) {
if (dt == null)
dt = new Date(System.currentTimeMillis());
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
cal.add(Calendar.MONTH, months);
return cal.getTime();
}
/**
* 得到傳入日期n天后的日期,如果傳入日期爲null,則表示當前日期n天后的日期
* @param dt
* @param days
* @return
*/
public static Date getAddDayDate(Date dt, int days) {
if (dt == null)
dt = new Date(System.currentTimeMillis());
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + days);
// 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 beginDate
* @return
*/
public static int getDateMargin(Date beginDate, Date endDate) {
if (endDate == null)
endDate = new Date();
final int mOfDay = 1000 * 60 * 60 * 24;
final long divtime = (endDate.getTime() - beginDate.getTime());
final long lday = divtime % mOfDay > 0 ?
divtime / mOfDay + 1 : divtime / mOfDay;
return Long.valueOf(lday).intValue();
}
/**
* 得到當前時間和指定時間的小時差,不足一小時按小時算
*
* @return
*/
public static long getHourMargin(Date beginDate, Date endDate) {
if (endDate == null)
endDate = new Date();
long hour = endDate.getTime() - beginDate.getTime();
hour = hour / (60 * 60 * 1000);
hour = hour % (60 * 60 * 1000) > 0 ? hour + 1 : hour;
return hour;
}
/**
* 得到兩個時間相差多少分鐘,不足一分鐘按一分鐘算
*
* @param beginDate
* @param endDate
* @return
*/
public static int getMinuteMargin(Date beginDate, Date endDate) {
if (endDate == null)
endDate = new Date();
final int mOfMinute = 1000 * 60;
final long divtime = (endDate.getTime() - beginDate.getTime());
final long lminute = divtime % mOfMinute > 0 ?
divtime / mOfMinute + 1 : divtime / mOfMinute;
return Long.valueOf(lminute).intValue();
}
/**
* 獲取指定時間的那天 00:00:00.000 的時間
*
* @param date
* @return
*/
public static Date dayBegin(final Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
/**
* 獲取指定時間的那天 23:59:59.999 的時間
*
* @param date
* @return
*/
public static Date dayEnd(final Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}
/***************************************************************************
* 根據年月日得到Date類型時間
*
* @author
* @param year
* @param month
* @param day
* @return Date
*/
public static Date getTime(Integer year, Integer month, Integer day) {
Calendar cal = Calendar.getInstance();
if (year != null)
cal.set(Calendar.YEAR, year);
if (month != null)
cal.set(Calendar.MONTH, month - 1);
if (day != null)
cal.set(Calendar.DAY_OF_MONTH, day);
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();
}
/***************************************************************************
* 得到傳入日期所在月的開始時間,如果傳入日期爲null,則表示當前日期n天后的日期
*
* @author
* @param dt
* @return Date
*/
public static Date getMonthBeginTime(Date dt) {
if (dt == null)
dt = new Date(System.currentTimeMillis());
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
cal.set(Calendar.DAY_OF_MONTH, 1);
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();
}
/***************************************************************************
* 得到傳入日期所在月的結束時間,如果傳入日期爲null,則表示當前日期n天后的日期
*
* @author
* @param dt
* @return Date
*/
public static Date getMonthEndTime(Date dt) {
if (dt == null)
dt = new Date(System.currentTimeMillis());
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, -1);
return cal.getTime();
}
/**
* 指定日期獲取本週上週
* @param today
* @param week week爲推遲的週數,0本週,-1向前推遲一週,1下週,依次類推
* @param end true 返回週一 false 返回週日
* @return
*/
public static Date getDayToWeek(Date today, int week, boolean end) {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(today);
cal.add(Calendar.DATE, week * 7);
if (end) {
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.DAY_OF_MONTH, -1);
} else {
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
}
return cal.getTime();
}
/**
* 本週上週
* @param week week爲推遲的週數,0本週,-1向前推遲一週,1下週,依次類推
* @param end true 返回週一 false 返回週日
* @return
*/
public static Date getToWeek(int week, boolean end) {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.add(Calendar.DATE, week * 7);
if (end) {
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.DAY_OF_MONTH, -1);
} else {
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
}
return cal.getTime();
}
/**
* 返回兩個時間之間的天數差(直接相減不計小時)
* 時間爲"20181231"格式
* @return
*/
public static Integer subDay (String bigTime, String smallTime) {
try {
DateFormat format = new SimpleDateFormat("yyyyMMdd");
Date time1 = format.parse(bigTime);
Date time2 = format.parse(smallTime);
BigDecimal decimal1 = new BigDecimal(time1.getTime());
BigDecimal decimal2 = new BigDecimal(time2.getTime());
//86400000 爲一天時間
return decimal1.subtract(decimal2).divide(new BigDecimal(86400000)).intValue();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 返回給定時間的小時(0-23)
* @return
*/
public static Integer getHour(String time) {
try {
DateFormat format = new SimpleDateFormat("yyyyMMdd HH");
Date date = format.parse(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}