关于获得时间相关的

本文介绍了一个日期操作工具类,提供了多种日期格式化、日期计算、日期比较等实用功能。包括获取当前日期、指定格式日期、日期加减天数、计算日期差、判断日期有效性等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 本类用于获得时间相关的
 * 
 * @author sunyanlong
 * 
 */
public class CalendarUtil
{
	// 用来全局控制 上一周,本周,下一周的周数变化
	private int weeks = 0;
	private int MaxDate;// 一月最大天数
	private int MaxYear;// 一年最大天数

	/**
	 * @param args
	 * @throws ParseException 
	 */
	public static void main(String[] args) throws ParseException
	{
		CalendarUtil tt = new CalendarUtil();
//		com.sinosoft.util.pub.Log.debug("获取当天日期:" + tt.getNowTime("yyyy-MM-dd"));
//		com.sinosoft.util.pub.Log.debug("获取本周一日期:" + tt.getMondayOFWeek());
//		com.sinosoft.util.pub.Log.debug("获取本周日的日期~:" + tt.getCurrentWeekday());
//		com.sinosoft.util.pub.Log.debug("获取上周一日期:" + tt.getPreviousWeekday());
//		com.sinosoft.util.pub.Log.debug("获取上周日日期:" + tt.getPreviousWeekSunday());
//		com.sinosoft.util.pub.Log.debug("获取下周一日期:" + tt.getNextMonday());
//		com.sinosoft.util.pub.Log.debug("获取下周日日期:" + tt.getNextSunday());
//		com.sinosoft.util.pub.Log.debug("获得相应周的周六的日期:" + tt.getNowTime("yyyy-MM-dd"));
//		com.sinosoft.util.pub.Log.debug("获取本月第一天日期:" + tt.getFirstDayOfMonth());
//		com.sinosoft.util.pub.Log.debug("获取本月最后一天日期:" + tt.getDefaultDay());
//		com.sinosoft.util.pub.Log.debug("获取上月第一天日期:" + tt.getPreviousMonthFirst());
//		com.sinosoft.util.pub.Log.debug("获取上月最后一天的日期:" + tt.getPreviousMonthEnd());
//		com.sinosoft.util.pub.Log.debug("获取下月第一天日期:" + tt.getNextMonthFirst());
//		com.sinosoft.util.pub.Log.debug("获取下月最后一天日期:" + tt.getNextMonthEnd());
//		com.sinosoft.util.pub.Log.debug("获取本年的第一天日期:" + tt.getCurrentYearFirst("yyyy-MM-dd HH:mm:ss"));
//		com.sinosoft.util.pub.Log.debug("获取本年最后一天日期:" + tt.getCurrentYearEnd());
//		com.sinosoft.util.pub.Log.debug("获取去年的第一天日期:" + tt.getPreviousYearFirst());
//		com.sinosoft.util.pub.Log.debug("获取去年的最后一天日期:" + tt.getPreviousYearEnd());
//		com.sinosoft.util.pub.Log.debug("获取明年第一天日期:" + tt.getNextYearFirst());
//		com.sinosoft.util.pub.Log.debug("获取明年最后一天日期:" + tt.getNextYearEnd());
//		com.sinosoft.util.pub.Log.debug("获取本季度第一天到最后一天:" + tt.getThisSeasonTime(11));
//		com.sinosoft.util.pub.Log.debug("获取两个日期之间间隔天数2008-12-1~2008-9.29:" + CalendarUtil.getTwoDay("2008-12-1", "2008-9-29"));
		
		System.out.println(CalendarUtil.getDate(new SimpleDateFormat("yyyyMMdd").parse("20130601"),"yyyy-MM-dd"));
	}

	/**
	 * 取得当前日期(yyyy)
	 * 
	 * @return String
	 */
	public static String getDateYear()
	{
		return new SimpleDateFormat("yyyy").format(new Date());
	}

	/**
	 * 取得当前日期(yyyyMMdd)
	 * 
	 * @return String
	 */
	public static String getDate()
	{
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}

