Java get the Summary of implementation methods for a specified date

Source: Internet
Author: User
Tags date now date1

The following summarizes the implementation methods for obtaining a specified date in Java. For more information, seeCopy codeThe Code is as follows:
Format a Date String --> Date or Data --> String

SimpleDateFormat 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 to globally control the number of weeks in the previous week, this week, and next week
Private int weeks = 0;
Private int MaxDate; // The maximum number of days in January
Private int MaxYear; // The maximum number of days in a year
/**
* @ Param args
*/
Public static void main (String [] args ){
TimeTest tt = new TimeTest ();

System. out. println ("date after 6 months:" + tt. getAfterMonth (6 );
System. out. println ("Get date:" + tt. getNowTime ("yyyy-MM-dd "));
System. out. println ("Get Monday Date:" + tt. getMondayOFWeek ());
System. out. println ("Get the date of Sunday ~ : "+ Tt. getCurrentWeekday ());
System. out. println ("Get last Monday Date:" + tt. getpreviusweekday ());
System. out. println ("Get Sunday Date:" + tt. getpreviusweeksunday ());
System. out. println ("Get next Monday:" + tt. getNextMonday ());
System. out. println ("Get Sunday Date:" + tt. getNextSunday ());
System. out. println ("Get Saturday of the week:" + tt. getNowTime ("yyyy-MM-dd "));
System. out. println ("Get the first day of this month Date:" + tt. getFirstDayOfMonth ());
System. out. println ("Get the last day of the month:" + tt. getdefaday Day ());
System. out. println ("Get last month first day date:" + tt. getpreviusmonthfirst ());
System. out. println ("Get date of last day of last month:" + tt. getpreviusmonthend ());
System. out. println ("Get the first day of next month Date:" + tt. getNextMonthFirst ());
System. out. println ("Get the last day of next month:" + tt. getNextMonthEnd ());
System. out. println ("Get the first day of the year:" + tt. getCurrentYearFirst ());
System. out. println ("Get date of the last day of the current year:" + tt. getCurrentYearEnd ());
System. out. println ("Get the first day of last year:" + tt. getpreviusyearfirst ());
System. out. println ("Get last day date:" + tt. getpreviusyearend ());
System. out. println ("Get the first day of next year:" + tt. getNextYearFirst ());
System. out. println ("Get the last day of next year:" + tt. getNextYearEnd ());
System. out. println ("Get the first day to the last day of the quarter:" + tt. getThisSeasonTime (11 ));
System. out. println ("Get the number of days between two dates ~ 2008-
. 29: "+ TimeTest. getTwoDay "));
}

/**
* Get the date parameter after the specified month (Before) and pass it as a negative number.

*/

