1. 程式人生 > >java常用的日期函式

java常用的日期函式

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DataUtil {
	
	/**
	 * 返回當前日期所在月份的天數
	 */
	public static List<Date> getAllTheDateOftheMonth(Date date) {
		List<Date> list = new ArrayList<Date>();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		int month = cal.get(Calendar.MONTH);
		while (cal.get(Calendar.MONTH) == month) {
			list.add(cal.getTime());
			cal.add(Calendar.DATE, 1);
		}
		return list;
	}
	
	/**
	 * 獲取前XX天的時間
	 */
	public static Date getPastDate(int pastDay) throws ParseException {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - pastDay);
		Date today = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String result = sdf.format(today);
		Date date = sdf.parse(result);
		return date;
	 }
	
	/**
	 * 根據時間(變數)獲取未來XX天的時間
	 */
	public static Date getFutureDate(Date thisDay, int futureDay) throws ParseException {
		Calendar c = Calendar.getInstance();
		c.setTime(thisDay);
		c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + futureDay);
		Date future = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String result = sdf.format(future);
		Date date = sdf.parse(result);
		return date;
	}
	
	/**
	 * 根據系統當前時間獲取未來XX天的時間
	 */
	public static Date getFutureDate(int futureDay) throws ParseException {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + futureDay);
		Date today = c.getTime(); // 系統當前時間
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String result = sdf.format(today);
		Date date = sdf.parse(result);
		return date;
	}/**
	
* 獲取該日期的週一 * @param date * @return */ public static Date getThisWeekMonday(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); // 獲得當前日期是一個星期的第幾天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK); if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } // 設定一個星期的第一天,按中國的習慣一個星期的第一天是星期一 cal.setFirstDayOfWeek(Calendar.MONDAY); // 獲得當前日期是一個星期的第幾天 int day = cal.get(Calendar.DAY_OF_WEEK); // 根據日曆的規則,給當前日期減去星期幾與一個星期第一天的差值 cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
Date param = cal.getTime(); String result = sdf.format(param) + " 00:00:00"; try { return sdf2.parse(result); } catch (ParseException e) { LoggerConfig.systemLogger.error("日期格式轉換異常", e); } return null; } /** * 獲取前XX分鐘的時間 */ public static Date getPastMinute(Date newDate, int pastMinute) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calendar.getInstance(); c.setTime(newDate); c.set(c.get(Calendar.YEAR), (c.get(Calendar.MONTH)), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), (c.get(Calendar.MINUTE) - pastMinute), c.get(Calendar.SECOND)); Date past = c.getTime(); String result = sdf.format(past); return sdf.parse(result); } /** * 獲取未來XX小時後的時間 */ public static Date getFurterByHour(Date date, int hours) throws ParseException{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calendar.getInstance(); int fHour = c.get(Calendar.HOUR_OF_DAY) + hours; c.setTime(date); c.set(c.get(Calendar.YEAR),  c.get(Calendar.MONTH),  c.get(Calendar.DAY_OF_MONTH),  fHour,  c.get(Calendar.MINUTE),  c.get(Calendar.SECOND)); Date further = c.getTime(); String result = sdf.format(further); return sdf.parse(result); } /** * 獲取當前日期月份的第一天 */ public static Date getFirstDayByMonth(Date date) {     Calendar c = Calendar.getInstance();     c.setTime(date);     c.add(Calendar.MONTH, 0);     c.set(Calendar.DAY_OF_MONTH, 1);     return c.getTime();     } }