Java Date and Calendar instances

Source: Internet
Author: User

Java Date and Calendar instances

Package com. homer. learn; </p> <p> import java. text. dateFormat; <br/> import java. text. parsePosition; <br/> import java. text. simpleDateFormat; <br/> import java. util. calendar; <br/> import java. util. date; <br/> import java. util. gregorianCalendar; </p> <p> public class TimeTest {<br/> public static void main (String [] args) {<br/> TimeTest tt = new TimeTest (); </p> <p> System. out. println ("\ n current date:"); <br/> System. ou T. println (tt. getNowTime (); // obtain the current time <br/> System. out. println (tt. getNowTime2 (); // obtain the current time <br/> System. out. println (tt. getNowTime3 (); // obtain the current time </p> <p> System. out. println ("\ n computing week:"); <br/> System. out. println (tt. getDaysOfNow2SundayInWeek (); // obtain the number of days between the current date and the current day. <br/> System. out. println (tt. getDateOfMondayInWeek (); // obtain the date of this Monday <br/> System. out. println (tt. getDateOfSaturdayInWeek (); // obtain the Saturday date <br/> System. out. p Rintln (tt. getDateOfSaturdayInNextWeek (); // obtain the Date of next Monday <br/> System. out. println (tt. getDateOfSaturdayInLastWeek (); // obtain the date of last Monday </p> <p> System. out. println ("\ n calculation month:"); <br/> System. out. println (tt. getFirstDayOfMonth (); // obtain the first day of the current month <br/> System. out. println (tt. getLastDayOfMonth (); // obtain the last day of the month <br/> System. out. println (tt. getFirstDayOfPreviousMonth (); // obtain the first day of the previous month <br/> System. out. println (tt. getLastDayOfPreviousMont H (); // obtain the last day of the previous month <br/> System. out. println (tt. getFirstDayOfNextMonth (); // obtain the first day of next month <br/> System. out. println (tt. getLastDayOfNextMonth (); // obtain the last day of the next month </p> <p> System. out. println ("\ n calculation year:"); <br/> System. out. println (tt. getFirstDayOfYear (); // obtain the first day of the year <br/> System. out. println (tt. getLastDayOfYear (); // obtain the last day of the year <br/> System. out. println (tt. getFirstDayOfPreviousYear (); // obtain the first day of the previous year <br/> System. out. println (tt. getL AstDayOfPreviousYear (); // obtain the last day of the previous year <br/> System. out. println (tt. getFirstDayOfNextYear (); // obtain the first day of the next year <br/> System. out. println (tt. getLastDayOfNextYear (); // obtain the last day of the next year <br/> System. out. println (tt. getDaysOfYear (); // obtain the number of days of the current year </p> <p> System. out. println ("\ n calculation quarter:"); <br/> System. out. println (tt. getSeasonOfMonth (); // obtain the quarter of the current month <br/> System. out. println (tt. getLastDayOfNow (); // obtain the number of days in the month of the current day <br/> System. out. printl N (tt. isLeapYear (2012); // determines whether it is a runyear </p> <p> System. out. println ("\ n date format conversion and calculation:"); <br/> System. out. println (tt. getDateFromStr ("2012-06-20"); // converts the string time format yyyy-MM-dd to the Date type. <br/> System. out. println (tt. getWeek ("2012-06-20"); // returns the string of the day of the week based on a date. <br/> System. out. print ("-> 2012-06-12 days interval:"); // calculate the number of days interval between two days <br/> System. out. println (tt. getDaysFromTwoDate ("2012-06-02", "2012-06-12"); <br/>}</p> <p>/ /Obtain the current time <br/> public String getNowTime () {<br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd "); </p> <p> Date now = new Date (); <br/> return sDateFormat. format (now); <br/>}</p> <p> public String getNowTime2 () {<br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); </p> <p> Date now = new Date (System. currentTimeMillis (); <br/> return sDateFormat. format (now); <br />}</P> <p> public String getNowTime3 () {<br/> StringBuilder strBuildNow = new StringBuilder (); </p> <p> Calendar calendar = Calendar. getInstance (); <br/> int year = calendar. get (Calendar. YEAR); <br/> int month = calendar. get (Calendar. MONTH) + 1; <br/> int day = calendar. get (Calendar. DAY_OF_MONTH); <br/> int hour = calendar. get (Calendar. HOUR_OF_DAY); <br/> int minute = calendar. get (Calendar. MINUTE); <br/> in T second = calendar. get (Calendar. SECOND); <br/> int millissecond = calendar. get (Calendar. MILLISECOND); </p> <p> strBuildNow. append (year + "-"); <br/> strBuildNow. append (month + "-"); <br/> strBuildNow. append (day + ""); <br/> strBuildNow. append (hour + ":"); <br/> strBuildNow. append (minute + ":"); <br/> strBuildNow. append (second + ". "); <br/> strBuildNow. append (millissecond); </p> <p> return strBuildNow. toStrin G (); <br/>}</p> <p> // obtain the number of days between the current date and the current Sunday. <br/> public int getDaysOfNow2SundayInWeek () {<br/> Calendar calendar = Calendar. getInstance (); </p> <p> int dayOfWeek = calendar. get (Calendar. DAY_OF_WEEK)-1; // minus one day, Monday is 1, in line with Chinese habits. Sunday-1; Monday-2; Saturday-7 <br/> if (dayOfWeek = 0) {// Sunday <br/> return 0; <br/>} else {<br/> return 0-dayOfWeek; <br/>}</p> <p> // obtain the date of the current Monday. <br/> public String getDateOfMondayInWeek () {<br/> int day = this. getDaysOfNow2SundayInWeek () + 1; // Add 1, that is, the number of days between Monday and Sunday <br/> GregorianCalendar gCalendar = new GregorianCalendar (); </p> <p> gCalendar. add (GregorianCalendar. DATE, day); // time interval between calculation and the current Monday <br/> Date curDay = gCalendar. getTime (); </p> <p> DateFormat dateFormat = DateFormat. getInstance (); <br/> String dateOfMonday = dateFormat. format (curDay); </p> <p> return dateOfMonday; <br/>}</p> <p> // obtain the Saturday date <br/> public String getDateOfSaturdayInWeek () {<br/> int day = this. getDaysOfNow2SundayInWeek () + 6; // Add 6, that is, the number of days between Saturday and Sunday <br/> GregorianCalendar gCalendar = new GregorianCalendar (); </p> <p> gCalendar. add (Grego RianCalendar. DATE, day); // interval between calculation and the current Saturday <br/> Date curDay = gCalendar. getTime (); </p> <p> DateFormat dateFormat = DateFormat. getInstance (); <br/> String dateOfMonday = dateFormat. format (curDay); </p> <p> return dateOfMonday; <br/>}</p> <p> // obtain the date of last Monday <br/> public String getDateOfSaturdayInLastWeek () {<br/> int day = this. getDaysOfNow2SundayInWeek ()-1; // minus 1, that is, the number of days between the last Monday and the current Sunday <br/> GregorianCalendar gCalendar = New GregorianCalendar (); </p> <p> gCalendar. add (GregorianCalendar. DATE, day); // interval between calculation and last Monday <br/> Date curDay = gCalendar. getTime (); </p> <p> DateFormat dateFormat = DateFormat. getInstance (); <br/> String dateOfMonday = dateFormat. format (curDay); </p> <p> return dateOfMonday; <br/>}</p> <p> // obtain the Date of next Monday. <br/> public String getDateOfSaturdayInNextWeek () {<br/> int day = this. getDaysOfNow2SundayInWeek () + 8; // Add 8 That is, the number of days between Monday and Sunday <br/> GregorianCalendar gCalendar = new GregorianCalendar (); </p> <p> gCalendar. add (GregorianCalendar. DATE, day); // interval between calculation and next Monday <br/> Date curDay = gCalendar. getTime (); </p> <p> DateFormat dateFormat = DateFormat. getInstance (); <br/> String dateOfMonday = dateFormat. format (curDay); </p> <p> return dateOfMonday; <br/>}</p> <p> // obtain the first day of the current month <br/> public String getFirstDayOfMonth () {<br/> String strF IrstDay = ""; <br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd"); </p> <p> Calendar calendar = Calendar ar. getInstance (); <br/> calendar. set (Calendar. DATE, 1); // set the number 1 of the current month </p> <p> strFirstDay = sDateFormat. format (calendar. getTime (); <br/> return strFirstDay; <br/>}</p> <p> // obtain the last day of the month <br/> public String getLastDayOfMonth () {<br/> String strLastDay = ""; <br/> SimpleDateFormat sDateFormat = New SimpleDateFormat ("yyyy-MM-dd"); </p> <p> Calendar calendar = Calendar ar. getInstance (); <br/> calendar. set (Calendar. DATE, 1); // set the number 1 of the current month <br/> calendar. add (Calendar. MONDAY, 1); // Add a month to become the first day of the next month <br/> calendar. add (Calendar. DATE,-1); // subtract one day to the last day of the current month </p> <p> strLastDay = sDateFormat. format (calendar. getTime (); <br/> return strLastDay; <br/>}</p> <p> // obtain the first day of last month <br/> public String getFirstDayOfPreviousMonth (){ <Br/> String strFirstDay = ""; <br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd "); </p> <p> Calendar calendar = Calendar. getInstance (); <br/> calendar. set (Calendar. DATE, 1); // set the number 1 of the current month <br/> calendar. add (Calendar. MONDAY,-1); // minus one month to the first day of the previous month </p> <p> strFirstDay = sDateFormat. format (calendar. getTime (); <br/> return strFirstDay; <br/>}</p> <p> // obtain the last day of the previous month <br/> public String getLastDayOfPre ViousMonth () {<br/> String strLastDay = ""; <br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd "); </p> <p> Calendar calendar = Calendar. getInstance (); <br/> calendar. set (Calendar. DATE, 1); // set the number 1 of the current month <br/> calendar. add (Calendar. DATE,-1); // minus one day, to the first day of the previous month </p> <p> strLastDay = sDateFormat. format (calendar. getTime (); <br/> return strLastDay; <br/>}</p> <p> public String getLastDayOfPreviousMont H2 () {<br/> String strLastDay = ""; <br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd "); </p> <p> Calendar calendar = Calendar. getInstance (); <br/> calendar. add (Calendar. MONDAY,-1); // minus one month <br/> calendar. set (Calendar. DATE, 1); // set the number 1 of the current month <br/> calendar. roll (Calendar. DATE,-1); // roll back the DATE for one day, that is, the last day of the month </p> <p> strLastDay = sDateFormat. format (calendar. getTime (); <br/> return strLastDay; <br/>} </P> <p> // obtain the first day of next month <br/> public String getFirstDayOfNextMonth () {<br/> String strFirstDay = ""; <br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd"); </p> <p> Calendar calendar = Calendar. getInstance (); <br/> calendar. add (Calendar. MONTH, 1); // Add a MONTH <br/> calendar. set (Calendar. DATE, 1); // set the first day of the current month </p> <p> strFirstDay = sDateFormat. format (calendar. getTime (); <br/> return strFirstDay; <br/>} </P> <p> // obtain the last day of the next month <br/> public String getLastDayOfNextMonth () {<br/> String strLastDay = ""; <br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd"); </p> <p> Calendar calendar = Calendar. getInstance (); <br/> calendar. set (Calendar. DATE, 1); // set the number 1 of the current month <br/> calendar. add (Calendar. MONDAY, 2); // Add two months to change to "1" next month <br/> calendar. add (Calendar. DATE,-1); // minus one day, to the last day of next month </p> <p> strLastDay = sDateFor Mat. format (calendar. getTime (); <br/> return strLastDay; <br/>}</p> <p> public String getlastdayofnextmon2() {<br/> String strLastDay = ""; <br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd"); </p> <p> Calendar calendar = Calendar. getInstance (); <br/> calendar. add (Calendar. MONTH, 1); // Add 1 MONTH <br/> calendar. set (Calendar. DATE, 1); // set the DATE to the 1st day of the current month <br/> calendar. roll (Calendar. DATE,-1); // returns the DATE Roll for one day, that is, the last day of the month </p> <p> strLastDay = sDateFormat. format (calendar. getTime (); <br/> return strLastDay; <br/>}</p> <p> // obtain the first day of the year <br/> public String getFirstDayOfYear () {<br/> String strFirstDay = ""; <br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd "); </p> <p> Calendar calendar = Calendar. getInstance (); <br/> calendar. set (Calendar. DAY_OF_YEAR, 1); // set the first day of the current year </p> <p> strFirstDay = sDateFormat. Format (calendar. getTime (); <br/> return strFirstDay; <br/>}</p> <p> // obtain the last day of the year <br/> public String getLastDayOfYear () {<br/> String strLastDay = ""; </p> <p> Calendar calendar = Calendar ar. getInstance (); <br/> strLastDay = calendar ar. get (Calendar. YEAR) + "-12-31"; </p> <p> return strLastDay; <br/>}</p> <p> // obtain the first day of the previous year <br/> public String getFirstDayOfPreviousYear () {<br/> String strFirstDay = ""; </p> <p> Calendar ca Lendar = Calendar. getInstance (); <br/> strFirstDay = (calendar. get (Calendar. YEAR)-1) + "-01-01"; </p> <p> return strFirstDay; <br/>}</p> <p> // obtain the last day of the previous year <br/> public String getLastDayOfPreviousYear () {<br/> String strLastDay = ""; </p> <p> Calendar calendar = Calendar. getInstance (); <br/> strLastDay = (calendar. get (Calendar. YEAR)-1) + "-12-31"; </p> <p> return strLastDay; <br/>}</p> <p> // obtain the first day of the next year. <br/> publi C String getFirstDayOfNextYear () {<br/> String strFirstDay = ""; <br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd "); </p> <p> Calendar calendar = Calendar. getInstance (); <br/> calendar. add (Calendar. YEAR, 1); // Add a YEAR <br/> calendar. set (Calendar. DAY_OF_YEAR, 1); // set the first day of the current year </p> <p> strFirstDay = sDateFormat. format (calendar. getTime (); <br/> return strFirstDay; <br/>}</p> <p> // obtain the last day of the next year <br/> p Ublic String getLastDayOfNextYear () {<br/> String strLastDay = ""; <br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd "); </p> <p> Calendar calendar = Calendar. getInstance (); <br/> calendar. add (Calendar. YEAR, 1); // Add a YEAR <br/> calendar. set (Calendar. DAY_OF_YEAR, 1); // set the first day of the current year <br/> calendar. roll (Calendar. DAY_OF_YEAR,-1); // roll back to the last day of the current year </p> <p> strLastDay = sDateFormat. format (calendar. getTime ()); <Br/> return strLastDay; <br/>}</p> <p> // obtain the number of days of the current year <br/> public String getDaysOfYear () {<br/> int year = 0; <br/> int days = 0; </p> <p> Calendar calendar = Calendar ar. getInstance (); <br/> calendar. set (Calendar. DAY_OF_YEAR, 1); // set the date to the first day of the current year <br/> calendar. roll (Calendar. DAY_OF_YEAR,-1); // roll back the date for one day </p> <p> year = calendar ar. get (Calendar. YEAR); <br/> days = calendar. get (Calendar. DAY_OF_YEAR); </p> <p> return (days +" In "+ year); <br/>}</p> <p> // obtain the quarter of the current month <br/> public String getSeasonOfMonth () {<br/> String strSeason = ""; <br/> int year, month, day; <br/> int season = 1; <br/> int array [] [] = new int [] [] {, 3}, {, 6}, {, 9, 12 }}; </p> <p> Calendar calendar = Calendar. getInstance (); <br/> year = calendar. get (Calendar. YEAR); <br/> month = calendar. get (Calendar. MONDAY) + 1; <br/> day = calendar. get (Calendar. DAY_OF_MONTH); <br/> switch (month) {<br/> case 1: <br/> case 2: <br/> case 3: <br/> season = 1; <br/> break; <br/> case 4: <br/> case 5: <br/> case 6: <br/> season = 2; <br/> break; <br/> case 7: <br/> case 8: <br/> case 9: <br/> season = 3; <br/> break; <br/> case 10: <br/> case 11: <br/> case 12: <br/> season = 4; <br/> break; <br/> default: <br/> season = 1; <br/> break; <br/>}</p> <p> int start_month = array [season- 1] [0]; <br/> int end_month = array [season-1] [2]; <br/> strSeason = year + "-" + month + "-" + day; <br/> strSeason + = "in [" + year + "-" + start_month + ":" + year + "-" + end_month + "]"; </p> <p> return strSeason; <br/>}</p> <p> // obtain the number of days in the month of the current day <br/> public String getLastDayOfNow () {<br/> String strLastDay = ""; <br/> int year, month, day; <br/> int days = 0; </p> <p> Calendar calendar = Calendar. getInsta Nce (); <br/> year = calendar. get (Calendar. YEAR); <br/> month = calendar. get (Calendar. MONTH) + 1; <br/> day = calendar. get (Calendar. DAY_OF_MONTH); </p> <p> switch (month) {<br/> case 1: <br/> case 3: <br/> case 5: <br/> case 7: <br/> case 8: <br/> case 10: <br/> case 12: <br/> days = 31; <br/> break; <br/> case 4: <br/> case 6: <br/> case 9: <br/> case 11: <br/> days = 30; <br/> break; <br/> case 2: <br/> if (isLeapYear (Year) {<br/> days = 29; <br/>}else {<br/> days = 28; <br/>}< br/> break; <br/> default: <br/> days = 0; <br/> break; <br/>}</p> <p> strLastDay = days + "in [" + year + "-" + month + "-" + day + "]"; <br/> return strLastDay; <br/>}</p> <p> // determines whether it is a runner-up. <br/> public boolean isLeapYear (int year) {<br/> return (year % 4 = 0 & year % 100! = 0) | (year % 400 = 0); <br/>}</p> <p> // convert the string time format yyyy-MM-dd, convert to Date type <br/> public Date getDateFromStr (String txtDate) {<br/> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd "); <br/> ParsePosition pos = new ParsePosition (0); <br/> Date date = sDateFormat. parse (txtDate, pos); </p> <p> return date; <br/>}</p> <p> // based on a date, returns the String of the day of the week <br/> public String getWeek (String txtDate) {<br/> Date date = getDateFromStr (txtDate); <br/> Calendar calendar = Calendar. getInstance (); </p> <p> calendar. setTime (date); <br/> String week = new SimpleDateFormat ("EEEE "). format (date); </p> <p> return week; <br/>}</p> <p> // calculate the number of days between two days. <br/> public long getDaysFromTwoDate (String txtDate1, String txtDate2) {<br/> if (txtDate1 = null | txtDate1.equals ("") {<br/> return 0; <br/>}< br/> if (txtDate2 = null | txtDate2.equals ("") {<br/> return 0; <br/>}</p> <p> SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd"); <br/> long days = 0; <br/> try {<br/> Date date1 = sDateFormat. parse (txtDate1); <br/> Date date2 = sDateFormat. parse (txtDate2); <br/> days = (date2.getTime ()-date1.getTime ()/(24*60*60*1000); // use the getTime () method, convert the time Date to the Long type in millisecond format for calculation <br/>} catch (Exception e) {<br/> System. out. println (e. getMessage (); <br/>}</p> <p> return days; <br/>}</p> <p >}< br/>

Running result:

Current date:
2012-03-07
12:30:11
2012-3-7 12:30:11. 101

Computing week:
-3
3/5/12 PM
3/10/12 PM
3/12/12 PM
3/3/12 PM

Computing month:
2012-03-01
2012-03-
2012-02-01
2012-02-29
2012-04-01
2012-04-30

Computing year:
2012-01-01
2012-12-31
2011-01-01
2011-12-31
2013-01-01
2013-12-31
366 in 2012.

Computing quarter:
2012-3-7 in [2012-1: 2012-3]
31 in [2012-3-7]
True

Date Format Conversion and calculation:
Wed Jun 20 00:00:00 CST 2012
Wednesday
2012-06-02-> 2012-06-12 interval days: 10

Source code download

Reference recommendations:

Java Date and Calendar

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.