首页>代码>java自己写的一个系统监控代码>/monitoring/src/DAO/Sys/DateFunc.java
package DAO.Sys;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateFunc {/**
	* AM/PM
	*/
	public static final String AM_PM = "a";
	/**
	* 涓�釜���绗��澶�	*/
	public static final String DAY_IN_MONTH = "dd";
	/**
	* 涓�勾�����ぉ
	*/
	public static final String DAY_IN_YEAR = "DD";
	/**
	* 涓�������ぉ(Sunday,...)
	*/
	public static final String DAY_OF_WEEK = "EEEE";
	/**
	* 浠ュぉ涓哄�浣�	*/
	public static final int DIFF_DAY = Calendar.DAY_OF_MONTH;
	/**
	* 浠ュ��朵负���
	*/
	public static final int DIFF_HOUR = Calendar.HOUR_OF_DAY;
	/**
	* 浠ユ�绉�负���
	*/
	public static final int DIFF_MILLSECOND = Calendar.MILLISECOND;
	/**
	* 浠ュ���负���
	*/
	public static final int DIFF_MINUTE = Calendar.MINUTE;
	/**
	* 浠ユ�浠戒负���锛���ф���0澶╄�绠�	*/
	public static final int DIFF_MONTH = Calendar.MONTH;
	/**
	* 浠ョ�涓哄�浣�	*/
	public static final int DIFF_SECOND = Calendar.SECOND;
	/**
	* 浠ユ���负���锛���ф����7澶╄�绠�	*/
	public static final int DIFF_WEEK = Calendar.WEEK_OF_MONTH;
	/**
	* 浠ュ勾涓哄�浣�����姣�勾365澶╄�绠�	*/
	public static final int DIFF_YEAR = Calendar.YEAR;
	/**
	* ��ぉ�����0-11)
	*/
	public static final String HOUR_IN_APM = "KK";
	/**
	* 涓�ぉ�����0-23)
	*/
	public static final String HOUR_IN_DAY = "HH";
	/**
	* ��ぉ�����1-12)
	*/
	public static final String HOUR_OF_APM = "hh";
	/**
	* 涓�ぉ�����1-24)
	*/
	public static final String HOUR_OF_DAY = "kk";

	/**
	* 骞����)
	*/
	public static final String LONG_YEAR = "yyyy";
	/**
	* 姣��
	*/
	public static final String MILL_SECOND = "SSS";
	/**
	* ���
	*/
	public static final String MINUTE = "mm";
	/**
	* ��	*/
	public static final String MONTH = "MM";
	/**
	* 绉�	*/
	public static final String SECOND = "ss";
	/**
	* 骞�浜��)
	*/
	public static final String SHORT_YEAR = "yy";
	/**
	* 涓�釜���绗����	*/
	public static final String WEEK_IN_MONTH = "W";
	/**
	* 涓�勾������
	*/
	public static final String WEEK_IN_YEAR = "ww";
	
	private static Calendar localTime; 
	
	  private static int x;                  // �ユ�灞��锛�勾   
	  private static int y; 
	
	//璁剧疆�堕��跺�
	static{ 
	TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
	}
	/**
	* 妫������堕����宸茶�杩���堕��煎�涓���存��垮害
	* <p>
	* �ㄤ��ゅ�褰�����宸茬�瓒��
	* 
	* @param destDate ����堕�锛����负褰���堕�
	* @param sourceDate 婧���达�涓��涓轰�浠朵骇�����	* @param type �堕�璁$����锛�负�������剁�
	* @param elapse ��画�堕��垮害
	* @return ���瓒��
	* @throws CodedException
	*/
	public static boolean compareElapsedTime(
	Date destDate,
	Date sourceDate,
	int type,
	int elapse)
	throws RuntimeException {
	if (destDate == null || sourceDate == null)
	throw new RuntimeException("compared date invalid");

	return destDate.getTime() > getRelativeDate(sourceDate, type, elapse).getTime();
	}

	/**
	* �������村�绗�覆
	* <p>
	* �堕�瀛��涓叉�寮�负锛�勾(4浣�-��唤(2浣�-�ユ�(2浣� 灏��(2浣�:���(2浣�:绉�2浣�
	* 
	* @return �堕�瀛��涓�	*/
	public static String getCurrentDateString() {
	return getCurrentDateString("yyyy-MM-dd HH:mm:ss");
	}
	
	/** 
     * ���锛���板����浠芥����煎�涓猴�xxxx-yy-zz (eg: 2007-12-01) 
     * @return String 
     * @author pure 
     */ 
   public static String getMonthStart(int year,int month) {   
       String strY = null;   
       x = year;   
       y = month;   
       strY = y >= 10 ? String.valueOf(y) : ("0" + y);return x + "-" + strY + "-01";   
   }   
	
	 public static String getMonthEnd(int year,int month) {   
	        String strY = null;   
	        String strZ = null;   
	        boolean leap = false;   
	        x = year;   
	        y =  month;   
	        if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {   
	            strZ = "31";   
	        }   
	        if (y == 4 || y == 6 || y == 9 || y == 11) {   
	            strZ = "30";   
	        }   
	        if (y == 2) {   
	            leap = leapYear(x);   
	            if (leap) {   
	                strZ = "29";   
	            }else {   
	                strZ = "28";   
	            }   
	        }   
	        strY = y >= 10 ? String.valueOf(y) : ("0" + y);   
	        return x + "-" + strY + "-" + strZ;   
	    }   
	   public static boolean leapYear(int year) {   
	        boolean leap;   
	        if (year % 4 == 0) {   
	            if (year % 100 == 0) {   
	                if (year % 400 == 0) leap = true;   
	                    else leap = false;   
	                }   
	            else leap = true;   
	        }   
	        else leap = false;   
	        return leap;   
	    }     
	 
	
	/**
	* ���寮��褰���堕�瀛��涓�	* 
	* @param formatString �煎�瀛��涓�	* @return
	*/
	public static String getCurrentDateString(String formatString) {

	Date currentDate = new Date();
	return getDateString(currentDate, formatString);
	}

	/**
	* ���澶╁�涓�������ぉ
	* @return
	*/
	public static int getCurrentDayOfWeek() {
	return getDayOfWeek(new Date());
	}
	/**
	* 
	* @Enclosing_Method: getCurrentDate
	* @Written by: liuxmi
	* @Creation Date: Jun 9, 2010 7:31:50 AM 
	* @version: v1.00
	* @Description:�峰�褰���堕� 
	* @return
	* @return Date
	*
	*/
	public static Date getCurrentDate() {
	return getDateFromString(getDateString(new Date(), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
	}
	/**
	* 
	* @Enclosing_Method: getDate
	* @Written by: liuxmi
	* @Creation Date: Jun 9, 2010 7:32:11 AM 
	* @version: v1.00
	* @Description: �ユ��煎���	* @param date
	* @return
	* @return Date
	*
	*/
	public static Date getDate(Date date) {
	return getDateFromString(getDateString(date, "yyyy-MM-dd"), "yyyy-MM-dd");
	}

	/**
	* �规��堕�瀛��涓茬������	* 
	* @param dateString �堕�瀛��涓叉�寮�	* @return �堕�
	* @throws RuntimeException
	*/
	public static Date getDateFromString(String dateString)
	throws RuntimeException {
	return getDateFromString(dateString, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	* �规��堕�瀛��涓茬������	* 
	* @param dateString �堕�瀛��涓叉�寮�	* @return �堕�
	* @throws RuntimeException
	*/
	public static Date getDateFromStringNew(String dateString)
	throws RuntimeException {
		dateString=dateString.substring(0, 10);
	return getDateFromString(dateString, "yyyy-MM-dd");
	}

	/**
	* �规�瀛��涓茬������	* 
	* @param dateString �堕�瀛��涓�	* @param pattern �堕�瀛��涓叉�寮��涔�	* @return �堕�
	* @throws RuntimeException
	*/
	public static Date getDateFromString(String dateString, String pattern)
	throws RuntimeException {
	SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
	Date date = null;

	try {
	date = dateFormat.parse(dateString);
	} catch (java.text.ParseException e) {
	throw new RuntimeException(
	"parse date string '"
	+ dateString
	+ "' with pattern '"
	+ pattern
	+ "' failed: "
	+ e.getMessage());
	}

	return date;
	}

	/**
	* ����村�绗�覆
	* 
	* @param date �堕�
	* @return �堕�瀛��涓�	*/
	public static String getDateString(Date date) {
	return getDateString(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	* ����村�绗�覆
	* 
	* @param date �堕�
	* @param formatString 杞���煎�
	* @return �堕�瀛��涓�	*/
	public static String getDateString(Date date, String formatString) {
	return getDateString(date, formatString, Locale.PRC);
	}

	/**
	* ����村�绗�覆
	* 
	* @param date �堕�
	* @param formatString 杞���煎�
	* @param locale �板�
	* @return �堕�瀛��涓�	*/
	public static String getDateString(Date date, String formatString, Locale locale) {
	if (date == null)
	return null;
	SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
	return dateFormat.format(date);
	}


	/**
	* ������涓�������ぉ
	* 
	* @param date �ユ�
	* @return
	*/
	public static int getDayOfWeek(Date date) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);

	return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	* ������涓�������ぉ
	* 
	* @param date �ユ�
	* @return
	*/
	public static int getDayOfMonth(Date date) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);

	return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	* ���涓�����澶уぉ��	* 
	* @param date �ユ�
	* @return
	*/
	public static int getDaysOfMonth(Date date) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);

	return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	* �������ㄦ�浠界���ぇ澶╂�
	* 
	* @param date �ユ�
	* @return
	*/
	public static int getMaximumDay(Date date) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);

	return calendar.getMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	* �规�婧���村��堕�璁$�����堕�
	* 
	* @param date 婧����	* @param type �堕����
	* @param relate �堕�
	* @return ����堕�
	*/
	public static Date getRelativeDate(Date date, int type, int relate) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	calendar.add(type, relate);

	return calendar.getTime();
	}

	/**
	* �规�褰���堕�����胯�绠�������	* 
	* @param type �堕����
	* @param relate �堕�
	* @return ����堕�
	*/
	public static Date getRelativeDate(int type, int relate) {
	Date current = new Date();

	return getRelativeDate(current, type, relate);
	}

	/**
	* �规�褰���堕�����跨��������村�绗�覆
	* 
	* @param type �堕����
	* @param relate �堕�
	* @param formatString �堕��煎�
	* @return �堕�瀛��涓�	*/
	public static String getRelativeDateString(
	int type,
	int relate,
	String formatString) {
	return getDateString(getRelativeDate(type, relate), formatString);
	}

	public static Date getStartDate(Date date) {
	return getDateFromString(getDateString(date, "yyyyMMdd") + "00:00:00", "yyyyMMddHH:mm:ss");
	}

	public static Date getEndDate(Date date) {
	return getDateFromString(getDateString(date, "yyyyMMdd") + "23:59:59", "yyyyMMddHH:mm:ss");
	}

	/**
	* ����存�瀛��涓�	* 
	* @param date �堕�
	* @return �堕��冲�绗�覆
	*/
	public static String getTimestampString(Date date) {
	return getDateString(date, "yyyyMMddHHmmssSSS");
	}

	/**
	* ���澶╂����
	* 
	* @return �ユ�����板�
	*/
	public static int getToday() {
	return Integer.parseInt(getCurrentDateString("dd"));
	}

	public static long getTimeDiff(Date fromDate, Date toDate, int type) {
	fromDate = (fromDate == null) ? new Date() : fromDate;
	toDate = (toDate == null) ? new Date() : toDate;
	long diff = toDate.getTime() - fromDate.getTime();

	switch(type) {
	case DIFF_MILLSECOND:
	break;

	case DIFF_SECOND:
	diff /= 1000;
	break;

	case DIFF_MINUTE:
	diff /= 1000 * 60;
	break;

	case DIFF_HOUR:
	diff /= 1000 * 60 * 60;
	break;

	case DIFF_DAY:
	diff /= 1000 * 60 * 60 * 24;
	break;

	case DIFF_MONTH:
	diff /= 1000 * 60 * 60 * 24 * 30;
	break;

	case DIFF_YEAR:
	diff /= 1000 * 60 * 60 * 24 * 365;
	break;

	default:
	diff = 0;
	break;
	}

	return diff;
	}

	/**
	* 姣���堕��虫������	* 
	* @param arg0 �堕�
	* @param arg1 �堕�
	* @return ����稿�
	*/
	public static boolean isTimestampEqual(Date arg0, Date arg1) {
	return getTimestampString(arg0).compareTo(getTimestampString(arg1)) == 0;
	}

	/**
	* 
	* @Enclosing_Method: nDaysAfterNowDate
	* @Written by: liuxmi
	* @Creation Date: May 25, 2010 6:11:01 AM 
	* @version: v1.00
	* @Description: 褰���ユ����n澶╁������	* @param n
	* @return
	* @return Date
	*
	*/
	public static Date nDaysAfterNowDate(int n) { 
	Calendar rightNow = Calendar.getInstance(); 
	rightNow.add(Calendar.DAY_OF_MONTH,+n); 
	return rightNow.getTime(); 
	} 

	/**
	* 
	* @Enclosing_Method: nDaysAfterOneDateString
	* @Written by: liuxmi
	* @Creation Date: May 25, 2010 6:12:37 AM 
	* @version: v1.00
	* @Description: 缁��涓�釜�ユ����绗�覆锛�������澶╁�������瀛��涓�
	* @param basicDate
	* @param n
	* @return
	* @return String
	*
	*/ 
	public static String nDaysAfterOneDateString(String basicDate,int n) { 
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
	Date tmpDate = null; 
	try { 
	tmpDate = df.parse(basicDate); 
	} 
	catch(Exception e){ 
	System.out.println("dateformat:"+e.getMessage());
	} 
	long nDay=(tmpDate.getTime()/(24*60*60*1000)+1+n)*(24*60*60*1000); 
	tmpDate.setTime(nDay); 

	return df.format(tmpDate); 
	} 

	/**
	* 
	* @Enclosing_Method: nDaysAfterOneDate
	* @Written by: liuxmi
	* @Creation Date: May 25, 2010 6:13:45 AM 
	* @version: v1.00
	* @Description: 缁��涓�釜�ユ�锛�������澶╁������
	* @param basicDate
	* @param n
	* @return
	* @return Date
	*
	*/
	public static Date nDaysAfterOneDate(Date basicDate,int n) { 
	long nDay=(basicDate.getTime()/(24*60*60*1000)+n)*(24*60*60*1000); 
	basicDate.setTime(nDay); 
	return basicDate; 
	} 

	/**
	* 
	* @Enclosing_Method: nDaysBetweenTwoDate
	* @Written by: liuxmi
	* @Creation Date: May 25, 2010 6:14:10 AM 
	* @version: v1.00
	* @Description: 璁$�涓や釜�ユ��搁���ぉ��	* @param firstDate
	* @param secondDate
	* @return
	* @return int
	*
	*/ 
	public static int nDaysBetweenTwoDate(Date firstDate,Date secondDate) { 
	int nDay=(int)((secondDate.getTime()-firstDate.getTime())/(24*60*60*1000)); 
	return nDay; 
	} 
	/**
	* 
	* @Enclosing_Method: nYearsBetweenTwoDate
	* @Written by: liuxmi
	* @Creation Date: May 25, 2010 6:56:55 AM 
	* @version: v1.00
	* @Description: 璁$�涓や釜�ユ��搁���勾��	* @param firstDate
	* @param secondDate
	* @return
	* @return int
	*
	*/
	public static int nYearsBetweenTwoDate(Date firstDate,Date secondDate) { 
	int nYear=nDaysBetweenTwoDate(firstDate, secondDate)/365+1; 
	return nYear; 
	} 
	/**
	* 
	* @Enclosing_Method: nDaysBetweenTwoDate
	* @Written by: liuxmi
	* @Creation Date: May 25, 2010 6:32:15 AM 
	* @version: v1.00
	* @Description: 璁$�涓や釜�ユ��搁���ぉ��	* @param firstString
	* @param secondString
	* @return
	* @return int
	*
	*/ 
	public static int nDaysBetweenTwoDate(String firstString,String secondString) { 
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
	Date firstDate=null; 
	Date secondDate=null; 
	try { 
	firstDate = df.parse(firstString); 
	secondDate=df.parse(secondString); 
	} 
	catch(Exception e) { 
	System.out.println("dateformat:"+e.getMessage()); 
	} 
	int nDay=(int)((secondDate.getTime()-firstDate.getTime())/(24*60*60*1000)); 
	return nDay; 
	} 
	/**
	* 
	* @Enclosing_Method: getSundayOneDate
	* @Written by: liuxmi
	* @Creation Date: May 28, 2010 1:36:06 AM 
	* @version: v1.00
	* @Description: �峰�缁���ユ����������涓�ぉ
	* @param date
	* @return void
	*
	*/
	public static Date getFirstOfWeekOneDate(Date date){
	int day = DateFunc.getDayOfWeek(date);
	Date sunDay = DateFunc.getRelativeDate(date, DateFunc.DIFF_DAY,-(day-1) );
	return getDate(sunDay);
	}
	/**
	* 
	* @Enclosing_Method: getWeeksOfYear
	* @Written by: liuxmi
	* @Creation Date: May 28, 2010 4:02:48 AM 
	* @version: v1.00
	* @Description: 杩��缁���ユ��ㄦ�涓�勾涓��绗����	* @param date
	* @return
	* @return int
	*
	*/
	
	
	
	public static int getWeeksOfYear(Date date) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	return calendar.get(Calendar.WEEK_OF_YEAR);
	}
	
	

}
最近下载更多
caomin  LV4 2023年5月25日
2089675149  LV7 2023年2月17日
wddlhysss  LV5 2021年3月11日
落轩昂  LV12 2020年11月30日
wangshixi2010  LV3 2020年8月21日
wangmeicong  LV12 2020年7月15日
zuiwomengjiaren  LV11 2019年10月9日
yongyuan0230  LV4 2019年7月26日
xuanshao940  LV2 2019年7月11日
13072368302  LV8 2019年7月2日
最近浏览更多
shaohuaqingfu  LV3 11月6日
escape1023 2月13日
暂无贡献等级
3334004690  LV10 2023年11月1日
edpwyg  LV14 2023年10月21日
钱小小  LV3 2023年7月16日
caomin  LV4 2023年5月25日
qiuyuqiuyuqiuyu 2023年5月25日
暂无贡献等级
matintalorr  LV10 2023年5月9日
weixiao  LV6 2023年5月7日
2089675149  LV7 2023年2月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友