Public static String getAfterMonth (int month ){
Calendar c = Calendar. getInstance (); // gets an instance of a Calendar.
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");
Date date = null;
Try {
Date = sdf. parse ("2009-11-04"); // the initial date.
} Catch (Exception e ){

}
C. setTime (date); // set the calendar time
C. add (Calendar. MONTH, month); // add 6 months to the Calendar MONTH.
String strDate = sdf. format (c. getTime (); // the date you want to get 6 months later
Return strDate;

}


/**
* Get the number of days between two days.
*/
Public static String 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 "";
}
Return day + "";
}
/**
* Returns the day of the week based on a date.
*/
Public static String getWeek (String sdate ){
// Re-convert to time
Date date = TimeTest. strToDate (sdate );
Calendar c = Calendar. getInstance ();
C. setTime (date );
// Int hour = c. get (Calendar. DAY_OF_WEEK );
// The day of the week is stored in hour. The value range is 1 ~ 7
// 1 = Sunday 7 = Saturday, and so on
Return new SimpleDateFormat ("EEEE"). format (c. getTime ());
}
/**
* Convert a short-time string to a time string in yyyy-MM-dd
*/
Public static Date strToDate (String strDate ){
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd ");
ParsePosition pos = new ParsePosition (0 );
Date strtodate = formatter. parse (strDate, pos );
Return strtodate;
}
/**
* Number of days between two times
*
* @ 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;
// Convert 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;
}
// Calculate the last day of the current month and return a string
Public String getDefaultDay (){
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");
Calendar lastDate = Calendar. getInstance ();
LastDate. set (Calendar. DATE, 1); // set it to the number 1 of the current month
LastDate. add (Calendar. MONTH, 1); // add a MONTH to the first day of the next MONTH.
LastDate. add (Calendar. DATE,-1); // subtract one day to the last day of the month.
Str = sdf. format (lastDate. getTime ());
Return str;
}
// The first day of last month
Public String getpreviusmonthfirst (){
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");
Calendar lastDate = Calendar. getInstance ();
LastDate. set (Calendar. DATE, 1); // set it to the number 1 of the current month
LastDate. add (Calendar. MONTH,-1); // subtract a MONTH and change it to the first day of the next MONTH.
// LastDate. add (Calendar. DATE,-1); // subtract one day to the last day of the month.
Str = sdf. format (lastDate. getTime ());
Return str;
}
// Obtain 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 it to the number 1 of the current month
Str = sdf. format (lastDate. getTime ());
Return str;
}
// Obtain the date of Sunday of the week
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;
}
// Obtain the current day's time
Public String getNowTime (String dateformat ){
Date now = new Date ();
SimpleDateFormat dateFormat = new SimpleDateFormat (dateformat); // optional
For convenience
Modify Date Format
String hehe = dateFormat. format (now );
Return hehe;
}
// Obtain the number of days for which the current date differs from the current Sunday
Private int getMondayPlus (){
Calendar cd = Calendar. getInstance ();
// Obtain the day of the week, Sunday, and Tuesday ......
Int dayOfWeek = cd. get (Calendar. DAY_OF_WEEK)-1; // as per China Monday
As the first
Day, so here we subtract 1
If (dayOfWeek = 1 ){
Return 0;
} Else {
Return 1-dayOfWeek;
}
}
// Obtain the date of this Monday
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;
}
// Obtain the Saturday date of the corresponding week
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;
}
// Obtain the date of last week's Sunday
Public String getpreviusweeksunday (){
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;
}
// Obtain the date of last week Monday
Public String getpreviusweekday (){
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;
}
// Obtain the date of the next Monday
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;
}
// Obtain the date of Sunday next week
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); // set the DATE to the first day of the month.
Cd. roll (Calendar. DATE,-1); // roll back one day, that is, the last day
MaxDate = cd. get (Calendar. DATE );
If (monthOfNumber = 1 ){
Return-MaxDate;
} Else {
Return 1-monthOfNumber;
}
}
// Obtain the date of the last day of the previous month
Public String getpreviusmonthend (){
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");
Calendar lastDate = Calendar. getInstance ();
LastDate. add (Calendar. MONTH,-1); // minus one MONTH
LastDate. set (Calendar. DATE, 1); // set the DATE to the first day of the current month.
LastDate. roll (Calendar. DATE,-1); // roll back one day, that is, the last day of the month.
Str = sdf. format (lastDate. getTime ());
Return str;
}
// Obtain the date of the first day of the 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 MONTH
LastDate. set (Calendar. DATE, 1); // set the DATE to the first day of the current month.
Str = sdf. format (lastDate. getTime ());
Return str;
}
// Obtain the date of the last day of the next month
Public String getNextMonthEnd (){
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");
Calendar lastDate = Calendar. getInstance ();
LastDate. add (Calendar. MONTH, 1); // add a MONTH
LastDate. set (Calendar. DATE, 1); // set the DATE to the first day of the current month.
LastDate. roll (Calendar. DATE,-1); // roll back one day, that is, the last day of the month.
Str = sdf. format (lastDate. getTime ());
Return str;
}
// Obtain 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); // add a YEAR
LastDate. set (Calendar. DAY_OF_YEAR, 1 );
LastDate. roll (Calendar. DAY_OF_YEAR,-1 );
Str = sdf. format (lastDate. getTime ());
Return str;
}
// Obtain 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); // add a YEAR
LastDate. set (Calendar. DAY_OF_YEAR, 1 );
Str = sdf. format (lastDate. getTime ());
Return str;
}
// Obtain the number of days of the current year
Private int getMaxYear (){
Calendar cd = Calendar. getInstance ();
Cd. set (Calendar. DAY_OF_YEAR, 1); // set the date to the first day of the current year.
Cd. roll (Calendar. DAY_OF_YEAR,-1); // roll back the date for 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); // obtain the day of the year
Cd. set (Calendar. DAY_OF_YEAR, 1); // set the date to the first day of the current year.
Cd. roll (Calendar. DAY_OF_YEAR,-1); // roll back the date for one day.
Int MaxYear = cd. get (Calendar. DAY_OF_YEAR );
If (yearOfNumber = 1 ){
Return-MaxYear;
} Else {
Return 1-yearOfNumber;
}
}
// Obtain the date of the first day of the current year
Public String getCurrentYearFirst (){
Int yearPlus = this. getYearPlus ();
GregorianCalendar currentDate = new GregorianCalendar ();
CurrentDate. add (GregorianCalendar. DATE, yearPlus );
Date yearDay = currentDate. getTime ();
DateFormat df = DateFormat. getDateInstance ();
String preYearDay = df. format (yearDay );
Return preYearDay;
}
// Obtain the date of the last day of the current year *
Public String getCurrentYearEnd (){
Date date = new Date ();
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy"); // optional
Easy repair
Change Date Format
String years = dateFormat. format (date );
Return years + "-12-31 ";
}

// Obtain the date of the first day of the previous year *
Public String getpreviusyearfirst (){
Date date = new Date ();
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy"); // optional
Easy repair
Change Date Format
String years = dateFormat. format (date); int years_value = Integer. parseInt
(Years );
Years_value --;
Return years_value + "-1-1 ";
}
// Obtain the date of the last day of the previous year
Public String getpreviusyearend (){
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;
}
// Obtain 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 start_month = array [season-1] [0];
Int end_month = array [season-1] [2];
Date date = new Date ();
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy"); // optional
Easy repair
Change 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;
}
/**
* Obtain the last day of a month.
* @ Param year
* @ Param 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;
}
/**
* Leap year
* @ Param year
* @ Return
*/
Public boolean isLeapYear (int year ){
Return (year % 4 = 0 & amp; year % 100! = 0) | (year % 400 = 0 );
}

 

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.