现在的位置: 首页 > 综合 > 正文

Java学习笔记_Date日期相关处理

2017年12月27日 ⁄ 综合 ⁄ 共 19232字 ⁄ 字号 评论关闭

---数据库中的Date类型有:
传入数据库中用作查询条件的date参数是String类型的
http://windshome.iteye.com/blog/1871058
http://blog.csdn.net/binbinxyz/article/details/8557558

---Java中的Date:
Java语言的Calendar(日历),Date(日期)和DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分.
年:from 1900
月:from 0 to 11
周:起始为周日,0

--Date:
Date date = new Date();  
System.out.println(date);               // Fri Mar 02 22:38:41 CST 2012  
System.out.println(date.getTime());     // getTime:毫秒数,1330699121772 

--Calendar:
GregorianCalendar calendar = new GregorianCalendar();
//calendar.set(2012, Calendar.NOVEMBER, 15, 18, 23,55);  
calendar.setTime(date);
Date curDay = calendar.getTime();  //Calendar转成Date:Thu May 08 11:55:38 CST 2014

--new Date():从1970年1月1日开始经历的毫秒数:
System.currentTimeMillis();

--格式化显示日期:
// 采用SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.format(new Date());//格式化显示日期

sdf.parse("2014-01-01");//将字符串解析化成Date

// 采用DateFormat
DateFormat.getDateInstance(int dateStyle).format(date);//Date
DateFormat.getDateTimeInstance(int dateStyle, int timeStyle).format(date);//DateTime

--处理Calendar的set(),get(),add(),roll()
//设置和获取日期数据的特定部分:
Calendar cal=Calendar.getInstance();
Integer y = cal.get(Calendar.YEAR);
//取值
Integer m = cal.get(Calendar.MONTH) + 1;
Integer d = cal.get(Calendar.DATE);

cal.set(Calendar.YEAR, 2006);
//设值
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DAY_OF_MONTH, 3);

GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);// 指定日期为当前日期

System.out.println("System Date: " + calendar.getTime()); //date:Thu May 08 13:50:32 CST 2014

calendar.clear();
//在使用set方法之前,必须先clear一下,否则很多信息会继承自系统当前时间

calendar.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY);// 指定日期为当前周的星期五

calendar.set(Calendar.DATE, 1);
// 设置当前月的1号
//calendar.add(Calendar.MONDAY, -1);
// 减一个月,变为上月的1号
calendar.add(Calendar.MONDAY, 1);
// 加一个月,变为下月的1号
calendar.add(Calendar.DATE, -1);
// 减去一天,变为当前月的最后一天

calendar.add(GregorianCalendar.DAY_OF_MONTH, 7);//add 方法让我们能够在日期上加减数值推进时间,润年的所有复杂的计算都由这个方法自动处理

//set(),add()方法中,YEAR,MONTH,DATE是可以分离开设置的,

calendar.set(Calendar.DAY_OF_YEAR, 1);
// 设置当年第一天
calendar.get(Calendar.YEAR) + "-12-31";
// 获取本年的最后一天
/* 本年的最后一天
or:
calendar.add(Calendar.YEAR, 1);//加一年
calendar.set(Calendar.DAY_OF_YEAR, 1);
//设置为年的第一天
calendar.add(Calendar.DAY_OF_YEAR, -1);//减一天

*/
(calendar.get(Calendar.YEAR) - 1) + "-01-01";
// 获取上年的第一天

calendar.roll(Calendar.DAY_OF_YEAR, -1);
// 把日期回滚一天

calendar.getTime(); //获取到日期

//计算两个日期的距离天数,分别通过date.getTime()并相减得出间隔毫秒数,再将毫秒数转换成天数

--Calendar和Date的转化
(1) Calendar转化为Date
Calendar cal=Calendar.getInstance();
Date date=cal.getTime();
(2) Date转化为Calendar
Date date=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(date);

--roll-链接:http://blog.sina.com.cn/s/blog_62c89b450100sau9.html
roll()函数处理,只会比相应的字段进行处理,不会智能的对其它字段也进行逻辑上的改变。但是add()函数会在逻辑上改变其它字段,使结果正确。

链接:http://www.iteye.com/topic/218271

public class DateCalendarStudyDemo {

