⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdate.java

📁 本源码为教学管理信息系统
💻 JAVA
字号:
package com.wygl.taglib;

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.TimeZone;


/**
 * 日期操作类
 * <p>Title:ManageDate </p>
 * <p>Description:日期操作类  </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 黑龙江傲立信息产业有限公司</p>
 * @author 庞志国
 * @version 1.0
 */

public class MDate {			
	/**
	 * 功能:得到系统当前日期时间,无参数表示当前日期
	 * @return 返回字符串类型日期时间
	 */
	public static String DatetimeToString(Date userDate){
	  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	  return format.format(userDate);
	}
	/**
	* 格式化日期
	* @param date Date 日期
	* @param strFormat String 日期格式化类型 
	* @return String
	*/
	public static String DTFormat(Date date, String strFormat) {
		DateFormat df = new SimpleDateFormat(strFormat);
		return df.format(date);
	}
	/**
	* 格式化日期时间
	* @param sdate Date 日期
	* @param datestyle int 日期格式化类型 
	* @param timestyle int 时间格式化类型
	* @return String
	*/
	public static String DTFormat(Date date, int datestyle, int timestyle) {
		DateFormat df = DateFormat.getDateTimeInstance(datestyle, timestyle);
		String rtndate = df.format(date);
		return rtndate;
	}
	/**
	   * 获得服务器时间
	   * @return java.util.Date
	   */
	public static java.util.Date getCurDate() {
		java.util.Date rtndate = new java.util.Date();
		return rtndate;
	}
	/**
	 * 功能:获得没有小时、分、秒的当前时间
	 * @param cur_date	 
	 */
	public static Date _getCurDate() {
		String s = DatetimeToString(getCurDate());	
		return _getCurDate(s);		
	}
	/**
	 * 功能:获得没有小时、分、秒的当前时间
	 * @param s 当前日期的字符串格式	 
	 */
	public static Date _getCurDate(String s) {		
		int year = Integer.parseInt(s.substring(0,4));
		int month = Integer.parseInt(s.substring(5,7));
		int day = Integer.parseInt(s.substring(8,10));	
		Calendar xmas = new GregorianCalendar(year,month-1,day);		
		return xmas.getTime();		
	}
	/**
	 * Method RelativeDate.
	 * <BR>获得相对某日i天的日期
	 * @param date 参照日期
	 * @param i 天数(可正可负)
	 * @return Date 改变后的日期
	 */
	public static Date getRelDate(Date date, int i) {
		Date rDate = new Date();
		rDate.setTime(date.getTime() + i * 86400000);
		return rDate;
	}
	/**
	 * 功能:得到userDate经过intervalDays天的日期或时间
	 * @param userDate 源日期
	 * @param intervalDays 经过的 天数
	 * @param type 类型:如果是日期就是Calendar.DAY OF YEAR
	 * @return 最终的日期
	 */
	public static Date getAfterDatetime(Date userDate, int intervaldatetime,int type) {
	  Calendar usercal; //用户日期
	  usercal = new GregorianCalendar();
	  usercal.setTime(userDate);
	  usercal.add(type, intervaldatetime);
	  return usercal.getTime();
	}
	/**
	* 服务器时间格式化
	* @param date String 日期字符串
	* @param style int 格式化类型
	* @return String
	*/
	public static String DTFormat(String date, int style) {
		try {
			switch (style) {
				case 1 : //"yyyy.mm.dd"
					return date.substring(0, 10);
				case 2 : //"yyyy.mm"
					return date.substring(0, 7);
				case 3 : //"yyyy"
					return date.substring(0, 4);
				case 4 : //"mm"
					return date.substring(5, 7);
				case 5 : //"dd"
					return date.substring(8, 10);
				case 6 : //hh:mm:ss
					return date.substring(11, 19);
				case 7 : //hh
					return date.substring(11, 13);
				case 8 : //mm
					return date.substring(14, 16);
				case 9 : //ss
					return date.substring(17, 19);
				case 10 : //hh:mm
					return date.substring(11, 16);
				default :
					return null;
			}
		} catch (Exception e) {
			return "&nbsp;";
		}
	}
	/**
	* 得到指定年月的最后一天的日期
	* @param date int 年
	* @param month int 月
	* @return String
	*/
	public static String getLastDate(int year, int month) {
		int day = 0;
		String rtndate = null;
		String stryear = null;
		String strmonth = null;
		String strday = null;
		boolean blrn = false; //是否为闰年
		if ((month < 1) || (month > 12)) {
			return null;
		}
		//判断是否为闰年
		if (((year % 4 == 0) || (year % 100 == 0)) && (year % 400 == 0)) {
			blrn = true;
		} else {
			blrn = false;
		}
		//根据不同的月份计算每个月的天数
		if ((month == 1)
			|| (month == 3)
			|| (month == 5)
			|| (month == 7)
			|| (month == 8)
			|| (month == 10)
			|| (month == 12)) {
			day = 31;
		}
		if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
			day = 30;
		}
		if (month == 2) {
			if (blrn == true) {
				day = 29;
			} else {
				day = 28;
			}
		}
		//形成日期字符串并且返回
		stryear = Integer.toString(year);
		if (month < 10) {
			strmonth = "0" + Integer.toString(month);
		} else {
			strmonth = Integer.toString(month);
		}
		strday = Integer.toString(day);
		rtndate = stryear + "." + strmonth + "." + strday;
		return rtndate;
	}
	/**
	* 获得两个日期之间的差值
	* @param sdate1 String 日期1
	* @param sdate2 String 日期2
	* @param fmt String 格式化类型
	* @param tz TimeZone 
	* @return int
	*/
	public static int getDateDiff(
		String sdate1,
		String sdate2,
		String fmt,
		TimeZone tz) {
		SimpleDateFormat df = new SimpleDateFormat(fmt);
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = df.parse(sdate1);
			date2 = df.parse(sdate2);
		} catch (ParseException pe) {
			pe.printStackTrace();
		}
		Calendar cal1 = null;
		Calendar cal2 = null;
		if (tz == null) {
			cal1 = Calendar.getInstance();
			cal2 = Calendar.getInstance();
		} else {
			cal1 = Calendar.getInstance(tz);
			cal2 = Calendar.getInstance(tz);
		}
		cal1.setTime(date1);
		long ldate1 =
			date1.getTime()
				+ cal1.get(Calendar.ZONE_OFFSET)
				+ cal1.get(Calendar.DST_OFFSET);
		cal2.setTime(date2);
		long ldate2 =
			date2.getTime()
				+ cal2.get(Calendar.ZONE_OFFSET)
				+ cal2.get(Calendar.DST_OFFSET);
		int hr1 = (int) (ldate1 / 3600000); //60*60*1000
		int hr2 = (int) (ldate2 / 3600000);
		int days1 = (int) hr1 / 24;
		int days2 = (int) hr2 / 24;
		int dateDiff = days2 - days1;
		int weekOffset =
			(cal2.get(Calendar.DAY_OF_WEEK) - cal1.get(Calendar.DAY_OF_WEEK))
				< 0
				? 1
				: 0;
		int weekDiff = dateDiff / 7 + weekOffset;
		int yearDiff = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
		int monthDiff =
			yearDiff * 12 + cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);
		return dateDiff;
	}
	/**
	* 获得指定日期的星期
	* @param sdate String 日期
	* @param fmt String 格式化类型 
	* @return String
	*/
	public static String getDayOfWeek(String sdate, String fmt) {
		SimpleDateFormat df = new SimpleDateFormat(fmt);
		java.util.Date date = null;
		Calendar cal1 = Calendar.getInstance();
		String chiweek = null;
		try {
			date = df.parse(sdate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//设置时间
		cal1.setTime(date);
		int bh = cal1.get(Calendar.DAY_OF_WEEK);
		switch (bh) {
			case 1 :
				chiweek = "星期日";
				break;
			case 2 :
				chiweek = "星期一";
				break;
			case 3 :
				chiweek = "星期二";
				break;
			case 4 :
				chiweek = "星期三";
				break;
			case 5 :
				chiweek = "星期四";
				break;
			case 6 :
				chiweek = "星期五";
				break;
			case 7 :
				chiweek = "星期六";
				break;
		}
		return chiweek;
	}
	/**
	* 获得系统的当前年度
	* @return String
	*/
	public static String getYear() {
		return DTFormat(getCurDate(), "yyyy");
	}
	/**
	* 获得系统的当前月份
	* @return String
	*/
	public static String getMonth() {
		return DTFormat(getCurDate(), "MM");
	}
	/**
	* 获得系统的当前日
	* @return String
	*/
	public static String getDay() {
		return DTFormat(getCurDate(), "dd");
	}
	/**
	* 获得系统的当前年度
	* @return String
	*/
	public static int _getYear() {
		return Integer.parseInt(DTFormat(getCurDate(), "yyyy"));
	}
	/**
	* 获得系统的当前月份
	* @return String
	*/
	public static int _getMonth() {
		return Integer.parseInt(DTFormat(getCurDate(), "MM"));
	}
	/**
	* 获得系统的当前日
	* @return String
	*/
	public static int _getDay() {
		return Integer.parseInt(DTFormat(getCurDate(), "dd"));
	}
	/**
	 * 获得指定日期是今年的第几周
	 */
	public static int getWeekOfYear(Date date) {			
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.WEEK_OF_YEAR);
	}
	/**
	 * 获得当前日期是今年的第几周
	 */
	public static int getWeekOfYear() {
		return getWeekOfYear(getCurDate());
	}
	/**
	 * 获得当前日期是今年的第几周
	 */
	public static int getRelWeekOfYear() {
		return getWeekOfYear(getRelDate(getCurDate(), -7));
	}
	/**
	 * 获得指定年有多少周
	 */
	public static int getWeekNumbersOfYear(int year) {
		GregorianCalendar cal = new GregorianCalendar(year,12,31);
		return cal.getMaximum(GregorianCalendar.WEEK_OF_YEAR);
	}
	/**
	 * 获得指定年,月有多少天
	 */
	public static int getDaysByYearMonth(int year,int month) {
		int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31};
		int day=0;
		if (year%4==0) days[2] = 29;
		for(int i=1;i<13;i++){
			if(i==month){
				day = days[i];	
			}
		}
		return day;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -