Java method Summary for getting a specified date

Source: Internet
Author: User
Tags date now dateformat

Format 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 week change of the week, week, week
private int weeks = 0;
private int maxdate;//January max days
private int maxyear;//Maximum number of days in a year
/**
* @param args
*/
public static void Main (string[] args) {
Timetest tt = new Timetest ();

System.out.println ("Date 6 months later:" +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 day of last date:" +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-
9.29: "+timetest.gettwoday (" 2008-12-1 "," 2008-9-29 "));
}

/**
* Get a negative number for the date parameter after the specified month (before)

*/

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 ());//The date you want to be 6 months later
return strdate;

}


/**
* Get the number of days between two dates
*/
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 + "";
}
/**
* 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);
The hour is the day of the week, its range 1~7
1= Sunday 7= Saturday, other analogies
return new SimpleDateFormat ("Eeee"). Format (C.gettime ());
}
/**
* Convert short time Format strings to time 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 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;
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;
}
Calculates the last day of the month, returns a string
Public String Getdefaultday () {
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
Calendar lastdate = Calendar.getinstance ();
Lastdate.set (calendar.date,1);//Set as number 1th of the current month
Lastdate.add (calendar.month,1);//Add one months to the next month's number 1th
Lastdate.add (calendar.date,-1);//Minus one day, becoming the last day of the month
Str=sdf.format (Lastdate.gettime ());
return str;
}
First day of last month
Public String Getpreviousmonthfirst () {
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
Calendar lastdate = Calendar.getinstance ();
Lastdate.set (calendar.date,1);//Set as number 1th of the current month
Lastdate.add (calendar.month,-1);//Minus one months, change to 1th in next month
Lastdate.add (calendar.date,-1);//Minus one day, becoming 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 number 1th of the current month
Str=sdf.format (Lastdate.gettime ());
return str;
}
Get the date of the week Sunday
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;
}
Get the time of day
public string Getnowtime (string dateformat) {
Date now = new Date ();
SimpleDateFormat DateFormat = new SimpleDateFormat (DateFormat);//Can
To facilitate
To 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 the day of the week, Sunday is the first, Tuesday is the second day ...
int dayOfWeek = Cd.get (Calendar.day_of_week)-1; Because according to China Monday
As the first
Day, so minus 1 here.
if (DayOfWeek = = 1) {
return 0;
} else {
return 1-dayofweek;
}
}
Get 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;
}
Get 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;
}
Get the date of last Sunday
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;
}
Get the date of last Monday
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;
}
Get the date of 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;
}
Get the date of next Sunday
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);//Date Roll 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, 1);//Set the date to the first day of the month
Lastdate.roll (Calendar.date,-1);//date rolled back one day, the last day of the 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, 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 one months
Lastdate.set (calendar.date, 1);//Set the date to the first day of the month
Lastdate.roll (Calendar.date,-1);//date rolled back one day, the last day of the 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 int 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);//Get the day of the year
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);
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);
Date yearday = Currentdate.gettime ();
DateFormat df = dateformat.getdateinstance ();
String preyearday = 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");
To repair
Change date format
String years = Dateformat.format (date);
Return years+ "-12-31";
}

Get the date of the first day of the year *
Public String Getpreviousyearfirst () {
Date date = new Date ();
SimpleDateFormat DateFormat = new SimpleDateFormat ("yyyy");
To repair
Change date format
String years = Dateformat.format (date); int years_value = Integer.parseint
(years);
years_value--;
Return years_value+ "-1-1";
}
Get the date of the last day of the year
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;
}
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 start_month = array[season-1][0];
int end_month = array[season-1][2];
Date date = new Date ();
SimpleDateFormat DateFormat = new SimpleDateFormat ("yyyy");
To repair
Change date format
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;
}
/**
* Get the last day of a month in a year
* @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;
}


/**
* Whether leap year
* @param year
* @return
*/
public boolean isleapyear (int year) {
Return (year% 4 = = 0 && Year% 100! = 0) | | (Year% 400 = = 0);
}

Java method Summary for getting 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.