	/**
	 * 格式化显示日期、解析日期字符串成Date。 
	 * SimpleDateFormat; 
	 * DateFormat.getDateInstance(int dateStyle); 
	 * DateFormat.getDateTimeInstance(int dateStyle, int timeStyle);
	 * 
	 * @author mao
	 */
	public static void formatOrParseStringDate() throws Exception {

		// 采用SimpleDateFormat
		if (true) {
			SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
			System.out.println("05-20-2014: " + sdf.format(new Date()));//格式化
			Date date = sdf.parse("06-20-2014");//解析成Date
			System.out.println("date: " + date.getTime());
		}

		// 采用DateFormat
		if (true) {
			String strDate = null;

			Date date = new Date();
			System.out.println("Tue May 20 00:24:28 CST 2014:	" + date);

			//Date DateFormat.getDateInstance(int dateStyle)
		
			strDate = DateFormat.getDateInstance().format(date);
			System.out.println("2014-5-8 :	" + strDate);

			strDate = DateFormat.getDateInstance(DateFormat.DEFAULT).format(date);
			System.out.println("2014-5-8 :	" + strDate);

			strDate = DateFormat.getDateInstance(DateFormat.SHORT).format(date);
			System.out.println("14-5-8 :	" + strDate);

			strDate = DateFormat.getDateInstance(DateFormat.MEDIUM).format(date);
			System.out.println("2014-5-8 :	" + strDate);

			strDate = DateFormat.getDateInstance(DateFormat.LONG).format(date);
			System.out.println("2014年5月8日 :	" + strDate);

			strDate = DateFormat.getDateInstance(DateFormat.FULL).format(date);
			System.out.println("2014年5月8日 星期四:	" + strDate);

			
			//DateTime DateFormat.getDateTimeInstance(int dateStyle, int timeStyle)
			
			strDate = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT).format(date);
			System.out.println("2014-5-8 10:30:31:	" + strDate);

			strDate = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date);
			System.out.println("14-5-8 上午10:30:	" + strDate);

			strDate = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(date);
			System.out.println("2014-5-8 10:30:31:	" + strDate);

			strDate = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(date);
			System.out.println("2014年5月8日 上午10时30分31秒:	" + strDate);

			strDate = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(date);
			System.out.println("2014年5月8日 星期四 上午10时30分31秒 CST:	" + strDate);
		}
	}
	

	/**
	 * 获取yyyy-MM-dd HH:mm:ss中的指定部分
	 * 
	 * @author mao
	 */
	public static void getPartOfYYYYMMDD() throws Exception {
		Calendar c = Calendar.getInstance();
		System.out.println("年: " + c.get(Calendar.YEAR));
		String m = (c.get(Calendar.MONTH) + 1)+"";//月份要加1; 5月份只会显示5,不会显示05,需手动加上
		System.out.println("月(d): " + m );		
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		Date month = sdf.parse(m);
		System.out.println("月(dd): " + sdf.format(month));
		
		System.out.println("日: " + c.get(Calendar.DAY_OF_MONTH));
		System.out.println("时: " + c.get(Calendar.HOUR_OF_DAY));
		System.out.println("分: " + c.get(Calendar.MINUTE));
		System.out.println("秒: " + c.get(Calendar.SECOND));
		System.out.println("当前时间毫秒数:" + c.getTimeInMillis());
		System.out.println(c.getTime());

		Date d = new Date();
		System.out.println("Java Date: "+d);
		
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("格式化后的日期:" + sdf2.format(d));//Date转String

		String str = "2012-1-13 17:26:33"; 
		System.out.println("字符串转成日期:" + sdf2.parse(str));//String解析成Date
	}



	/**
	 * 通过Calendar设置日期变动
	 * 	主要API:set(),get(),add().
	 * 
	 * @author mao
	 */
	public static void setCalendarTime() throws Exception {
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
		
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);// 设置为当前日期
		System.out.println("System Date: " + calendar.getTime());

		calendar.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY);// 设置为day_of_week的星期五,每周从周日开始算
		System.out.println("Day of Week to Friday:  " + dateFormat.format(calendar.getTime()));

		int friday13Counter = 0;//计算接下来每月的13号是星期五的前十个日期
		while (friday13Counter < 10) {
			calendar.add(GregorianCalendar.DAY_OF_MONTH, 7);//add 方法让我们能够在日期上加上数值,润年的所有复杂的计算都由这个方法自动处理
			//System.out.println(dateFormat.format(calendar.getTime()));
			if (calendar.get(GregorianCalendar.DAY_OF_MONTH) == 13) {
				friday13Counter++;
				System.out.println(friday13Counter + " :" + dateFormat.format(calendar.getTime()));
			}
		}
	}
	
	/**
	 * 获取每月的第一天和最后一天的区域
	 * 
	 * @author mao
	 */
	public static void getCurrentFirstAndLastDayOfMonth() throws Exception {
		Calendar c = Calendar.getInstance();
		Integer y = c.get(Calendar.YEAR);
		Integer m = c.get(Calendar.MONTH) + 1;
		Integer d = c.get(Calendar.DATE);
		Integer d1 = c.get(Calendar.DAY_OF_MONTH);// 一个月的第几天
		Integer d3 = c.get(Calendar.DAY_OF_WEEK);// 一周的星期几
		Integer d2 = c.get(Calendar.DAY_OF_YEAR);// 一年的第几天
		System.out.println(y);
		System.out.println(m);
		System.out.println(d);
//		System.out.println(d1);
//		System.out.println(d2);
//		System.out.println(d3);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Calendar cal = Calendar.getInstance();//创建Calendar
		cal.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1);//当前月的第一天
		Date firstDayOfMonth = cal.getTime();//由Calendar转换成Date
		System.out.println("first date: " + sdf.format(firstDayOfMonth));
		
		cal.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1, 1, 0, 0, 0);//下个月的第一天
		cal.add(Calendar.DAY_OF_MONTH, -1);//往前推一天,即本月最后一天
		Date lastDayOfMonth = cal.getTime();
		System.out.println("last date: " + sdf.format(lastDayOfMonth));
	}
}

