Java gets a summary of the implementation methods for the specified date _java

Source: Internet
Author: User
Tags date now date1 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; The public class Timetest {//is used to globally control the week of the week, the weekly number of weeks of the week is changed private int weeks = 0; private int maxdate;//January maximum days private int maxyear;/

/year Maximum days/** * @param args */public static void main (string[] args) {timetest tt = new Timetest ();
System.out.println ("Get the date 6 months later:" +tt. Getaftermonth (6);
System.out.println ("Get Today's Date:" +tt.getnowtime ("Yyyy-mm-dd"));
System.out.println ("Get the 1st period of this week:" +tt.getmondayofweek ());
System.out.println ("Get this Sunday date ~:" +tt.getcurrentweekday ());
System.out.println ("Get Last week 1st:" +tt.getpreviousweekday ()); System.out. println ("Get Giges period last week:" +tt.getpreviousweeksunday ());
System.out.println ("Get 1st period next week:" +tt.getnextmonday ());
System.out.println ("Get next week Giges period:" +tt.getnextsunday ());
System.out.println ("Get the corresponding week of Saturday:" +tt.getnowtime ("Yyyy-mm-dd"));
System.out.println ("Get date of first day of this month:" +tt.getfirstdayofmonth ());
System.out.println ("Get date of last day of this month:" +tt.getdefaultday ());
System.out.println ("Get the first day of last month:" +tt.getpreviousmonthfirst ());
System.out.println ("Get the date of the last day of the Month:" +tt.getpreviousmonthend ());
System.out.println ("Get the first day of next month:" +tt.getnextmonthfirst ());
System.out.println ("Get the date of the last day of next month:" +tt.getnextmonthend ());
SYSTEM.OUT.PRINTLN ("Obtain the first day of the Year:" +tt.getcurrentyearfirst ());
System.out.println ("Get date of last day of this year:" +tt.getcurrentyearend ());
System.out.println ("Get the first day of last year:" +tt.getpreviousyearfirst ());
System.out.println ("Get the last date of last day:" +tt.getpreviousyearend ());
System.out.println ("Get the first day of next year:" +tt.getnextyearfirst ());
System.out.println ("Get the date of the last day of next year:" +tt.getnextyearend ());
System.out.println ("Get the first day of the quarter to the last day:" +tt.getthisseasontime (11)); System.out.println ("Get between two datesInterval days 2008-12-1~2008-: "+timetest.gettwoday (" 2008-12-1 "," 2008-9-29 ")); /** * Get the date parameter after the specified month (before) to send a negative number can be * * public static String getaftermonth (int month) {Calendar c = calendar.getinstance ()  
    ;//Get a calendar instance of 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 6 months later return s

 Trdate; /** * Gets the number of days between two/public static string Gettwoday (String sj1, String sj2) {SimpleDateFormat myformatter = new Simp
Ledateformat ("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 + "";} /** * According to a date, returns the string of days of the Week/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 analogies return to new SimpleDateFormat ("Eeee"). Format (C.gettime ()); /** * Converts a short time format string to a time Yyyy-mm-dd/public static Date strtodate (String strdate) {SimpleDateFormat formatter = new Simpl
Edateformat ("Yyyy-mm-dd");
Parseposition pos = new parseposition (0);
Date strtodate = Formatter.parse (strdate, POS);
return strtodate;  /** * Two days between * * @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.getti Me ()-Mydate.gettime ())/(24 * 60 * 60 * 1000);
Return day;
//To calculate the last day of the month, return 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 the current month, number 1th Lastdate.add (calendar.month,1),//plus one months, into the next month 1th Lastdate.add (
CALENDAR.DATE,-1);/Minus one day, into the last day of the month Str=sdf.format (Lastdate.gettime ());
return str;
///The 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 to the current month of the number 1th Lastdate.add (calendar.month,-1),//minus one months, into the next month 1th number//lastdate.add (
CALENDAR.DATE,-1);/Minus one day, into 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 to the current month, number 1th Str=sdf.format (lastdate.getTime ());
return str;
//Get this week Sunday date 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 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 between the current date and this Sunday private int getmondayplus () {Calendar cd = Calendar.getinstance ();//Get Today is the day of the week, Sunday is the first, and Tuesday is the next day. .... int dayofweek = Cd.get (Calendar.day_of_week)-1;
Because press China Monday as the first day so here minus 1 if (DayOfWeek = = 1) {return 0;} else {return 1-dayofweek;}}
Get this Monday date 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 corresponding week's Saturday date 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 last Sunday's date 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 last Monday date 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 Next Monday's date 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 Next Sunday's date 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, the last day Maxdate=cd.get (calendar.date); if (Monthofnumber = = 1) {return-maxdate}
else{return 1-monthofnumber}}
Get date 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 rolls back one day, 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, 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 month lastdate.set (calendar.date, 1);//Set the date to the first day of the month Lastdate.roll (Calendar.date,-1
)//Date rolls 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 of the year private int getmaxyear () {Calendar cd = Calendar.getinstance (); Cd.set (Calendar.day_of_year,1); Set the date as 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);
Is the day of the Year Cd.set (calendar.day_of_year,1); Set the date to the first day of Cd.roll (calendar.day_of_year,-1);/to roll the date back.
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 this 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;
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 ";
The date of the first day of last 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 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& amp;&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");//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 certain 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 = n | | month = =) {return;} if (month = 4 | | month = 6 | | month = 9 | | month = = 11)
{return 30;}
if (month = 2) {if (Isleapyear (year)) {return;} "else {return;}}" return 0; /** * Whether leap years * @param year *return */public boolean isleapyear (int year) {return (year% 4 = 0 && Year% 100!= 0) | |
(Year% 400 = 0); }

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.