	/**
	 * 取得当前时间(HH:mm:ss)
	 * 
	 * @return String
	 */
	public static String getTime()
	{
		return new SimpleDateFormat("HH:mm:ss").format(new Date());
	}

	/**
	 * 取得当前日期时间直至Millisecond(毫秒)的样式
	 * 
	 * @return String
	 */
	public static String getDateTimeMillisecond()
	{
		return new SimpleDateFormat("yyyyMMddHHmmssS").format(new Date());
	}

	/**
	 * 根据传入的值和格式返回Date类型数据
	 * 
	 * @return String
	 */
	public static Date parseTime(String value, String pattern) 
	{
		try
		{
			return new SimpleDateFormat(pattern).parse(value);
		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据传入的值和格式返回Date类型数据
	 * 
	 * @return String
	 */
	public static String parseTimeStr(String value, String pattern) 
	{
		try
		{
			return getDate(new SimpleDateFormat(pattern).parse(value),pattern);
		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 输入Date类型以指定格式输出日期
	 * 
	 * @param date
	 * @param designateformat
	 *          例:(yyyy-MM-dd)
	 * @return String
	 */
	public static String getDate(Date date, String designateformat)
	{
		if (date == null)
		{
			return "";
		}
		return new SimpleDateFormat(designateformat).format(date);
	}

	/**
	 * 输入日期获得n天以后的日期
	 * 
	 * @param dateString
	 * @param afterNum
	 * @param designateformat
	 *          例:(yyyy-MM-dd)
	 * @return String
	 */
	public static String getDateByAfter(String dateString, int afterNum, String designateformat)
	{
		Calendar calendar = Calendar.getInstance();
		try
		{
			Date date = new SimpleDateFormat(designateformat).parse(dateString);
			calendar.setTime(date);
		}
		catch (Exception e)
		{
			return null;
		}
		calendar.add(GregorianCalendar.DAY_OF_YEAR, afterNum);
		return new SimpleDateFormat(designateformat).format(calendar.getTime());
	}

	/**
	 * 计算两个日期的天数差值 间隔=Date1.getTime()-Date2.getTime();得出来的是毫秒数.
	 * 除1000是秒,再除60是分,再除60是小时
	 * 
	 * @param dateStrString
	 *          开始的日期 格式为"yyyy-MM-dd HH:mm:ss"
	 * @param dateEndString
	 *          结束的日期 格式为"yyyy-MM-dd HH:mm:ss"
	 * @throws ParseException
	 * @return:String
	 */
	public static String getDateDifference(String dateStrString, String dateEndString)
	{
		Date strdate = null;
		try
		{
			strdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStrString);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		Date enddate = null;
		try
		{
			enddate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateEndString);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}

		long beginTime = strdate.getTime();
		long endTime = enddate.getTime();
		long betweenDays = (long) ((endTime - beginTime) / (1000 * 60 * 60 * 24) + 0.5);

		return new Long(betweenDays).toString();
	}

	/**
	 * 计算两个日期的天数差值 间隔=Date1.getTime()-Date2.getTime();得出来的是毫秒数.
	 * 除1000是秒,再除60是分,再除60是小时
	 * 
	 * @param dateStrString
	 *          开始的日期
	 * @param dateEndString
	 *          结束的日期
	 * @param format格式
	 *          (如:yyyy-MM-dd)
	 * @throws ParseException
	 * @return:String
	 */
	public static String getDateDifference(String dateStrString, String dateEndString, String format)
	{
		Date strdate = null;
		try
		{
			strdate = new SimpleDateFormat(format).parse(dateStrString);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		Date enddate = null;
		try
		{
			enddate = new SimpleDateFormat(format).parse(dateEndString);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}

		long beginTime = strdate.getTime();
		long endTime = enddate.getTime();
		long betweenDays = (long) ((endTime - beginTime) / (1000 * 60 * 60 * 24) + 0.5);

		return new Long(betweenDays).toString();
	}

	/**
	 * 计算当前日期是否大于结束日期
	 * 
	 * @return:String
	 */
	public static boolean getDateDifferencelong(String dateStrString, String dateEndString)
	{
		Date strdate = null;
		try
		{
			strdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStrString);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		Date enddate = null;
		try
		{
			enddate = new SimpleDateFormat("yyyy-MM-dd").parse(dateEndString);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}

		long beginTime = strdate.getTime();
		long endTime = enddate.getTime();
		boolean count = false;
		if (beginTime > endTime)
		{
			count = true;
		}
		return count;
	}

	/**
	 * 输入一个日期获得当月的起始日期和结束日期
	 * 
	 * @param date
	 * @param designateformat
	 *          例:(yyyy-MM-dd)
	 * @return String[]
	 * @throws ParseException
	 */
	public static String[] getNowQuarter(String date, String designateformat) throws ParseException
	{
		String[] record = new String[2];
		String startDateStr = "";
		String endDateStr = "";

		String year = new SimpleDateFormat("yyyy").format(new SimpleDateFormat(designateformat).parse(date));
		String month = new SimpleDateFormat("MM").format(new SimpleDateFormat(designateformat).parse(date));
		int monthInteger = Integer.parseInt(month);

		if (monthInteger == 1 || monthInteger == 3 || monthInteger == 5 || monthInteger == 7 || monthInteger == 8 || monthInteger == 10 || monthInteger == 12)
		{
			startDateStr = "-" + month + "-01";
			endDateStr = "-" + month + "-31";
		}
		if (monthInteger == 4 || monthInteger == 6 || monthInteger == 9 || monthInteger == 11)
		{
			startDateStr = "-" + month + "-01";
			endDateStr = "-" + month + "-30";
		}
		if (monthInteger == 2)
		{
			startDateStr = "-" + month + "-01";
			endDateStr = "-" + month + "-29";
		}

		record[0] = year + startDateStr;
		record[1] = year + endDateStr;

		return record;
	}

	/**
	 * 用于获得默认格式的当前日期
	 * 
	 * @return String 当前日期
	 */
	public static String getCurrentDate()
	{
		return getCurrentDate("yyyy-MM-dd");
	}

	/**
	 * 用于获得默认格式的当前日期
	 * 
	 * @return String 当前日期
	 */
	public static String getCurrentDateTime()
	{
		return getCurrentDate("yyyy-MM-dd KK:mm:ss");
	}

	/**
	 * 用于获得默认格式的当前日期24小时
	 * 
	 * @author LiangYang
	 * @return String 当前日期
	 */
	public static String getCurrentDateTime24()
	{
		return getCurrentDate("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 用于获得指定格式的当前日期
	 * 
	 * @param format
	 *          日期格式
	 * @return String 当前日期
	 */
	public static String getCurrentDate(String format)
	{
		String currentDate = "";
		try
		{
			Calendar calendar = GregorianCalendar.getInstance();
			SimpleDateFormat simpleDateFormat;
			Date date = calendar.getTime();
			simpleDateFormat = new SimpleDateFormat(format);
			currentDate = simpleDateFormat.format(date);

		}
		catch (Exception e)
		{
			currentDate = "";
		}
		return currentDate;

	}

	/**
	 * 用于获得指定格式的日期
	 * 
	 * 
	 * @param format
	 *          ,date 日期格式
	 * @return String 日期
	 */
	public static String getFormatDate(String format, Date date)
	{
		String formatDate = "";
		try
		{
			SimpleDateFormat simpleDateFormat;
			simpleDateFormat = new SimpleDateFormat(format);
			formatDate = simpleDateFormat.format(date);

		}
		catch (Exception e)
		{
			formatDate = "";
		}
		return formatDate;

	}

	/**
	 * 用于返回当前日期的下天的日期
	 * 
	 * @return String 当前日期的下天的日期
	 */
	public static String getNextDay()
	{
		return getFutureDay(getCurrentDate(), 1);
	}

	/**
	 * 用于返回指定日期的下天的日期
	 * 
	 * @param appDate
	 *          指定日期
	 * @return 指定日期的下天的日期
	 */
	public static String getNextDay(String appDate)
	{
		return getFutureDay(appDate, "yyyy-MM-dd", 1);
	}

	/**
	 * 用于返回指定日期增加指定天数的日期
	 * 
	 * @param appDate
	 *          指定日期
	 * @param days
	 *          指定天数
	 * @return 指定日期增加指定天数的日期
	 * 
	 */
	public static String getFutureDay(String appDate, int days)
	{
		return getFutureDay(appDate, "yyyy-MM-dd", days);
	}

	/**
	 * 用于返回指定日期格式的日期增加指定天数的日期
	 * 
	 * @param appDate
	 *          指定日期
	 * @param format
	 *          指定日期格式
	 * @param days
	 *          指定天数
	 * @return 指定日期格式的日期增加指定天数的日期
	 */
	public static String getFutureDay(String appDate, String format, int days)
	{
		String future = "";
		try
		{
			Calendar calendar = GregorianCalendar.getInstance();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			Date date = simpleDateFormat.parse(appDate);
			calendar.setTime(date);
			calendar.add(Calendar.DATE, days);
			date = calendar.getTime();
			future = simpleDateFormat.format(date);
		}
		catch (Exception e)
		{

		}

		return future;
	}

	/**
	 * 用于获得当前时间的毫秒
	 * 
	 * @return Long 当前时间的毫秒
	 * 
	 */
	public static Long currentTimeMillis()
	{
		return new Long(System.currentTimeMillis());
	}

	/**
	 * 计算两个日期相差的天数(后面的传进的日期减去前面的日期)
	 * 
	 * @param fistDate
	 * @param secondDate
	 * @return
	 */
	public static int getBetweenDays(String begin, String end)
	{
		if (begin == null || end == null || begin.equals("") || end.equals(""))
		{
			return 0;
		}
		String beginDate = StringUtils.nullStrToDefau(begin, "");
		String endDate = StringUtils.nullStrToDefau(end, "");
		if (beginDate.equals(endDate))
		{
			return 0;
		}
		int days = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try
		{
			Date sdate = sdf.parse(begin);
			Date edate = sdf.parse(end);
			long times = edate.getTime() - sdate.getTime();
			days = (int) (times / 86400000);// 24 * 60 * 60 * 1000 = 86400000
			// if(days < 0)
			// {
			// days = days+(-1);
			// }
			// else
			// {
			// days = days+0;
			// }
		}
		catch (ParseException pe)
		{
			pe.printStackTrace();
		}
		return days;
	}

	/**
	 * 计算两个日期相差的秒数
	 * 
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getBetweenSeconds(String begin, String end)
	{
		if (begin == null || end == null)
		{
			return 0;
		}
		int seconds = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			Date sdate = sdf.parse(begin);
			Date edate = sdf.parse(end);
			long times = edate.getTime() - sdate.getTime();
			seconds = (int) (times / 1000);// 24 * 60 * 60 * 1000 = 86400000
		}
		catch (ParseException pe)
		{
			pe.printStackTrace();
		}
		return seconds;
	}

	/**
	 * 计算两个日期相差的秒
	 * 
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getBetweenSeconds24(String begin, String end)
	{
		if (begin == null || end == null)
		{
			return 0;
		}
		int seconds = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			Date sdate = sdf.parse(begin);
			Date edate = sdf.parse(end);
			long times = edate.getTime() - sdate.getTime();
			seconds = (int) (times / 1000);// 24 * 60 * 60 * 1000 = 86400000
		}
		catch (ParseException pe)
		{
			pe.printStackTrace();
		}
		return seconds;
	}

	/**
	 * 计算两个日期相差的秒
	 * 
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getBetweenMilliSeconds(String begin, String end)
	{
		if (begin == null || end == null)
		{
			return 0;
		}
		int milliSeconds = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			Date sdate = sdf.parse(begin);
			Date edate = sdf.parse(end);
			long times = edate.getTime() - sdate.getTime();
			milliSeconds = (int) times;// 24 * 60 * 60 * 1000 = 86400000
		}
		catch (ParseException pe)
		{
			pe.printStackTrace();
		}
		return milliSeconds;
	}

	/**
	 * 返回指定月的第一天是星期星期日到星期六为1-7,week of first day in month
	 * 
	 * @param calendar
	 * @return
	 */
	public static int getWFDM(Calendar calendar)
	{
		if (calendar == null)
		{
			return 0;
		}
		Calendar cal = (Calendar) calendar.clone();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return cal.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 第一个参数跟第二个参数相比较 第二个参数要是比第一个参数大 就返回true 小就返回flase
	 * 
	 * @param baseTime
	 *          基准时间
	 * @param realTime
	 *          比较时间
	 * @return
	 */
	public static boolean after(String baseTime, String realTime, String format)
	{
		boolean after = false;
		try
		{
			Calendar base = GregorianCalendar.getInstance();
			base.setTime(toDate(baseTime, format));

			Calendar real = GregorianCalendar.getInstance();
			real.setTime(toDate(realTime, format));
			if (real.after(base))
			{
				after = true;
			}
			if(baseTime.equals(realTime))
			{
				after = true;
			}
		}
		catch (Exception e)
		{
			// TODO: handle exception
		}
		return after;
	}
	
	/**
	 * 第一个参数跟第二个参数相比较 第二个参数要是比第一个参数大 就返回true 小就返回flase
	 * 
	 * @param baseTime
	 *          基准时间
	 * @param realTime
	 *          比较时间
	 * @return
	 */
	public static boolean after2(String baseTime, String realTime, String format)
	{
		boolean after = false;
		try
		{
			Calendar base = GregorianCalendar.getInstance();
			base.setTime(toDate(baseTime, format));

			Calendar real = GregorianCalendar.getInstance();
			real.setTime(toDate(realTime, format));
			if (real.after(base))
			{
				after = true;
			}
			if(baseTime.equals(realTime))
			{
				after = false;
			}
		}
		catch (Exception e)
		{
			// TODO: handle exception
		}
		return after;
	}

	/**
	 * 比较realTime是否比baseTime,如果是则返回true,如果否则返回false
	 * 
	 * @param baseTime
	 *          基准时间
	 * @param realTime
	 *          比较时间
	 * @return
	 */
	public static boolean after(Calendar baseTime, Calendar realTime)
	{
		boolean after = false;
		try
		{
			if (realTime.after(baseTime))
			{
				after = true;
			}
		}
		catch (Exception e)
		{
			// TODO: handle exception
		}
		return after;
	}

	/**
	 * 比较realTime是否比baseTime,如果是则返回true,如果否则返回false
	 * 
	 * @param baseTime
	 *          基准时间
	 * @param realTime
	 *          比较时间
	 * @return
	 */
	public static boolean after(Date baseTime, Date realTime)
	{
		boolean after = false;
		try
		{
			if (realTime.after(baseTime))
			{
				after = true;
			}
		}
		catch (Exception e)
		{
			// TODO: handle exception
		}
		return after;
	}

	/**
	 * 是否是有效的时间
	 * 
	 * @param time
	 * @param format
	 * @return
	 */
	public static boolean isValidTime(String time, String format)
	{
		boolean valid = false;
		try
		{
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			simpleDateFormat.parse(time);
			valid = true;
		}
		catch (Exception e)
		{

		}
		return valid;
	}

	/**
	 * 把字符串转换为
	 * 
	 * @param value
	 * @param format
	 * @return
	 */
	public static Date toDate(String value, String format)
	{
		Date date = new Date();
		try
		{
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			date = simpleDateFormat.parse(value);
		}
		catch (Exception e)
		{

		}
		return date;
	}

	/**
	 * 把日期转换为字符
	 * 
	 * @param value
	 * @param format
	 * @return
	 */
	public static String toString(Date value, String format)
	{
		String date = "";
		try
		{
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			date = simpleDateFormat.format(value);
		}
		catch (Exception e)
		{

		}
		return date;
	}

	public static Calendar toCalendar(String value, String format)
	{
		Calendar calendar = GregorianCalendar.getInstance();
		try
		{
			Date date1 = toDate(value, format);
			// int a = date1.getHours();

			calendar.setTime(date1);
		}
		catch (Exception e)
		{
			// TODO: handle exception
		}
		return calendar;
	}

	/**
	 * 以指定的格式去格式化日期
	 * 
	 * @param value
	 * @param format
	 * @return
	 */
	public static String format(String value, String format)
	{
		if (value == null || format == null)
		{
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try
		{
			date = sdf.parse(value);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return sdf.format(date);
	}

	/**
	 * 得到二个日期间的间隔天数
	 */
	public static long getTwoDay(String sj1, String sj2)
	{
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		long day = 0;
		try
		{
			java.util.Date date = myFormatter.parse(sj1);
			java.util.Date mydate = myFormatter.parse(sj2);
			day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		}
		catch (Exception e)
		{
			return -1;
		}
		return day;
	}

	/**
	 * 根据一个日期,返回是星期几的字符串
	 * 
	 * @param sdate
	 * @return
	 */
	public static String getWeek(String sdate)
	{
		// 再转换为时间
		Date date = CalendarUtil.strToDate(sdate,"yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		// int hour=c.get(Calendar.DAY_OF_WEEK);
		// hour中存的就是星期几了,其范围 1~7
		// 1=星期日 7=星期六,其他类推
		return new SimpleDateFormat("EEEE").format(c.getTime());
	}

	/**
	 * 将短时间格式字符串转换为时间
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDate(String strDate, String format)
	{
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * 两个时间之间的天数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getDays(String date1, String date2)
	{
		if (date1 == null || date1.equals(""))
			return 0;
		if (date2 == null || date2.equals(""))
			return 0;
		// 转换为标准时间
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = null;
		java.util.Date mydate = null;
		try
		{
			date = myFormatter.parse(date1);
			mydate = myFormatter.parse(date2);
		}
		catch (Exception e)
		{
		}
		long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		return day;
	}

	// 计算当月最后一天,返回字符串
	public String getDefaultDay()
	{
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号
		lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天

		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 上月第一天
	public String getPreviousMonthFirst()
	{
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.add(Calendar.MONTH, -1);// 减一个月,变为下月的1号
		// lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天

		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获取当月第一天
	public String getFirstDayOfMonth()
	{
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得本周星期日的日期
	public String getCurrentWeekday()
	{
		weeks = 0;
		int mondayPlus = this.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
		Date monday = currentDate.getTime();

		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	// 获取当天时间
	public static String getNowTime(String dateformat)
	{
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);// 可以方便地修改日期格式
		String hehe = dateFormat.format(now);
		return hehe;
	}

	// 获得当前日期与本周日相差的天数
	private int getMondayPlus()
	{
		Calendar cd = Calendar.getInstance();
		// 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1)
		{
			return 0;
		}
		else
		{
			return 1 - dayOfWeek;
		}
	}

	// 获得本周一的日期
	public String getMondayOFWeek()
	{
		weeks = 0;
		int mondayPlus = this.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus);
		Date monday = currentDate.getTime();

		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	// 获得相应周的周六的日期
	public String getSaturday()
	{
		int mondayPlus = this.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6);
		Date monday = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	// 获得上周星期日的日期
	public String getPreviousWeekSunday()
	{
		weeks = 0;
		weeks--;
		int mondayPlus = this.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + weeks);
		Date monday = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	// 获得上周星期一的日期
	public String getPreviousWeekday()
	{
		weeks--;
		int mondayPlus = this.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
		Date monday = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	// 获得下周星期一的日期
	public String getNextMonday()
	{
		weeks++;
		int mondayPlus = this.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
		Date monday = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	// 获得下周星期日的日期
	public String getNextSunday()
	{

		int mondayPlus = this.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 + 6);
		Date monday = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	private int getMonthPlus()
	{
		Calendar cd = Calendar.getInstance();
		int monthOfNumber = cd.get(Calendar.DAY_OF_MONTH);
		cd.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		cd.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
		MaxDate = cd.get(Calendar.DATE);
		if (monthOfNumber == 1)
		{
			return -MaxDate;
		}
		else
		{
			return 1 - monthOfNumber;
		}
	}

	// 获得上月最后一天的日期
	public String getPreviousMonthEnd()
	{
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, -1);// 减一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得下个月第一天的日期
	public String getNextMonthFirst()
	{
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, 1);// 减一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得下个月最后一天的日期
	public String getNextMonthEnd()
	{
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, 1);// 加一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得明年最后一天的日期
	public String getNextYearEnd()
	{
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.YEAR, 1);// 加一个年
		lastDate.set(Calendar.DAY_OF_YEAR, 1);
		lastDate.roll(Calendar.DAY_OF_YEAR, -1);
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得明年第一天的日期
	public String getNextYearFirst()
	{
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.YEAR, 1);// 加一个年
		lastDate.set(Calendar.DAY_OF_YEAR, 1);
		str = sdf.format(lastDate.getTime());
		return str;

	}

	// 获得本年有多少天
	private int getMaxYear()
	{
		Calendar cd = Calendar.getInstance();
		cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天
		cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滚一天。
		int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
		return MaxYear;
	}

	private static int getYearPlus()
	{
		Calendar cd = Calendar.getInstance();
		int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);// 获得当天是一年中的第几天
		cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天
		cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滚一天。
		int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
		if (yearOfNumber == 1)
		{
			return -MaxYear;
		}
		else
		{
			return 1 - yearOfNumber;
		}
	}

	// 获得本年第一天的日期
	public static String getCurrentYearFirst(String format)
	{
		int yearPlus = getYearPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, yearPlus);
		Date yearDay = currentDate.getTime();
		// DateFormat df = DateFormat.getDateInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);// 可以方便地修改日期格式
		String preYearDay = dateFormat.format(yearDay);
		return preYearDay;
	}

	// 获得本年最后一天的日期 *
	public String getCurrentYearEnd()
	{
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
		String years = dateFormat.format(date);
		return years + "-12-31";
	}

	// 获得上年第一天的日期 *
	public static String getPreviousYearFirst()
	{
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
		String years = dateFormat.format(date);
		int years_value = Integer.parseInt(years);
		years_value--;
		return years_value + "-01-01";
	}

	// 获得上年最后一天的日期
	public String getPreviousYearEnd()
	{
		weeks--;
		int yearPlus = this.getYearPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, yearPlus + MaxYear * weeks + (MaxYear - 1));
		Date yearDay = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preYearDay = df.format(yearDay);
		getThisSeasonTime(11);
		return preYearDay;
	}

	// 获得本季度
	public String getThisSeasonTime(int month)
	{
		int array[][] =
		{
		{ 1, 2, 3 },
		{ 4, 5, 6 },
		{ 7, 8, 9 },
		{ 10, 11, 12 } };
		int season = 1;
		if (month >= 1 && month <= 3)
		{
			season = 1;
		}
		if (month >= 4 && month <= 6)
		{
			season = 2;
		}
		if (month >= 7 && month <= 9)
		{
			season = 3;
		}
		if (month >= 10 && month <= 12)
		{
			season = 4;
		}
		int start_month = array[season - 1][0];
		int end_month = array[season - 1][2];

		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
		String years = dateFormat.format(date);
		int years_value = Integer.parseInt(years);

		int start_days = 1;// years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);
		int end_days = getLastDayOfMonth(years_value, end_month);
		String seasonDate = years_value + "-" + start_month + "-" + start_days + ";" + years_value + "-" + end_month + "-" + end_days;
		return seasonDate;

	}

	/**
	 * 获取某年某月的最后一天
	 * 
	 * @param year
	 *          年
	 * @param month
	 *          月
	 * @return 最后一天
	 */
	private int getLastDayOfMonth(int year, int month)
	{
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
		{
			return 31;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11)
		{
			return 30;
		}
		if (month == 2)
		{
			if (isLeapYear(year))
			{
				return 29;
			}
			else
			{
				return 28;
			}
		}
		return 0;
	}

	/**
	 * 是否闰年
	 * 
	 * @param year
	 *          年
	 * @return
	 */
	public boolean isLeapYear(int year)
	{
		return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值