/**
 * 
 * @author csdn
 *
 */
public class DateCalendarUseDemo {
	public static void main(String[] args) {
		DateCalendarUseDemo tt = new DateCalendarUseDemo();

		System.out.println("\n当前日期: ");
		System.out.println(tt.getNowTime());					// 获取当前时间
		System.out.println(tt.getNowTime2());					// 获取当前时间
		System.out.println(tt.getNowTime3());					// 获取当前时间
		
		System.out.println("\n计算周: ");
		System.out.println(tt.getDaysOfNow2SundayInWeek());		// 获取当前日期与本周日的相差天数
		System.out.println(tt.getDateOfMondayInWeek());			// 获取本周一的日期
		System.out.println(tt.getDateOfSaturdayInWeek());		// 获取本周六的日期
		System.out.println(tt.getDateOfSaturdayInNextWeek());	// 获取下周一的日期
		System.out.println(tt.getDateOfSaturdayInLastWeek());	// 获取上周六的日期

		System.out.println("\n计算月: ");
		System.out.println(tt.getFirstDayOfMonth());			// 获取当月的第一天
		System.out.println(tt.getLastDayOfMonth());				// 获取当月最后一天
		System.out.println(tt.getFirstDayOfPreviousMonth());	// 获取上月第一天		
		System.out.println(tt.getLastDayOfPreviousMonth());		// 获取上月最后一天			
		System.out.println(tt.getFirstDayOfNextMonth());		// 获取下月第一天	
		System.out.println(tt.getLastDayOfNextMonth());			// 获取下月最后一天

		System.out.println("\n计算年: ");
		System.out.println(tt.getFirstDayOfYear());				// 获取本年的第一天
		System.out.println(tt.getLastDayOfYear());				// 获取本年最后一天
		System.out.println(tt.getFirstDayOfPreviousYear());		// 获取上年的第一天
		System.out.println(tt.getLastDayOfPreviousYear());		// 获取上年最后一天
		System.out.println(tt.getFirstDayOfNextYear());			// 获取下年的第一天
		System.out.println(tt.getLastDayOfNextYear());			// 获取下年最后一天
		System.out.println(tt.getDaysOfYear());					// 获取本年的天数

		System.out.println("\n计算季度: ");
		System.out.println(tt.getSeasonOfMonth());			 	// 获取当前月的季度
		System.out.println(tt.getLastDayOfNow());				// 获取当前天所在月份的天数
		System.out.println(tt.isLeapYear(2012));			 	// 判断是否是润年
		
		System.out.println("\n日期格式转换与计算: ");
		System.out.println(tt.getDateFromStr("2012-06-20"));	// 将字符串时间格式 yyyy-MM-dd,转换成Date类型
		System.out.println(tt.getWeek("2012-06-20"));			// 根据一个日期,返回是星期几的字符串
		System.out.print("2012-06-02 --> 2012-06-12 间隔天数: ");	// 计算两个日期间的间隔天数
		System.out.println(tt.getDaysFromTwoDate("2012-06-02", "2012-06-12"));			
	}
	
