Java Date query: Quarterly, month, week, etc time information

Source: Internet
Author: User
Package com.stt.dateChange;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;

Import Java.util.Date;
Import Org.apache.commons.logging.Log;

Import Org.apache.commons.logging.LogFactory; /** * * @author zengms * @date 2012-12-27 * */public class Dateutils {private static final Log logger = logfact

	Ory.getlog (Dateutils.class);

	public static final String YYYYMMDD = "YYYY-MM-DD";

	public static final String Yyyymmdd_zh = "yyyy year mm month DD Day"; public static final int first_day_of_week = Calendar.monday; China Monday is the first day of the week/** * * @param strdate * @return/public static Date parsedate (String strdate) {return par
	Sedate (strdate, NULL); /** * parsedate * * * @param strdate * @param pattern * @return/public static Date parsedate (String str
		Date, String pattern) {Date date = null;
			try {if (pattern = = NULL) {pattern = YYYYMMDD;
			SimpleDateFormat format = new SimpleDateFormat (pattern);
		Date = Format.parse (strdate); } CAtch (Exception e) {logger.error ("Parsedate error:" + E);
	} return date; /** * Format Date * * @param date * @return/public static String formatdate (date date) {return forma
	Tdate (date, null); /** * Format Date * * @param date * @param pattern * @return/public static String formatdate (Date dat
		E, String pattern) {string strdate = null;
			try {if (pattern = = NULL) {pattern = YYYYMMDD;
			SimpleDateFormat format = new SimpleDateFormat (pattern);
		Strdate = Format.format (date);
		catch (Exception e) {logger.error ("FormatDate error:", e);
	return strdate; /** * Acquisition Date: Year * * @param date * @return/public static int getyear (date date) {Calendar c = calendar.g
		Etinstance ();
		C.settime (date);
		int year = C.get (calendar.year);
	return to year; /** * Acquisition Date: Year * * @param date * @return/public static int getmonth (date date) {Calendar c = Calendar.
		GetInstance (); C.settime (date);
		int month = C.get (calendar.month);
	Return month + 1; /** * Acquisition Date: Year * * @param date * @return/public static int getday (date date) {Calendar c = calendar.ge
		Tinstance ();
		C.settime (date);
		int da = c.get (calendar.day_of_month);
	Return da; /** * The date of the day is week * * @param date * @return/public static int Getweekday (date date) {Calendar c = Cale
		Ndar.getinstance ();
		C.settime (date);
		int week_of_year = C.get (Calendar.day_of_week);
	return week_of_year-1; /** * Obtained the first weeks of the year * * @param date * @return/public static int getweekofyear (date date) {Calendar c = Ca
		Lendar.getinstance ();
		C.settime (date);
		int week_of_year = C.get (calendar.week_of_year);
	return week_of_year; /** * getweekbeginandenddate * * @param date * @param pattern * @return/public static String getweekb
		Eginandenddate (date date, String pattern) {Date Monday = Getmondayofweek (date);
		Date Sunday = Getsundayofweek (date); return foRmatdate (Monday, pattern) + "-" + formatdate (Sunday, pattern); /** * Date * @param date * @return/public static date Getmondayofweek of the week-week 1st * * * * on the basis of dates {Calenda
		R Monday = Calendar.getinstance ();
		Monday.settime (date);
		Monday.setfirstdayofweek (First_day_of_week);
		Monday.set (Calendar.day_of_week, calendar.monday);
	return Monday.gettime (); /** * Date * @param date * @return/public static date (date Getsundayofweek) {Calenda
		R Sunday = Calendar.getinstance ();
		Sunday.settime (date);
		Sunday.setfirstdayofweek (First_day_of_week);
		Sunday.set (Calendar.day_of_week, calendar.sunday);
	return Sunday.gettime (); /** * The remaining days of the month * * @param date * @return/public static int getremaindayofmonth (date date) {int dayof
		Month = GetDayOfMonth (date);
		int day = Getpassdayofmonth (date);
	return dayofmonth-day; /** * The number of days in which the month has elapsed * * @param date * @return/public static int Getpassdayofmonth(Date date)
		{Calendar c = calendar.getinstance ();
		C.settime (date);
	Return C.get (Calendar.day_of_month); /** * Number of months * * @param date * @return/public static int GetDayOfMonth (date date) {Calendar c = Calen
		Dar.getinstance ();
		C.settime (date);
	Return C.getactualmaximum (Calendar.day_of_month);  /** * The first day of the month * * @param date * @return/public static date Getfirstdateofmonth (date date) {Calendar C
		= Calendar.getinstance ();
		C.settime (date);
		C.set (Calendar.day_of_month, C.getactualminimum (Calendar.day_of_month));
	return C.gettime ();  /** * The last day of the month * * @param date * @return/public static date Getlastdateofmonth (date date) {Calendar C
		= Calendar.getinstance ();
		C.settime (date);
		C.set (Calendar.day_of_month, C.getactualmaximum (Calendar.day_of_month));
	return C.gettime (); /** * made the first day of the quarter * * @param date * @return/public static date Getfirstdateofseason (date date) {return g Etfirstdateofmonth (GETseasondate (date) [0]); /** * Get Quarterly Last day * * @param date * @return/public static date Getlastdateofseason (date date) {return g
	Etlastdateofmonth (getseasondate (date) [2]);
		/** * Get Quarterly days * * @param date * @return/public static int Getdayofseason (date date) {int day = 0;
		date[] Seasondates = getseasondate (date);
		for (Date date2:seasondates) {Day + = GetDayOfMonth (date2);
	Return day; /** * Get Quarterly remaining days * * @param date * @return/public static int Getremaindayofseason (date date) {return g
	Etdayofseason (date)-Getpassdayofseason (date); /** * Obtained quarterly past days * * @param date * @return/public static int Getpassdayofseason (date date) {int day =

		0;

		date[] Seasondates = getseasondate (date);
		Calendar C = calendar.getinstance ();
		C.settime (date);

		int month = C.get (calendar.month); if (month = = Calendar.january | | month = = Calendar.april | | month = = Calendar.july | | month = calendar.october) {//Quarter first Month day = Getpassdayofmonth (Seasondates[0]); else if (month = Calendar.february | | month = Calendar.may | | month = Calendar.august | | month = CALENDAR.NOVEMB
		ER) {//quarterly second Month day = GetDayOfMonth (Seasondates[0]) + getpassdayofmonth (seasondates[1]); else if (month = Calendar.march | | month = Calendar.june | | month = Calendar.september | | month = CALENDAR.DECEM BER) {//quarterly third Month day = GetDayOfMonth (Seasondates[0]) + getdayofmonth (seasondates[1]) + getpassdayofmonth (seasondate
		S[2]);
	Return day;  /** * Obtained quarterly month * * @param date * @return/public static date[] Getseasondate (date date) {date[] season =

		New Date[3];
		Calendar C = calendar.getinstance ();

		C.settime (date);
		int Nseason = Getseason (date);
			if (Nseason = = 1) {//First quarter C.set (Calendar.month, calendar.january);
			Season[0] = C.gettime ();
			C.set (Calendar.month, calendar.february);
			SEASON[1] = C.gettime ();
			C.set (Calendar.month, Calendar.march); SEASON[2] = C.gettime ();
			else if (Nseason = 2) {//second quarter C.set (Calendar.month, Calendar.april);
			Season[0] = C.gettime ();
			C.set (Calendar.month, Calendar.may);
			SEASON[1] = C.gettime ();
			C.set (Calendar.month, Calendar.june);
		SEASON[2] = C.gettime ();
			else if (Nseason = 3) {//third quarter c.set (Calendar.month, calendar.july);
			Season[0] = C.gettime ();
			C.set (Calendar.month, calendar.august);
			SEASON[1] = C.gettime ();
			C.set (Calendar.month, Calendar.september);
		SEASON[2] = C.gettime ();
			else if (Nseason = 4) {//Fourth quarter C.set (Calendar.month, Calendar.october);
			Season[0] = C.gettime ();
			C.set (Calendar.month, Calendar.november);
			SEASON[1] = C.gettime ();
			C.set (Calendar.month, Calendar.december);
		SEASON[2] = C.gettime ();
	Return season;  /** * * 1 First quarter 2 second quarter 3 third quarter 4 quarter * * @param date * @return/public static int Getseason (date date)

		{int season = 0;
		Calendar C = calendar.getinstance ();
		C.settime (date); Int month= C.get (Calendar.month);
			Switch (month) {case Calendar.JANUARY:case Calendar.FEBRUARY:case Calendar.MARCH:season = 1;
		Break
			Case Calendar.APRIL:case Calendar.MAY:case Calendar.JUNE:season = 2;
		Break
			Case Calendar.JULY:case Calendar.AUGUST:case Calendar.SEPTEMBER:season = 3;
		Break
			Case Calendar.OCTOBER:case Calendar.NOVEMBER:case Calendar.DECEMBER:season = 4;
		Break
		Default:break;
	Return season;

		public static void Main (string[] args) {String strdate = ' 2013-01-01 ';

		Date date = Parsedate (strdate); System.out.println (strdate + "What year is it today?")
		"+ getyear (date)); System.out.println (strdate + "What month is it today?")
		"+ getmonth (date)); System.out.println (Strdate + "today is the date.)
		"+ getday (date)); System.out.println (Strdate + "Today is the week.")
		"+ getweekday (date)); System.out.println (Strdate + "is the first few weeks of the year.)
		"+ getweekofyear (date)); System.out.println (Strdate +) the start of the week from the end date.
		"+ getweekbeginandenddate (date," yyyy mm month DD day ")); SYstem.out.println (Strdate + "in the Week Monday" is.
		"+ FormatDate (Getmondayofweek (date));" System.out.println (Strdate + "in the Week Sunday" is.

		"+ FormatDate (Getsundayofweek (date));" System.out.println (strdate + "first day of the month" date.
		"+ FormatDate (getfirstdateofmonth (date));" System.out.println (strdate + "The date of the last day of the month."
		"+ FormatDate (getlastdateofmonth (date));" System.out.println (strdate + "Days of the month.")
		"+ getdayofmonth (date)); System.out.println (strdate + ") How many days have passed in the month.
		"+ getpassdayofmonth (date)); System.out.println (strdate + "The number of days remaining in the month."

		"+ getremaindayofmonth (date)); System.out.println (Strdate +) The first day of the quarter.
		"+ FormatDate (Getfirstdateofseason (date));" System.out.println (strdate + "The date of the last day of the quarter."
		"+ FormatDate (Getlastdateofseason (date));" System.out.println (strdate +) The number of days of the quarter.
		"+ Getdayofseason (date)); System.out.println (Strdate + ") The number of days after the quarter.
		"+ Getpassdayofseason (date)); System.out. println (strdate +) The number of days remaining in the quarter.
		"+ Getremaindayofseason (date)); System.out.println (Strdate + "is the first quarter.") "+ Getseason (date)); System.out.println (strdate +) quarter month.
				"+ FormatDate (getseasondate (date) [0]," yyyy mm month ") +"/"+ FormatDate (getseasondate (date) [1]," yyyy year mm month ") +"/"
	+ FormatDate (getseasondate (date) [2], "yyyy mm month")); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.