Java get a summary of implementation methods for a specified date

Source: Internet
Author: User
Tags date1 dateformat

Format Date  String-->Date   or  Data-->StringSimpleDateFormat sdf = new  SimpleDateFormat ("Yyyy-mm-dd");    date  date = sdf.parse ("2009-11-04");// String-->date  string  sdate  = sdf.format (Date );// Data-- >String ===============================================================package com.hefeng.test; import java.text.dateformat;import java.text.parseposition;import java.text.simpledateformat; import java.util.calendar;import java.util.date;import java.util.gregoriancalendar;public  class timetest {//used for global control   last week, week, week change private int weeks = 0;private  int maxdate;//January Maximum number of days private int maxyear;//one year maximum number of days/***  @param  args*/public static  void main (String[] args)  {timetest tt = new timetest (); System.out.println ("Get 6 months after the date:" +tt .getaftermonth (6); System.out.println ("Get Day Date:" +tt.getnowtime ("Yyyy-mm-dd")); System.out.println ("Get this week 1st:" +tt.getmondayofweek ()); System.out.println ("Get this Sunday date ~:" +tt.getcurrentweekday ()); System.out.println ("Get last 1st period:" +tt.getpreviousweekday ()); System.out.println ("Get last week Giges period:" +tt.getpreviousweeksunday ()); System.out.println ("Get Next week 1st:" +tt.getnextmonday ()); System.out.println ("Get next week Giges period:" +tt.getnextsunday ()); System.out.println ("To obtain the corresponding week of Saturday:" +tt.getnowtime ("Yyyy-mm-dd")); System.out.println ("Get the first day of the month Date:" +tt.getfirstdayofmonth ()); System.out.println ("Get the last day of the month Date:" +tt.getdefaultday ()); System.out.println ("Get the first day of the month Date:" +tt.getpreviousmonthfirst ()); System.out.println ("Get date of last day of last month:" +tt.getpreviousmonthend ()); System.out.println ("Get first day of next month Date:" +tt.getnextmonthfirst ()); System.out.println ("Get the last day of next month Date:" +tt.getnextmonthend ()); System.out.println ("Get the first day of the year Date:" +tt.getcurrentyearfirst ()); System.out.println ("Get the last day of the year Date:" +tt.getcurrentyearend ()); System.out.println ("Get the first day of last year date:" +tt.getpreviousyearfirst ()); System.out.println ("Get last date of last day: "+tt.getpreviousyearend ()); System.out.println ("Get the first day of next year's date:" +tt.getnextyearfirst ()); System.out.println ("Get the last day of next year date:" +tt.getnextyearend ()); System.out.println ("Get the first day of the quarter to the last day:" +tt.getthisseasontime (11)); System.out.println ("Gets the number of days between two dates 2008-12-1~2008-.29:" +timetest.gettwoday ("2008-12-1", "2008-9-29"));} /***  the date   parameter of the specified month (before) can be */public static string  getaftermonth (int month)  {        calendar c = calendar.getinstance ();// Get an instance of a calendar            simpledateformat sdf =  new simpledateformat ("Yyyy-mm-dd");            date date = null;           try{                date =  Sdf.parse ("2009-11-04");//Initial date             }catch (exception e) {          }           c.settime (date);//Set Calendar time            c.add (calendar.month,month);//Add 6 months to the calendar month            string strdate = sdf.format ( C.gettime ());//To the date you want to be 6 months            return  strdate; }/***  gets the number of days between two dates */public static string gettwoday (string sj1,  STRING&NBSP;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);d ay =  (Date.gettime ()  -  Mydate.gettime ())  /  (24 * 60 * 60 * 1000);}  catch  (exception e)  {return  "";} return day +  "";} /***  A string that returns the day of the week based on a date */public static string getweek (string sdate)  {//  Convert to Time date date = timetest.strtodate (sdate); Calendar c = calendar.getinstance (); C.settime (date);// int hour=c.get (Calendar.DAY_OF _week);// hour  is the day of the week, its range  1~7// 1= Sunday  7= Saturday, other analogy return new  SimpleDateFormat ("Eeee"). Format (C.gettime ());} /***  converting a short time format string to a time  yyyy-mm-dd*/public static date strtodate (string strdate)  {simpledateformat formatter = new simpledateformat ("Yyyy-MM-dd"); Parseposition pos = new parseposition (0);D Ate strtodate = formatter.parse ( Strdate, pos); return strtodate;} /***  days between two hours **  @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;//  converted to standard Time 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;}   Calculates the last day of the month, returns the string Public string getdefaultday () {string str =  ""; Simpledateformat sdf=new simpledateformat ("Yyyy-mm-dd"); Calendar lastdate = calendar.getinstance (); Lastdate.set (calendar.date,1);//set to 1  of the current month Number Lastdate.add (Calendar.month,1);//Plus one months, change to next month's 1  Lastdate.add (calendar.date,-1);//Minus one day becomes the last day of the month Str=sdf.format (Lastdate.gettime ()); Return str;}   Public string getpreviousmonthfirst () {string str =  "" On the first day of last month; Simpledateformat sdf=new simpledateformat ("Yyyy-mm-dd"); Calendar lastdate = calendar.getinstance (); Lastdate.set (calendar.date,1);//set to 1  of the current month Lastdate.add (calendar.month,-1);//Minus one months, which becomes the 1 //lastdate.add (calendar.date,-1) of next month;/Minus one day becomes the last day of the month str= Sdf.format (Lastdate.gettime ()); return str;} Get the first day of the Month Public string getfirstdayofmonth () {string str =  ""; Simpledateformat sdf=new simpledateformat ("Yyyy-mm-dd"); Calendar lastdate = calendar.getinstance (); Lastdate.set (calendar.date,1);//Set as 1  of the current month str= Sdf.format (Lastdate.gettime ()); return str;}   Get the date of Sunday this week public string getcurrentweekday ()  {weeks = 0;int mondayplus  = this.getmondayplus (); GregoriancalendAr currentdate = new gregoriancalendar (); Currentdate.add (gregoriancalendar.date,  mondayplus+6);D ate monday = currentdate.gettime ();D ateformat df =  Dateformat.getdateinstance (); String premonday = df.format (Monday); return premonday;} Get the day Time Public string getnowtime (String dateformat) {date now = new date (); Simpledateformat dateformat = new simpledateformat (DateFormat);//You can easily modify the date format string  Hehe = dateformat.format (now); return hehe;}   Get the number of days that the current date differs from this Sunday private int getmondayplus ()  {Calendar cd =  Calendar.getinstance ();//  get today is day of the week, Sunday is the first day, Tuesday is the second day ... int dayofweek = cd.get ( Calendar.day_of_week) -1; //because of the first day of worship in China, so here 1if  (dayofweek == 1)  {return 0;}  else {return 1 - dayofweek;}} Get this Monday date Public string getmondayofweek () {WEEKS&NBsp;= 0;int mondayplus = this.getmondayplus (); Gregoriancalendar currentdate = new gregoriancalendar (); Currentdate.add ( Gregoriancalendar.date, mondayplus);D ate monday = currentdate.gettime ();D ATEFORMAT&NBSP;DF  = dateformat.getdateinstance (); String premonday = df.format (Monday); return premonday;} Date of Saturday of the corresponding week public string getsaturday ()  {int mondayplus = this.getmondayplus (); Gregoriancalendar currentdate = new gregoriancalendar (); Currentdate.add ( GREGORIANCALENDAR.DATE,&NBSP;MONDAYPLUS&NBSP;+&NBSP;7&NBSP;*&NBSP;WEEKS&NBSP;+&NBSP;6);D ate monday =  currentdate.gettime ();D ateformat df = dateformat.getdateinstance (); String premonday = df.format (Monday); return premonday;}   received last Sunday's date public string getpreviousweeksunday ()  {weeks=0;weeks--;int mondayplus  = this.getmondayplus (); Gregoriancalendar currentdate = new gregoriancalendar (); Currentdate.add ( Gregoriancalendar.date, mondayplus+weeks);D ate monday = currentdate.gettime ();D Ateformat  df = dateformat.getdateinstance (); String premonday = df.format (Monday); return premonday;}   received last Monday's date public string getpreviousweekday ()  {weeks--;int mondayPlus =  This.getmondayplus (); Gregoriancalendar currentdate = new gregoriancalendar (); Currentdate.add ( Gregoriancalendar.date, mondayplus + 7 * weeks);D ate monday =  Currentdate.gettime ();D ateformat df = dateformat.getdateinstance (); String premonday = df.format (Monday); return premonday;}   Get the date of next Monday public string getnextmonday ()  {weeks++;int mondayPlus =  This.getmondayplus (); Gregoriancalendar currentdate = new gregoriancalEndar (); Currentdate.add (gregoriancalendar.date, mondayplus + 7);D ate monday =  Currentdate.gettime ();D ateformat df = dateformat.getdateinstance (); String premonday = df.format (Monday); return premonday;}   Get the date of next Sunday public string getnextsunday ()  {int mondayPlus =  This.getmondayplus (); Gregoriancalendar currentdate = new gregoriancalendar (); Currentdate.add ( gregoriancalendar.date, mondayplus + 7+6);D ate monday = currentdate.gettime ();D Ateformat 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);//Set the date to the first day of the month Cd.roll ( CALENDAR.DATE,&NBSP;-1);//Date rolls back one day, that is, the last day Maxdate=cd.get (calendar.date); if (monthofnumber == 1) {return -maxdate;} Else{return 1-monthofnumber;}} Get the date of the last day of last month public string getpreviousmonthend () {string str =  ""; Simpledateformat sdf=new simpledateformat ("Yyyy-mm-dd"); Calendar lastdate = calendar.getinstance (); Lastdate.add (calendar.month,-1);//Minus One months Lastdate.set ( CALENDAR.DATE,&NBSP;1);//Set the date to the first day of the month Lastdate.roll (calendar.date, -1);//date rolled back one day, that is, the last day of this month Str=sdf.format ( Lastdate.gettime ()); return str;} Get the date of the first day of next month Public string getnextmonthfirst () {string str =  ""; Simpledateformat sdf=new simpledateformat ("Yyyy-mm-dd"); Calendar lastdate = calendar.getinstance (); Lastdate.add (calendar.month,1);//Minus One months Lastdate.set ( CALENDAR.DATE,&NBSP;1);//Set the date to the first day of the Month Str=sdf.format (Lastdate.gettime ()); return str;} Get the date of the last day of next month public string getnextmonthend () {string str =  ""; Simpledateformat sdf=new simpledateformat ("Yyyy-mm-dd"); CalendAr lastdate = calendar.getinstance (); Lastdate.add (calendar.month,1);//plus a month lastdate.set ( CALENDAR.DATE,&NBSP;1);//Set the date to the first day of the month Lastdate.roll (calendar.date, -1);//date rolled back one day, that is, the last day of this month Str=sdf.format ( Lastdate.gettime ()); return str;} Get the date of the last day of next year public string getnextyearend () {string str =  ""; Simpledateformat sdf=new simpledateformat ("Yyyy-mm-dd"); Calendar lastdate = calendar.getinstance (); Lastdate.add (calendar.year,1);//Plus One year lastdate.set ( calendar.day_of_year, 1); Lastdate.roll (calendar.day_of_year, -1); Str=sdf.format (LastDate.getTime ()); Return str;} Get the date of the first day of next year Public string getnextyearfirst () {string str =  ""; Simpledateformat sdf=new simpledateformat ("Yyyy-mm-dd"); Calendar lastdate = calendar.getinstance (); Lastdate.add (calendar.year,1);//Plus One year lastdate.set ( calendar.day_of_year, 1); Str=sdf.format (Lastdate.gettime ()); return str;} Get how many days this year PRIVATE&NBSP;INT&NBsp;getmaxyear () {calendar cd = calendar.getinstance (); Cd.set (calendar.day_of_year,1);// Set the date to the first day of the Year Cd.roll (calendar.day_of_year,-1);//Roll the date back one day. Int maxyear = cd.get (calendar.day_of_year); return maxyear;} Private int getyearplus () {calendar cd = calendar.getinstance (); int yearOfNumber  = cd.get (calendar.day_of_year);//To obtain the day is the first day of the Year Cd.set (calendar.day_of_year,1);//Set the date to Cd.roll ( CALENDAR.DAY_OF_YEAR,-1);//Roll the date back one day. Int maxyear = cd.get (calendar.day_of_year); if (yearofnumber == 1) {return -MaxYear ;} Else{return 1-yearofnumber;}} Get the date of the first day of the year Public string getcurrentyearfirst () {int yearplus = this.getyearplus (); Gregoriancalendar currentdate = new gregoriancalendar (); Currentdate.add ( Gregoriancalendar.date,yearplus);D ate yearday = currentdate.gettime ();D ateformat df =  dateformat.getdateinstance (); String preyearday&nbSp;= df.format (yearday); return preyearday;} Get the date of the last day of the year  *public string getcurrentyearend () {date date = new date (); Simpledateformat dateformat = new simpledateformat ("yyyy");//You can easily modify the date format String years  = dateformat.format (date); return years+ "-12-31";} Get the date of the first day of the previous year  *public string getpreviousyearfirst () {date date = new date (); Simpledateformat dateformat = new simpledateformat ("yyyy");//You can easily modify the date format String years  = dateformat.format (date);  int years_value = integer.parseint (years); Years_ Value--;return years_value+ "-1-1";} Get the last day of the year date public string getpreviousyearend () {weeks--;int yearplus =  This.getyearplus (); Gregoriancalendar currentdate = new gregoriancalendar (); Currentdate.add ( gregoriancalendar.date,yearplus+maxyear*weeks+ (MaxYear-1));D Ate yearday = currenTdate.gettime ();D ateformat df = dateformat.getdateinstance (); String preyearday = df.format (Yearday); Getthisseasontime (one); return preyearday;} Get this quarter 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&NBSP;START_MONTH&NBSP;=&NBSP;ARRAY[SEASON-1][0];INT&NBSP;END_MONTH&NBSP;=&NBSP;ARRAY[SEASON-1][2];D ate  date = new date (); Simpledateformat dateformat = new simpledateformat ("yyyy");//You can easily modify the date format String years  = dateformat.format (date); Int years_value = integer.parseint (years); Int start_ days =1;//years+ "-" +string.valueof (start_month) + "-";//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;} /***  get the last day of a month *  @param  year  *  @param  month  month *  @return   last day */ 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;} /***  whether a leap year*  @param  year  Year *  @return */public boolean isleapyear (int year)  {return   (year % 4 == 0 && year % 100 != 0)  | |   (year % 400 == 0);}

Java get a summary of implementation methods for a specified date

Related Article

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.