	// 获取当前时间
	public String getNowTime() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Date now = new Date();
		return sDateFormat.format(now);
	}

	public String getNowTime2() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Date now = new Date(System.currentTimeMillis());
		return sDateFormat.format(now);
	}

	public String getNowTime3() {
		StringBuilder strBuildNow = new StringBuilder();
		
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		int second = calendar.get(Calendar.SECOND);
		int millissecond = calendar.get(Calendar.MILLISECOND);
		
		strBuildNow.append(year + "-");
		strBuildNow.append(month + "-");
		strBuildNow.append(day + " ");
		strBuildNow.append(hour + ":");
		strBuildNow.append(minute + ":");
		strBuildNow.append(second + ".");
		strBuildNow.append(millissecond);
		
		return strBuildNow.toString();
	}
	
	// 获取当前日期与本周日的相差天数
	public int getDaysOfNow2SundayInWeek() {
		Calendar calendar = Calendar.getInstance();
		
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;		// 减一天,周一为1,符合中国人习惯。Sunday - 1; Monday - 2; Saturday - 7
		if(dayOfWeek == 0) {		// 周日
			return 0;
		} else {
			return 0 - dayOfWeek;
		}
	}
	
	// 获取本周一的日期
	public String getDateOfMondayInWeek(){
		int day = this.getDaysOfNow2SundayInWeek() + 1;	// 加1,即当前日期与本周一的间隔天数
		GregorianCalendar gCalendar = new GregorianCalendar();
		
		gCalendar.add(GregorianCalendar.DATE, day);		// 计算与本周一相差的时间间隔
		Date curDay = gCalendar.getTime();
		
		DateFormat dateFormat = DateFormat.getInstance();
		String dateOfMonday = dateFormat.format(curDay);
		
		return dateOfMonday;
	}

	// 获取本周六的日期
	public String getDateOfSaturdayInWeek(){
		int day = this.getDaysOfNow2SundayInWeek() + 6;	// 加6,即当前日期与本周六的间隔天数
		GregorianCalendar gCalendar = new GregorianCalendar();
		
		gCalendar.add(GregorianCalendar.DATE, day);		// 计算与本周六相差的时间间隔
		Date curDay = gCalendar.getTime();
		
		DateFormat dateFormat = DateFormat.getInstance();
		String dateOfMonday = dateFormat.format(curDay);
		
		return dateOfMonday;
	}

	// 获取上周六的日期
	public String getDateOfSaturdayInLastWeek(){
		int day = this.getDaysOfNow2SundayInWeek() - 1;	// 减1,即当前日期与上周六的间隔天数
		GregorianCalendar gCalendar = new GregorianCalendar();
		
		gCalendar.add(GregorianCalendar.DATE, day);		// 计算与上周一相差的时间间隔
		Date curDay = gCalendar.getTime();
		
		DateFormat dateFormat = DateFormat.getInstance();
		String dateOfMonday = dateFormat.format(curDay);
		
		return dateOfMonday;
	}
	
	// 获取下周一的日期
	public String getDateOfSaturdayInNextWeek(){
		int day = this.getDaysOfNow2SundayInWeek() + 8;	// 加8,即当前日期与下周一的间隔天数
		GregorianCalendar gCalendar = new GregorianCalendar();
		
		gCalendar.add(GregorianCalendar.DATE, day);		// 计算与下周一相差的时间间隔
		Date curDay = gCalendar.getTime();
		
		DateFormat dateFormat = DateFormat.getInstance();
		String dateOfMonday = dateFormat.format(curDay);
		
		return dateOfMonday;
	}
	
	// 获取当月第一天
	public String getFirstDayOfMonth() {
		String strFirstDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);		// 设置当前月的1号
		
		strFirstDay = sDateFormat.format(calendar.getTime());
		return strFirstDay;
	}

	// 获取当月最后一天
	public String getLastDayOfMonth() {
		String strLastDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);		// 设置当前月的1号
		calendar.add(Calendar.MONDAY, 1);	// 加一个月,变为下月的1号
		calendar.add(Calendar.DATE, -1);	// 减去一天,变为当前月的最后一天
		
		strLastDay = sDateFormat.format(calendar.getTime());
		return strLastDay;
	}

	// 获取上月第一天
	public String getFirstDayOfPreviousMonth() {
		String strFirstDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);		// 设置当前月的1号
		calendar.add(Calendar.MONDAY, -1);	// 减一个月,变为上月的1号
		
		strFirstDay = sDateFormat.format(calendar.getTime());
		return strFirstDay;
	}

	// 获取上月最后一天
	public String getLastDayOfPreviousMonth() {
		String strLastDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);		// 设置当前月的1号
		calendar.add(Calendar.DATE, -1);	// 减一天,变为上月的1号
		
		strLastDay = sDateFormat.format(calendar.getTime());
		return strLastDay;
	}

	public String getLastDayOfPreviousMonth2() {
		String strLastDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONDAY, -1);	// 减一个月
		calendar.set(Calendar.DATE, 1);		// 设置当前月的1号
		calendar.roll(Calendar.DATE, -1);	// 把日期回滚一天,也就是本月最后一天
		
		strLastDay = sDateFormat.format(calendar.getTime());
		return strLastDay;
	}
	
	// 获取下月第一天
	public String getFirstDayOfNextMonth() {
		String strFirstDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 1);	// 加一个月
		calendar.set(Calendar.DATE, 1);		// 设置当前月第一天
		
		strFirstDay = sDateFormat.format(calendar.getTime());
		return strFirstDay;
	}

	// 获取下月最后一天
	public String getLastDayOfNextMonth() {
		String strLastDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);		// 设置当前月的1号
		calendar.add(Calendar.MONDAY, 2);	// 加两个月,变为下下月的1号
		calendar.add(Calendar.DATE, -1);	// 减一天,变为下月的最后一天
		
		strLastDay = sDateFormat.format(calendar.getTime());
		return strLastDay;
	}

	public String getLastDayOfNextMonth2(){
		String strLastDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 1);	// 加1个月
		calendar.set(Calendar.DATE, 1);		// 把日期设置为当月第1天
		calendar.roll(Calendar.DATE, -1);	// 日期回滚1天,也就是本月最后1天
		
		strLastDay = sDateFormat.format(calendar.getTime());
		return strLastDay;
	}
	
	// 获取本年的第一天
	public String getFirstDayOfYear(){
		String strFirstDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, 1);		// 设置当年第一天
		
		strFirstDay = sDateFormat.format(calendar.getTime());
		return strFirstDay;
	}

	// 获取本年的最后一天
	public String getLastDayOfYear(){
		String strLastDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
//		strLastDay = calendar.get(Calendar.YEAR) + "-12-31";
		
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		calendar.add(Calendar.YEAR, 1);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		strLastDay = sDateFormat.format(calendar.getTime());
		
		return strLastDay;
	}
	
	// 获取上年的第一天
	public String getFirstDayOfPreviousYear(){
		String strFirstDay = "";
		
		Calendar calendar = Calendar.getInstance();
		strFirstDay = (calendar.get(Calendar.YEAR) - 1) + "-01-01";
		
		return strFirstDay;
	}

	// 获取上年的最后一天
	public String getLastDayOfPreviousYear(){
		String strLastDay = "";
		
		Calendar calendar = Calendar.getInstance();
		strLastDay = (calendar.get(Calendar.YEAR) - 1) + "-12-31";
		
		return strLastDay;
	}
	
	// 获取下年的第一天
	public String getFirstDayOfNextYear(){
		String strFirstDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, 1);				// 加一年
		calendar.set(Calendar.DAY_OF_YEAR, 1);		// 设置当年第一天
		
		strFirstDay = sDateFormat.format(calendar.getTime());
		return strFirstDay;
	}

	// 获取下年的最后一天
	public String getLastDayOfNextYear(){
		String strLastDay = "";
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, 1);				// 加一年
		calendar.set(Calendar.DAY_OF_YEAR, 1);		// 设置当年第一天
		calendar.roll(Calendar.DAY_OF_YEAR, -1);	// 回滚到当年最后一天
		
		strLastDay = sDateFormat.format(calendar.getTime());
		return strLastDay;
	}
	
	// 获取本年的天数
	public String getDaysOfYear(){
		int year = 0;
		int days = 0;
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, 1);		// 设置日期为当年第一天
		calendar.roll(Calendar.DAY_OF_YEAR, -1);	// 把日期回滚一天
		
		year = calendar.get(Calendar.YEAR);
		days = calendar.get(Calendar.DAY_OF_YEAR);
		
		return (days + " in " + year);
	}
	
	// 获取当前月的季度
	public String getSeasonOfMonth(){
		String strSeason = "";
		int year, month, day;
		int season = 1;
		int array[][] = new int[][]{ {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} };
		
		Calendar calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONDAY) + 1;
		day = calendar.get(Calendar.DAY_OF_MONTH);
		switch (month) {
		case 1:
		case 2:
		case 3:
			season = 1;
			break;
		case 4:
		case 5:
		case 6:
			season = 2;
			break;
		case 7:
		case 8:
		case 9:
			season = 3;
			break;
		case 10:
		case 11:
		case 12:
			season = 4;
			break;
		default:
			season = 1;
			break;
		}
		
		
		int start_month = array[season-1][0];
		int end_month = array[season-1][2];
		strSeason = year + "-" + month + "-" +day;
		strSeason += " in [ " + year + "-" + start_month + " : " + year + "-" + end_month + " ]";
		
		return strSeason;
	}
	
	
	// 获取当前天所在月份的天数
	public String getLastDayOfNow(){
		String strLastDay = "";
		int year, month, day;
		int days = 0;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH,1);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		int daysOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		strLastDay = daysOfMonth + "";
		
