Java Tool Class (1)--java date function Encyclopedia (date comparison size, date subtraction, date addition, acquisition year, etc.)

Source: Internet
Author: User
Tags add days date1 string format
Package Com.gcloud.common;

Import Org.apache.http.util.TextUtils;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;

Import Java.util.regex.Pattern;
 /** * Date Time Tool class * Created by Charlin on 2017/9/3.
    * * Public class Dateutil {public static final String China_date_format = "yyyy mm month dd Day";
    public static final String Datetime_format = "Yyyy-mm-dd HH:mm:ss";
    public static final String date_format = "YYYY-MM-DD";


    public static final String Time_format = "HH:mm:ss"; ----------------Judge-----------------------------------------------/** * is the run year * @param yearnum * @ret
        URN */public static Boolean isleapyear (int yearnum) {Boolean isleep = false;
        if ((yearnum% 4 = 0) && (yearnum%!= 0)) Isleep = true;
        else if (yearnum% = = 0) Isleep = true;
        else {isleep = false; ' Return ' isThe Leep; 
        /** * To determine whether it is a date * * @param date * @return/public static Boolean isDate (String date) { A regular expression that determines the date of the year, accepts the input format as 2010-12-24, and can accept excepting leap year dates String regex = "([0-9]{3}[1-9]|[ 0-9]{2}[1-9][0-9]{1}| [0-9] {1} [1-9] [0-9] {2}| [1-9] [0-9] {3}) -((0[13578]|1[02])-(0[1-9]|[ 12][0-9]|3[01]) | ((0[469]|11)-(0[1-9]|[ 12][0-9]|30)) | (02-(0[1-9]|[ 1][0-9]|2[0-8])) | (([0-9]{2}) (0[48]|[ 2468][048]| [13579] [26]) | ((0[48]| [2468] [048]| [3579] [26])
        00)-02-29) ";
        Pattern pattern = pattern.compile (regex);
    return Pattern.matcher (date). matches (); /** * Verify is not birthday * * @param birthday * @return/public static Boolean Verifybirthday (St
        Ring Birthday} {if (Textutils.isempty (birthday)) return false;
        if (!birthday.contains ("-")) return false;
        string[] arr = Birthday.split ("-");
        if (null = = Arr | | arr.length!= 3 | | arr[0].length ()!= 4 | | arr[1].length ()!= 2 | | arr[2].length ()!= 2)    return false;
        int year = getyear (New Date ());
        int birthyear = Integer.parseint (arr[0]);
        if (birthyear <= 1900 | | birthyear > year) return false;
        String curdate = formatdate (New Date (), date_format);
        if (Birthday.compareto (curdate) > 0) return false;
    return isDate (birthday); }//-------------------automatic conversion--------------------------------------------/** * Converts a string automatically into a time format * @p Aram Datestr * @return/public static Date Parsedatebyauto (String datestr) {if (stringutil.isempt
        Y (datestr)) {return null;
        } String format = Date_format;
        if (Datestr.indexof ("/") >-1) {format = Format.replace ("-", "/");
        } if (Datestr.indexof (":")!=-1) {format = = "hh:mm";
        }//Exists second if (Datestr.indexof (":")!= Datestr.lastindexof (":")) {format = = ": ss"; return parsedate (DATESTR, format);
        /** * Automatic recognition format * @param date * @return/public static String Formatdatebyauto (date date) {
        String format = Date_format; if (!) ( Date instanceof Java.sql.Date) && (date.getseconds () >0| | Date.getminutes () >0| |
        Date.gethours () >0)) {format = Datetime_format;
    return formatdate (date, format); //------------Current date and time--------------------------------/** the current date/public static date Getcurrdate () {return Parsedate (New Date ()) (formatdate);
    /** take the current time/public static Date Getcurrdatetime () {return parsedate (formatdate (new Date));}    /** take the current date/public static String Getcurrdatestr () {return formatdate (new date ());
    /** take the current time/public static String Getcurrdatetimestr () {return formatdate (new Date (), Datetime_format);
    public static String Formatcurrdate () {return formatdate (new Date (), date_format);} public static String Formatcurrdatetime (){return formatdate (new Date (), Datetime_format);}
    public static string Formatcurrdatetos (String strformat) {return formatdate (new Date (), Strformat);} -----------Time Calculation--------------------------------------------/** * Time subtraction * @param strdatebegin * @para M Strdateend * @param itype * @return/public static int getdiffdate (string strdatebegin, String strd
        ateend, int itype) {Calendar calbegin = calendar.getinstance ();
        Calbegin.settime (Parsedate (Strdatebegin, Datetime_format));
        Calendar calend = Calendar.getinstance ();
        Calbegin.settime (Parsedate (Strdateend, Datetime_format));
        Long Lbegin = Calbegin.gettimeinmillis ();
        Long lEnd = Calend.gettimeinmillis ();
        if (Itype = = Calendar.second) return (int) (lend-lbegin)/1000L);
        if (Itype = = Calendar.minute) return (int) (lend-lbegin)/60000L); if (Itype = = Calendar.hour) return (int) (Lend-lbegin)/3600000L);
        if (Itype = = Calendar.day_of_month) {return (int) (lend-lbegin)/86400000L);
    } return-1;
     /** * Add days or months or years to get new time * * @param strdate * @param count * @param daytype calendar.year  * @return */public static string Getadddatetime (string strdate, int count, int daytype) {Calendar cal
        = Calendar.getinstance ();
        Cal.settime (Parsedate (strdate));
        Cal.add (DayType, Count);
        SimpleDateFormat SDF = null; if ((DayType = = calendar.year) | | (DayType = = Calendar.month) | |
            (DayType = = Calendar.day_of_month))
        SDF = new SimpleDateFormat (date_format);
        else SDF = new SimpleDateFormat (Datetime_format);
    Return Sdf.format (Cal.gettime ()); /** * Date Increase days * @param date * @param icount * @return * * public static date Getadddate ( Date date, int icount) {Calendar cal = CalendaR.getinstance ();
        Cal.settime (date);
        Cal.add (Calendar.day_of_month, icount);
    return Cal.gettime (); /** * Comparison Date * * @param dateStr1 * @param dateStr2 * @return/public static int C
        Omparedate (String dateStr1, String dateStr2) {Date date1 = parsedate (DATESTR1);
        Date date2 = parsedate (DATESTR2);
        if (Date1.gettime () > Date2.gettime ()) return-1;
        else if (Date1.gettime () < Date2.gettime ()) return 1;
    else return 0;
            public static int Comparedate (date date1, date date2) {if (Date1.gettime () > Date2.gettime ())
        return-1;
        else if (Date1.gettime () < Date2.gettime ()) return 1;
    else return 0; /** * Time Difference * * @param startdate * @param enddate * @return/public static int GE Tdiffdays (date startdate, date enddate) {int days = 0;
            if (Startdate.after (EndDate)) {Date temp = StartDate;
            StartDate = EndDate;
        EndDate = temp;
        days = (int) (Enddate.gettime ()-startdate.gettime ())/1000 * 60 * 60 * 24;
    return days; /** * The following days of the current date * * @param date * @param n * @return/public static date Getafte
        Rday (date date, int n) {Calendar c = calendar.getinstance ();
        C.settime (date);
        C.add (calendar.date, N);
    return C.gettime (); ---------Get days----------------------------------------------/** * Get the last day of the current month * * @param dat ESTR * @return/public static string Getmonthend (String datestr) {//current first day date = par
        Sedate (Getmonthbegin (DATESTR));
        Calendar C = calendar.getinstance ();
        C.settime (date);
        C.add (Calendar.month, 1);
        C.add (Calendar.day_of_year,-1); Return FormatDate (C.gettimE ());
        public static String getmonthend (date date) {Calendar c = calendar.getinstance ();
        C.settime (date);
        C.set (Calendar.date, C.getactualmaximum (calendar.date));
    Return FormatDate (C.gettime ()); /** * Obtain the first day of the month of the current date * * @param datestr * @return/public static String Getmonthbegin (
        String datestr) {Date date = Parsedate (DATESTR);
    return FormatDate (date, "yyyy-mm") + 01;
    public static String Getmonthbegin (date date) {return formatdate (date, "yyyy-mm") + 01; }//--------------format Date-----------------------------------------/** * format date is a string * * @param dat E * @param format * @return/public static string FormatDate (date date, String format) {Retu
    RN new SimpleDateFormat (format). format (date);
    public static String formatdate (date date) {return formatdate (date, date_format); } public staticString formatechinadate (date date) {return formatdate (date, china_date_format);
    public static String Formatedatetime (date date) {return formatdate (date, datetime_format);
    public static String Formatetime (date date) {return formatdate (date, time_format); }//-----------------format string is date--------------------------------------/** * format string is date * * @param d  ATE * @param format * @return/public static date Parsedate (string Date, string format) {try
        {return new SimpleDateFormat (format). Parse (date);
        catch (ParseException e) {e.printstacktrace ();
    return null;
    public static date Parsedate (String date) {return parsedate (date, date_format);}
    public static date Parsechinadate (String date) {return parsedate (date, china_date_format); The public static date ParseDateTime (String date) {return parsedate (date, Datetime_format);
    public static date Parsetime (String date) {return parsedate (date, time_format); 
     ----------------------------------------------------/** * Acquisition Year * * @param date---
        * @return */public static int getyear (date date) {Calendar c = calendar.getinstance ();
        C.settime (date);
    Return C.get (calendar.year);
        /** * Get Month * * @param date * @return/public static int getmonth (date date) {
        Calendar C = calendar.getinstance ();
        C.settime (date);
    Return C.get (Calendar.month) + 1; /** * Get Day * * @param date * @return/public static int getday (date date) {C
        Alendar C = calendar.getinstance ();
        C.settime (date);
    Return C.get (Calendar.day_of_month); /** * Get Week * * @param date * @return/public static int Getweek (date date){Calendar c = calendar.getinstance ();
        C.settime (date);
    Return C.get (Calendar.day_of_week);
        /** * Acquisition Time * @param date * @return/public static int Gethour (date date) {
        Calendar C = calendar.getinstance ();
        C.settime (date);
    Return C.get (Calendar.hour_of_day);
        /** * Get the Split type * @param date * @return/public static int Getminute (date date) {
        Calendar C = calendar.getinstance ();
        C.settime (date);
    Return C.get (Calendar.minute);
        /** * Get seconds * * @param date * @return/public static int Getsecond (date date) {
        Calendar C = calendar.getinstance ();
        C.settime (date);
    Return C.get (Calendar.second); *//--------------Get the day of the week---------------------------------------------------/** * Get the day of the week * * @param Strdate * @return/public static String GetweekdayName (String strdate) {string[] Mname = {"Day", "one", "two", "three", "four", "five", "six"};
        Date date = Parsedate (strdate);
        Int week = Getweek (date);
    Return "Week" + Mname[week];
        The public static String getweekdayname (date date) {string[] Mname = {"Day", "one", "two", "three", "four", "five", "six"};
        Int week = Getweek (date);
    Return "Week" + Mname[week]; /** * The week of the year * @return * * public static int getweeknumofyear (date date) {Calendar cale
        Ndar = Calendar.getinstance ();
        Calendar.settime (date);
    Return Calendar.get (calendar.week_of_year);
        public static int getweeknumofyear (String date) {Calendar calendar = calendar.getinstance ();
        Calendar.settime (parsedate (date, date_format));
    Return Calendar.get (calendar.week_of_year);
     /** * Get the date of this week Monday * @param yearnum * @param weekNum * @return * @throws parseexception */public static String Getyearweekfirstday (int yearnum, int weekNum) {Calendar cal = Calendar.getinstance ();
        Cal.set (Calendar.year, yearnum);
        Cal.set (Calendar.week_of_year, WeekNum);
        Cal.set (Calendar.day_of_week, 2);
        String tempyear = integer.tostring (yearnum);
        String tempmonth = integer.tostring (Cal.get (calendar.month) + 1);
        String tempday = integer.tostring (Cal.get (Calendar.day_of_month)-1);
    return tempyear + "-" + Tempmonth + "-" + tempday;
     /** * Get the date of this week Sunday * @param yearnum * @param weekNum * @return * @throws parseexception */public static String getyearweekendday (int yearnum, int weekNum) {Calendar cal = Calendar.getinstance ()
        ;
        Cal.set (Calendar.year, yearnum);
        Cal.set (calendar.week_of_year, WeekNum + 1);

        Cal.set (Calendar.day_of_week, 1);
        String tempyear = integer.tostring (yearnum); String tempmonth = integer.tostring (Cal.get (Calendar.month)+ 1);
        String tempday = integer.tostring (Cal.get (Calendar.day_of_month)-1);
    return tempyear + "-" + Tempmonth + "-" + tempday; --------------Get days---------------------------------------------------/** * Get the first day of a certain month * @param yea Rnum * @param monthnum * @return/public static Date getyearmonthfirstday (int yearnum, int mon
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.