`
农村哥们
  • 浏览: 295257 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java 对日期的运算操作

阅读更多
package com.ilantu.common.date;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtil implements Serializable{
	
	public static final TimeZone NewYorkTimeZone = TimeZone.getTimeZone("America/New_York");
	public static final TimeZone CSTTimeZone = TimeZone.getTimeZone("CST");
	public static final TimeZone GMTTimeZone = TimeZone.getTimeZone("GMT");
	public static final TimeZone HKTimeZone = TimeZone.getTimeZone("Asia/Hong_Kong");
	public static final TimeZone SHTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
	public static final TimeZone KRCHTimeZone = TimeZone.getTimeZone("Asia/Karachi");
	public static final TimeZone PRCTimeZone = TimeZone.getTimeZone("PRC");
	public static final TimeZone CTTTimeZone = TimeZone.getTimeZone("CTT");
	public static final TimeZone MSTTimeZone = TimeZone.getTimeZone("MST");
	public static final TimeZone PSTTimeZone = TimeZone.getTimeZone("PST");
	public static final TimeZone ASTTimeZone = TimeZone.getTimeZone("AST");
	public static final TimeZone ESTTimeZone = TimeZone.getTimeZone("EST");
	
	/**
	 * Java Default DateFormat string for Application.
	 */
	public static final String DATEFORMAT = "EEE MMM dd hh:mm:ss z yyyy";
	/**
	 * Default Dateformat string for System Administration
	 */
	public static final String DEFAULT_ADMIN_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";
	/**
	 * Default Dateformat string for System
	 */
	public static final String DEFAULT_SYSTEM_DATE_FORMAT="yyyy-MM-dd HH:mm:ss.SSS";
	/**
	 * Default Dateformat string for Bisiness
	 */
	public static final String DEFAULT_BIZ_DATE_FORMAT="yyyy-MM-dd";
	
	/**
	 * Default FSCS Date formatter
	 */
	public static final SimpleDateFormat DATEFORMATTER = new SimpleDateFormat(DATEFORMAT);

	/**
	 * Basic System Date formatter for innternal transfer.but not for presentation
	 */
	public static final SimpleDateFormat SYSTEM_DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	/**
	 * basic FSCS Date formatter.
	 */
	public static final SimpleDateFormat FSCS_DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	/**
	 * Default FSCS Business Date formatter
	 */
	public static final SimpleDateFormat FSCS_BIZ_DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * Basic FSCS Short Date formatter
	 */
	public static final SimpleDateFormat FSCS_SHORT_DATE_FORMATTER = new SimpleDateFormat("yy-MM-dd");
	/**
	 * Basic FSCS Admin Date formatter
	 */														
	public static final SimpleDateFormat FSCS_ADMIN_DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * YYYYMMDD FSCS Date formatter
	 */
	public static final SimpleDateFormat YYYYMMDD_FORMATTER = new SimpleDateFormat("yyyyMMdd");

	/**
	 * Short YYYYMMDD FSCS Date formatter
	 */
	public static final SimpleDateFormat YYMMDD_FORMATTER = new SimpleDateFormat("yyMMdd");
	/**
	 * Full Date format as yyyy-MM-dd HH:mm:ss
	 */
	public static final SimpleDateFormat YYYY_MM_DD_HH_MM_SS_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat YYYYMMDDHHMMSS_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	
	  
	/**
	 * RMS limit Date formatter?
	 */
	public static final SimpleDateFormat YYYY_MMM_DD_HH_MM_SS_FORMATTER = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
	/**
	 * YYYYMMMDD  FSCS Date formatter
	 */	
	public static final SimpleDateFormat YYYY_MMM_DD_FORMATTER = new SimpleDateFormat("yyyy-MMM-dd");

	/**
	 * DDMMMYYYY FSCS Date formatter
	 */
	public static final SimpleDateFormat DD_MMM_YYYY_FORMATTER = new SimpleDateFormat("dd-MMM-yyyy");

	/**
	 * in the future, the param will be put into config.xml
	 */
	private static String currentDbms = "ORACLE";
	/**
	 * get GMT mean Time.
	 * @return java.util.Date
	 * @deprecated
	 */
	public static final Date getGMTTimeStamp() {
		/*long zoneOffset = fromZone.getRawOffset() - GMTTimeZone.getRawOffset();
		return new Date((date.getTime() - zoneOffset));*/		
		return toDate(new Date(), DateUtil.GMTTimeZone);
	}
	
	/**
	 * get GMT mean Time.
	 * @return java.sql.Timestamp
	 */
	public static final Date getGMTTimestamp() {
		return toDate(new Date(), DateUtil.GMTTimeZone);
	}

	public static final Timestamp getGMTSqlTimestamp() {
		return new java.sql.Timestamp(getGMTTimestamp().getTime());
	}


	public static final Date toDate(Date date, TimeZone timeZone) {
		Date dt = null;
		try {
			String ds = toString(date, timeZone);
			dt = FSCS_DATE_FORMATTER.parse(ds);
		} catch (ParseException ex) {
			
		}
		return dt;
	}


	public static final Date toDate(Date date, TimeZone fromZone, TimeZone toZone) {
		long zoneOffset = fromZone.getRawOffset() - toZone.getRawOffset();
		return new Date((date.getTime() - zoneOffset));
	}

	public static final Date toDate(String dateStr) {
		Date date = null;
		if(dateStr==null||dateStr.trim().length()==0){
			return null;
		}
		try {
			date = SYSTEM_DATE_FORMATTER.parse(dateStr);
		} catch (ParseException ex) {
			
		}
		if(date==null){
			try {
				date = FSCS_ADMIN_DATE_FORMATTER.parse(dateStr);
			} catch (ParseException ex) {
				
			}			
		}
		if(date==null)	{
			try {
				Locale loc=null;
				loc=Locale.getDefault();
				SimpleDateFormat sysFormatter=(SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,loc);
				date = sysFormatter.parse(dateStr);
			} catch (ParseException ex) {
				
			}
		}
		return date;
	}
	

	public static final Date toDate(String dateStr,String format) {
		Date date = null;
		if(dateStr==null||dateStr.trim().length()==0){
			return null;
		}
		if(format==null||format.trim().length()==0){
			format=DEFAULT_ADMIN_DATE_FORMAT;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		try {
			date = formatter.parse(dateStr);
		} catch (ParseException ex) {
			
		}
		if(date==null){
			formatter = new SimpleDateFormat(DEFAULT_SYSTEM_DATE_FORMAT);
			try {
				date = formatter.parse(dateStr);
			} catch (ParseException ex) {
				
			}			
		}
		if(date==null)	{
			try {
				Locale loc=null;
				loc=Locale.getDefault();
				SimpleDateFormat sysFormatter=(SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,loc);
				date = sysFormatter.parse(dateStr);
			} catch (ParseException ex) {
				
			}
		}
		return date;
	}
	

	public static final Date toDate(String dateStr,SimpleDateFormat formatter) {
		Date date = null;
		if(dateStr==null||dateStr.trim().length()==0){
			return null;
		}
		try {
			date = formatter.parse(dateStr);
		} catch (ParseException ex) {
			
		}
		if(date==null)	{
			try {
				Locale loc=null;
				loc=Locale.getDefault();
				SimpleDateFormat sysFormatter=(SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,loc);
				date = sysFormatter.parse(dateStr);
			} catch (ParseException ex) {
				
			}
		}
		return date;
	}
		

	public static final Date toBizDate(String dateStr){
		Date date = null;
		if(dateStr==null||dateStr.trim().length()==0){
			return null;
		}		
		try {
			date = FSCS_BIZ_DATE_FORMATTER.parse(dateStr);
		} catch (ParseException ex) {
			
		}
		if(date==null)	{
			try {
				Locale loc=null;
				loc=Locale.getDefault();
				SimpleDateFormat sysFormatter=(SimpleDateFormat)DateFormat.getDateInstance(DateFormat.MEDIUM,loc);
				date = sysFormatter.parse(dateStr);
				
			} catch (ParseException ex) {
				
			}
		}
		return date;		
	}

	public static final Date toShortDate(String dateStr) {
		Date date = null;
		if(dateStr==null||dateStr.trim().length()==0){
			return null;
		}		
		try {
			date = FSCS_SHORT_DATE_FORMATTER.parse(dateStr);
		} catch (ParseException ex) {
			
		}
		if(date==null)	{
			try {
				Locale loc=null;
				loc=Locale.getDefault();
				SimpleDateFormat sysFormatter=(SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT,loc);
				date = sysFormatter.parse(dateStr);
			} catch (ParseException ex) {
				
			}
		}
		return date;
	}

	public static final Date toSqlTimestamp(String dateStr) {
		return new java.sql.Timestamp(toDate(dateStr).getTime());	
	}
	public static Date toSqlTimestamp(String dateStr,String format) {
		return new java.sql.Timestamp(toDate(dateStr,format).getTime());	
	}

	public static String toString(Date date) {
		return FSCS_ADMIN_DATE_FORMATTER.format(date);
	}
	

	public static final String toString(Date date, String format) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		formatter.setCalendar(calendar);
		return formatter.format(date);
	}

	public static final String toString(Date date, SimpleDateFormat formatter) {
		Calendar calendar = Calendar.getInstance();
		formatter.setCalendar(calendar);
		return formatter.format(date);
	}	


	public static final String toString(Date date, TimeZone timeZone) {
		return toString(date, timeZone, SYSTEM_DATE_FORMATTER);
	}
	

	public static final String toString(Date date, TimeZone timeZone, String format) {
		Calendar calendar = Calendar.getInstance(timeZone);
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		formatter.setCalendar(calendar);
		return formatter.format(date);
	}	

	public static String toString(Date date, TimeZone timeZone,	SimpleDateFormat formatter) {
		Calendar calendar = Calendar.getInstance(timeZone);
		formatter.setCalendar(calendar);
		return formatter.format(date);
	}

	public static final String toBizString(Date date) {
		return FSCS_BIZ_DATE_FORMATTER.format(date);
	}
	

	public static String toBizString(Date date,String format) {
		String dateStr = null;
		if(format==null||format.trim().length()==0){
			format=DEFAULT_BIZ_DATE_FORMAT;
		}		
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		dateStr = formatter.format(date);
		return dateStr;
	}			

	public static final String toShortString(Date date) {
		return FSCS_SHORT_DATE_FORMATTER.format(date);
	}	
			


	public static final int calculateDays(Date date1, Date date2) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(date1);
		c2.setTime(date2);
		long days = (c1.getTime().getTime() - c2.getTime().getTime() + (60*60*1000L))/(60*60*1000L*24);
		return (new Long(days)).intValue(); 
	}
	

	public static final boolean isBetweenDates(Date date1,Date date2,Date date){
		if(date.getTime()<=date2.getTime() && date.getTime()>=date1.getTime()){
			return true;
		}
		else{
			return false;
		}
	}

	public static final boolean isBetweenDates2(Date date1,Date date2,Date date){
		if(date.getTime()<date2.getTime() && date.getTime()>date1.getTime()){
			return true;
		}
		else{
			return false;
		}
	}	

	public static final boolean isWeekend(Date date) {
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		if ((cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
			|| (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)) {
			return true;
		}
		return false;
	}

	public static final boolean isWeekday(Date date) {
		return !isWeekend(date);
	}
	

	public static final boolean isBeforeByDay(Date date1, Date date2) {
		int i = compareByDay(date1, date2);
		if (i < 0)
			return true;
		return false;
	}


	public static final boolean isAfterByDay(Date date1, Date date2) {
		int i = compareByDay(date1, date2);
		if (i > 0)
			return true;
		return false;
	}


	public static final boolean isOnSameDay(Date date1, Date date2) {
		int i = compareByDay(date1, date2);
		if (i == 0)
			return true;
		return false;
	}


	public static final int compareByDay(Date date1, Date date2) {
		String d1 = YYYYMMDD_FORMATTER.format(date1);
		String d2 = YYYYMMDD_FORMATTER.format(date2);
		int retVal = 0;
		try {
			int dateInt1 = Integer.parseInt(d1);
			int dateInt2 = Integer.parseInt(d2);
			if (dateInt1 > dateInt2) {
				retVal = 1;
			} else if (dateInt1 < dateInt2) {
				retVal = -1;
			}

		} catch (Exception e) {
			
		}
		return retVal;
	}
	
	public static final int compareByDateTime(Date date1, Date date2) {
		long d1=date1.getTime();
		long d2=date2.getTime();
		int retVal = 0;
		if (d1 > d2) {
			retVal = 1;
		} else if (d1 < d2) {
			retVal = -1;
		}
		return retVal;
	}
	
	public static final int compareByDateTime2(Date date1, Date date2) {
		long d1=date1.getTime()/1000;
		long d2=date2.getTime()/1000;
		int retVal = 0;
		if (d1 > d2) {
			retVal = 1;
		} else if (d1 < d2) {
			retVal = -1;
		}
		return retVal;
	}	
	
	public static final int compareByDate(Date date1, Date date2) {
		int d=calculateDays(date1,date2);
		if(d>0){
			return 1;
		}
		else if(d<0){
			return -1;
		}
		return d;
	}	

	public static final Date toDateWithoutMs(java.util.Date date){
		return new Date((date.getTime()/1000)*1000);
	}
	public static final Date toDateWithoutTime(java.util.Date date){
		//System.out.println("toDateWithoutTime:1"+new Date((date.getTime()/1000/60/60/24)*1000*60*60*24));
		return toBizDate(toBizString(date));
	}	

	public static final Date toGMTTimestamp(java.util.Date localDate){
		TimeZone localTimeZone = SHTimeZone;
		return DateUtil.toDate(localDate,localTimeZone,DateUtil.GMTTimeZone);
	}


	public static final Date toGMTTimestamp(java.sql.Timestamp localTimestamp){
		TimeZone localTimeZone = SHTimeZone;
		return DateUtil.toDate(localTimestamp,localTimeZone,DateUtil.GMTTimeZone);
	}

	public static final Timestamp toGMTSqlTimestamp(java.util.Date localDate){
		//TimeZone localTimeZone = ((UserProfile)(MyObjects.getInstance().getObject("WAF_USERPROFILE"))).getUser().getSiteTimeZone();
		return new Timestamp(toGMTTimestamp(localDate).getTime());
	}


	public static Timestamp toGMTSqlTimestamp(java.sql.Timestamp localTimestamp){
		//TimeZone localTimeZone = ((UserProfile)(MyObjects.getInstance().getObject("WAF_USERPROFILE"))).getUser().getSiteTimeZone();
		return new Timestamp(toGMTTimestamp(localTimestamp).getTime());
	}		


	public static Timestamp getNextGMTSqlTimestamp(java.util.Date localDate){
		return toGMTSqlTimestamp(getNextLocalDate(localDate));
		//return toGMTTimestamp(getNextLocalDate(new Date((localDate.getTime()/(24*60*60*1000L))*(24*60*60*1000L))));
	}


	public static final Timestamp getPreviousGMTSqlTimestamp(java.util.Date localDate){
		return toGMTSqlTimestamp(getPreviousLocalDate(localDate));
		//return toGMTTimestamp(getPreviousLocalDate(new Date((localDate.getTime()/(24*60*60*1000L))*(24*60*60*1000L))));
	}	


	public static final Date getNextGMTTimestamp(java.util.Date localDate){
		return toGMTTimestamp(getNextLocalDate(localDate));
		//return toGMTTimestamp(getNextLocalDate(new Date((localDate.getTime()/(24*60*60*1000L))*(24*60*60*1000L))));
	}


	public static final Date getPreviousGMTTimestamp(java.util.Date localDate){
		return toGMTTimestamp(getPreviousLocalDate(localDate));
		//return toGMTTimestamp(getNextLocalDate(new Date((localDate.getTime()/(24*60*60*1000L))*(24*60*60*1000L))));
	}


	public static final Date toLocalTimestamp(Timestamp gmtTimestamp){
		TimeZone localTimeZone = SHTimeZone;
		return DateUtil.toDate(gmtTimestamp,DateUtil.GMTTimeZone,localTimeZone);
	}
	


	public static final Date toLocalTimestamp(java.util.Date gmtDate){
		TimeZone localTimeZone = SHTimeZone;
		return DateUtil.toDate(gmtDate,DateUtil.GMTTimeZone,localTimeZone);
	}		


	public static final Date getNextLocalDate(Date date) {
		//TimeZone localTimeZone = ((UserProfile)(MyObjects.getInstance().getObject("WAF_USERPROFILE"))).getUser().getSiteTimeZone();
		return addDays(date,1);
	}


	public static final Date getPreviousLocalDate(Date date) {
		return addDays(date,-1);
	}	



	public static final java.sql.Date getNextLocalSqlDate(Date date) {
		return new java.sql.Date(getNextLocalDate(date).getTime());
	}



	public static final java.sql.Date getPreviousLocalSqlDate(Date date) {
		return new java.sql.Date(getPreviousLocalDate(date).getTime());
	}	



	public static final String toLocalString(Date date){
				Locale loc=null;
				loc=Locale.getDefault();		
		SimpleDateFormat formatter = (SimpleDateFormat)DateFormat.getDateInstance(DateFormat.MEDIUM,loc);
		return formatter.format(date);
		
	}	



	public static final String toLocalString(Date date,String format) {
		Locale loc=Locale.getDefault();		
		return toLocalString(date,format,loc);
	}		



	public static final String toLocalString(Date date,String format,Locale locale) {
		String dateStr = null;
		if(format==null||format.trim().length()==0){
			format=DEFAULT_BIZ_DATE_FORMAT;
		}
		Locale loc=null;
		if(locale==null){
			loc=Locale.getDefault();
		}
		else{
			loc=locale;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format,loc);
		dateStr = formatter.format(date);
		return dateStr;
	}



	public static final String toFullLocalString(Date date){
		Locale loc=Locale.getDefault();		
		if(loc==null){
			loc=Locale.getDefault();
		}
		SimpleDateFormat formatter = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,loc);
		return formatter.format(date);
		
	}



	public static final String toFullLocalString(Date date,String format,Locale locale) {
		String dateStr = null;
		if(format==null||format.trim().length()==0){
			format=DEFAULT_ADMIN_DATE_FORMAT;
		}
		Locale loc=null;
		if(locale==null){
			loc=Locale.getDefault();
		}
		else{
			loc=locale;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format,loc);
		dateStr = formatter.format(date);
		return dateStr;
	}


	public static final Date getHostDate(){
		return new Date();
	}
	public static final Date getNextHostDate(){
		return addDays(new Date(),1);
	}
	public static final Date getPreviousHostDate(){
		return addDays(new Date(),-1);
	}	



	public static final java.sql.Date getHostSqlDate(){
		return new java.sql.Date((new Date()).getTime());
	}



	public static final Date addDays(Date date,int days){
		if(date==null){
			return null;
		}		
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(Calendar.DATE, days);
		return cal.getTime();
	}

	public static final Date addWeeks(Date date,int weeks){
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(Calendar.DATE, weeks*7);
		return cal.getTime();
	}


	public static final Date addMonths(Date date,int months){
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(Calendar.MONTH, months);
		return cal.getTime();
	}


	public static final Date addQuarters(Date date,int quarters){
		if(date==null){
			return null;
		}		
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(Calendar.MONTH, quarters*3);
		return cal.getTime();
	}



	public static final Date addYears(Date date,int years){
		if(date==null){
			return null;
		}		
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(Calendar.YEAR, years);
		return cal.getTime();
	}


	public static final boolean isLeapYear(Date date){
		if(date==null){
			return false;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year=cal.get(Calendar.YEAR);
		if (year % 4 == 0) {
			if (year % 400 == 0) {
				return true;
			} else if (year % 100 != 0) {
				return true;
			}
		}
		return false;
	}


	public static final Date getFirstDayOfWeek(Date date){
		if(date==null){
			return null;
		}		
		Locale locale =Locale.getDefault();		
		//Locale locale=Locale.CHINESE;
		Calendar cal = Calendar.getInstance(locale);
		cal.setTime(date);
		int days=cal.get(Calendar.DAY_OF_WEEK);
		return addDays(date,(-1)*days+1);	
	}



	public static final Date getLastDayOfWeek(Date date){
		if(date==null){
			return null;
		}
		Locale locale =Locale.getDefault();		
		//Locale locale=Locale.CHINESE;
		Calendar cal = Calendar.getInstance(locale);
		cal.setTime(date);
		int days=cal.get(Calendar.DAY_OF_WEEK);	
		return addDays(date,7-days);
	}	



	public static final Date getFirstDayOfMonth(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE,1);
		return cal.getTime();	
	}



	public static final Date getLastDayOfMonth(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE,1);
		cal.add(Calendar.MONTH,1);		
		cal.add(Calendar.DATE,-1);
		return cal.getTime();	
	}

	public static final Date getFirstBizDayOfMonth(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE,1);
		return cal.getTime();	
	}
	public static final Date getLastBizDayOfMonth(Date date){
		if(date==null){
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE,1);
		cal.add(Calendar.MONTH,1);		
		cal.add(Calendar.DATE,-1);
		return cal.getTime();	
	}	



	public static final Date getFirstDayOfPreviousMonth(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH,-1);
		cal.set(Calendar.DATE,1);
		return cal.getTime();	
	}



	public static final Date getLastDayOfPreviousMonth(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH,-1);
		cal.set(Calendar.DATE,1);		
		return getLastDayOfMonth(cal.getTime());	
	}



	public static final Date getFirstDayOfNextMonth(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH,1);
		cal.set(Calendar.DATE,1);
		return cal.getTime();	
	}



	public static final Date getLastDayOfNextMonth(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH,1);
		cal.set(Calendar.DATE,1);		
		return getLastDayOfMonth(cal.getTime());	
	}	



	public static final Date getFirstDayOfQuarter(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int month=cal.get(Calendar.MONTH);
		int quarter=month/3;
		
		cal.set(Calendar.MONTH,quarter*3);
		cal.set(Calendar.DATE,1);
		return cal.getTime();			
	}


	public static final Date getLastDayOfQuarter(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int month=cal.get(Calendar.MONTH);
		int quarter=month/3;
		
		cal.set(Calendar.MONTH,quarter*3+2);
		cal.set(Calendar.DATE,1);
		return getLastDayOfMonth(cal.getTime());			
	}	


	public static final Date getFirstDayOfYear(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.MONTH,1);
		cal.set(Calendar.DATE,1);
		return cal.getTime();			
	}
	public static final Date getFirstDayOfYear(int year){
		return toBizDate(year+"01-01");	
	}


	public static final Date getLastDayOfYear(Date date){
		if(date==null){
			return null;
		}		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.MONTH,11);
		cal.set(Calendar.DATE,31);
		return cal.getTime();			
	}
	public static final Date getLastDayOfYear(int year){
		return toBizDate(year+"12-31");	
	}


	public static final Date getBeginTimeOfDay(Date date){
		return toBizDate(toBizString(date));
	}


	public static final Date getEndTimeOfDay(Date date){
		return new Date(toBizDate(toBizString(addDays(date,1))).getTime()-1000);
	}


	public static final Date getEndTimeOfDay2(Date date){
		return toBizDate(toBizString(addDays(date,1)));
	}


	public static final Date getGMTBeginTimeOfDay(Date date){
		return getBeginTimeOfDay(toGMTTimestamp(date));	
	}

	public static final Date getGMTEndTimeOfDay(Date date){
		return getEndTimeOfDay(toGMTTimestamp(date));	
	}	

}

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    操作日期的java包

    6. **日期运算**:Joda-Time提供了丰富的日期运算方法,如加上或减去天数、月份、年份等,还支持复杂的日期时间间隔计算。 7. **比较和查询**:可以轻松比较两个日期时间对象,或者查询某个日期时间是否在另一日期...

    JAVA日期操作类详细讲解

    在日期运算中,`java.time`包提供了`plusDays()`, `minusMonths()`, `until()`等方法,使得日期的加减运算变得直观且方便。另外,`TemporalAdjusters`接口提供了一些实用的调整器,比如`firstDayOfMonth()`和`...

    java日期转化操作大全

    在Java编程语言中,日期和时间的处理是常见的任务,涉及到日期的创建、格式化、解析、比较以及各种转换操作。本篇文章将深入探讨Java中日期转换的各种方法和技巧。 一、日期对象的创建 在Java中,日期对象可以通过...

    java 位运算知识点

    Java 位运算是对二进制数进行操作的运算符,它们直接作用于整型变量的二进制表示。位运算通常用于底层编程、优化算法和处理二进制数据。以下是一些关键的Java位运算符: 1. **按位与(&)**:两个操作数中,如果对应...

    java所有可能的日期操作封装

    Java日期操作是编程中常见的需求,它涉及到对时间的处理、日期格式化、日期比较以及时间戳转换等。Java提供了一系列强大的API来处理日期和时间,主要包括`java.util.Date`、`java.util.Calendar`、`java.text....

    java语言日期处理大全

    以下是一些关于Java日期处理的关键知识点,这些知识点在给定的文件中有所提及: 1. **获取当前日期**: Java通过`java.util.Date`类可以获取服务器上的当前日期。创建一个Date对象即会返回当前系统时间,如`Date ...

    java日期计算

    根据给定文件的信息,我们可以总结出以下几个关于Java日期计算的关键知识点: ### 1. 获取某个月的最大天数 为了获取某个月的最大天数,我们可以通过`Calendar`类来进行操作。首先,我们需要创建一个`Calendar`...

    java版日期计算器

    总之,"Java版日期计算器"是一个实现日期运算的工具,利用Java提供的日期API,可以方便地进行日期加减、计算日期差等操作。随着Java版本的更新,开发者应该优先考虑使用`java.time`包,因为它提供了更加简洁和强大的...

    日期类的加减及java中所以日期类的操作算法大全

    本文将深入探讨Java中的日期类操作,包括日期的加减运算,以及如何利用不同的日期类进行高效的时间管理。 ### 一、日期类的基本操作 #### 1. 使用Calendar类 `Calendar`类是Java中处理日期和时间的核心类之一,它...

    JAVA_日期格式化

    - `Date`类提供了`add`、`subtract`等方法进行日期运算,但推荐使用Java 8的日期/时间API,如`LocalDate.plusDays(1)`来增加一天。 9. **日期比较**: - `java.util.Date`可以用`compareTo`方法比较两个日期的...

    应用java实现日期型字符串和日期之间的相互转换(源代码)

    Java 8的日期时间API提供了丰富的日期运算方法,如加减天数、月份等。 7. **日期比较:** 可以通过compareTo方法比较两个日期对象的大小。 在"9_4_日期处理组件"的源代码中,可能会包含以上所述的转换功能,以及...

    java 日期计算

    下面我们将深入探讨相关的Java日期处理知识。 Java中的日期和时间API主要有两个版本:一个是早期的`java.util.Date`和`java.util.Calendar`类,另一个是Java 8引入的`java.time`包,包含`LocalDate`, `LocalTime`, ...

    最全的java时间(日期)工具类

    然而,`java.util.Date`和`SimpleDateFormat`在处理复杂日期时间操作时存在一些局限性,例如不支持时区、日期时间计算等。Java 8引入了`java.time`包,提供了一套更加现代、强大且易于使用的API。 在`java.time`...

    java中的时间操作

    ### Java中的时间操作 在Java开发中,时间处理是一项非常重要的功能,特别是在涉及到日期和时间...对于现代Java应用来说,推荐使用`java.time`包中的类来进行日期和时间的操作,因为它们提供了更加现代化且易用的API。

    java中计算两个日期相差几天

    在Java编程中,经常需要处理与日期和时间相关的操作。其中一项常见的需求就是计算两个日期之间的差距。本篇文章将详细介绍如何在Java中计算两个日期相差几天,并深入探讨示例代码中的关键概念和技术细节。 #### ...

    日期连续日期

    例如,Python的`pandas`库可以轻松处理日期序列,进行日期运算、窗口操作等。SQL中的日期函数如`DATEADD`和`DATEDIFF`可以方便地对连续日期进行操作。 在实际应用中,我们还可能遇到如何生成连续日期序列的问题。...

    日期操作,转日期格式,加减日期,日期详解

    在Java 8及更高版本中,`java.time`包提供了方便的日期时间运算方法。你可以使用`ChronoUnit`枚举来添加或减去年、月、日等单位。比如,`plusDays()`和`minusDays()`方法可以方便地增加或减少日期中的天数。 在提供...

    java源码包---java 源码 大量 实例

    Java日期选择控件完整源代码 14个目标文件 内容索引:JAVA源码,系统相关,日历,日期选择  Java语言开发的简洁实用的日期选择控件,源码文件功能说明:  [DateChooser.java] Java 日期选择控件(主体类) [public]  ...

    java求日期差

    - 使用`add()`方法对日期进行增加或减少操作。 ##### 2. `roll()` ```java cal.roll(Calendar.DATE, -4); ``` **解释:** - `roll()`方法类似于`add()`方法,但不同的是它不会跨越边界(如月份、年份)。 #### ...

Global site tag (gtag.js) - Google Analytics