//		year = calendar.get(Calendar.YEAR);
//		month = calendar.get(Calendar.MONTH) + 1;
//		day = calendar.get(Calendar.DAY_OF_MONTH);
//		
//		switch (month) {
//		case 1:
//		case 3:
//		case 5:
//		case 7:
//		case 8:
//		case 10:
//		case 12:
//			days = 31;
//			break;
//		case 4:
//		case 6:
//		case 9:
//		case 11:
//			days = 30;
//			break;
//		case 2:
//			if (isLeapYear(year)) {
//				days = 29;
//			} else {
//				days = 28;
//			}
//			break;
//		default:
//			days = 0;
//			break;
//		}
//		
//		strLastDay = days + " in [ " + year + "-" + month + "-" + day + " ]";
		return strLastDay;
	}
	
	// 判断是否是润年
	public boolean isLeapYear(int year){
		return (year%4 == 0 && year%100 != 0) || (year%400 == 0);
	}
	
	// 将字符串时间格式 yyyy-MM-dd,转换成Date类型
	public Date getDateFromStr(String txtDate) {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date date = sDateFormat.parse(txtDate, pos);
		
		return date;
	}

	// 根据一个日期,返回是星期几的字符串
	public String getWeek(String txtDate) {
		Date date = getDateFromStr(txtDate);
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(date);
		String week = new SimpleDateFormat("EEEE").format(date);
		return week;
	}

	// 计算两个日期间的间隔天数
	public long getDaysFromTwoDate(String txtDate1, String txtDate2) {
		if(txtDate1 == null || txtDate1.equals("")) {
			return 0;
		}
		if(txtDate2 == null || txtDate2.equals("")) {
			return 0;
		}
		
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		long days = 0;
		try {
			Date date1 = sDateFormat.parse(txtDate1);
			Date date2 = sDateFormat.parse(txtDate2);
			days = (date2.getTime() - date1.getTime())/(24*60*60*1000);		// 通过getTime()方法,把时间Date转换成毫秒格式Long类型,进行计算
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
		return days;
	}
}

抱歉!评论已关闭.