Java Common Date Processing class __java

Source: Internet
Author: User
Import Java.text.SimpleDateFormat;   
Import java.util.ArrayList;   
Import Java.util.Calendar;   
Import Java.util.Date;   
Import Java.util.GregorianCalendar;   
  
Import Java.util.LinkedHashMap; /** * Date Common processing class * * @author Songjun * @version 1.3/public class Dateutil {/** * Resolve a date All months * * @param begindatestr * @param enddatestr * @return * * public static ARR  Aylist getmonthlist (String begindatestr, String enddatestr) {//Specify the time format to resolve simpledateformat f = new   
        SimpleDateFormat ("yyyy-mm");   
  
        The returned month list String Sret = "";   
        Define some variables Date begindate = null;   
  
        Date enddate = null;   
        GregorianCalendar BEGINGC = null;   
        GregorianCalendar ENDGC = null;   
  
        ArrayList list = new ArrayList ();   
            try {//parse the string into a date Begindate = F.parse (BEGINDATESTR); EndDate = F.parse (ENDDATESTR);   
            Set Calendar BEGINGC = new GregorianCalendar ();   
  
            Begingc.settime (begindate);   
            ENDGC = new GregorianCalendar ();   
  
            Endgc.settime (EndDate); Until two times the same while (Begingc.gettime (). CompareTo (Endgc.gettime ()) <= 0) {Sret = beging   
                C.get (calendar.year) + "-" + (Begingc.get (calendar.month) + 1);   
                List.add (Sret);   
            In month, increase time Begingc.add (Calendar.month, 1);   
        } return list;   
            catch (Exception e) {e.printstacktrace ();   
        return null; }/** * Resolves all dates between one date period * * @param begindatestr * Start date * @param enddatestr * End Date * @return/public static ArrayList getdaylist (String begi NDATESTR, String enddATESTR) {//Specifies the time format to parse SimpleDateFormat f = new SimpleDateFormat ("Yyyy-mm-dd");   
        Define some variables Date begindate = null;   
  
        Date enddate = null;   
        Calendar BEGINGC = null;   
        Calendar ENDGC = null;   
  
        ArrayList list = new ArrayList ();   
            try {//parse the string into a date Begindate = F.parse (BEGINDATESTR);   
  
            EndDate = F.parse (ENDDATESTR);   
            Set Calendar BEGINGC = Calendar.getinstance ();   
  
            Begingc.settime (begindate);   
            ENDGC = Calendar.getinstance ();   
            Endgc.settime (EndDate);   
  
            SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd"); Until two times the same while (Begingc.gettime (). CompareTo (Endgc.gettime ()) <= 0) {List.add (s   
                Df.format (Begingc.gettime ())); In days, increase the time Begingc.add (Calendar.day_of_month, 1);   
            } return list;   
            catch (Exception e) {e.printstacktrace ();   
        return null;   
        The public static ArrayList Getyearlist () {ArrayList list = new ArrayList ();   
        Calendar c = null;   
        c = calendar.getinstance ();   
        C.settime (New Date ());   
  
        int curryear = Calendar.getinstance (). get (Calendar.year);   
        int startyear = currYear-5;   
        int endyear = curryear + 10;   
        for (int i = StartYear i < endyear; i++) {List.add (new Integer (i));   
    } return list;   
    public static int Getcurryear () {return calendar.getinstance (). get (Calendar.year); /** * Get the total number of years * * @param year * @return/public static Linkedhas   
        Hmap getweeklist (int year) {Linkedhashmap map = new Linkedhashmap (); Calendar C= new GregorianCalendar ();   
        C.set (year, Calendar.december, 31, 23, 59, 59);   
  
        int count = Getweekofyear (C.gettime ());   
        SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");   
        String Dayofweekstart = "";   
        String dayofweekend = "";   
            for (int i = 1; I <= count; i++) {Dayofweekstart = Sdf.format (Getfirstdayofweek (year, i));   
            Dayofweekend = Sdf.format (Getlastdayofweek (year, i));   
        Map.put (New Integer (i), "+i+" Week (from "+dayofweekstart +" to "+ dayofweekend+"));   
  
    } return map; /** * Get the total number of weeks in a year * @param years * @return/public static int Getweekcoun   
        Tinyear (int year) {Calendar c = new GregorianCalendar ();   
        C.set (year, Calendar.december, 31, 23, 59, 59);   
        int count = Getweekofyear (C.gettime ());   
    return count;   
     /** * How many weeks to get the current date ** @param date * @return/public static int getweekofyear (date date) {Calendar c = new Gr   
        Egoriancalendar ();   
        C.setfirstdayofweek (Calendar.monday);   
        C.setminimaldaysinfirstweek (7);   
  
        C.settime (date);   
    Return C.get (calendar.week_of_year); /** * Get the first day of the week * * @param year * @param week * @return * p   
        Ublic static Date Getfirstdayofweek (int year, int week) {Calendar c = new GregorianCalendar ();   
        C.set (Calendar.year, year);   
        C.set (Calendar.month, calendar.january);   
  
        C.set (calendar.date, 1);   
        Calendar cal = (GregorianCalendar) c.clone ();   
  
        Cal.add (calendar.date, week * 7);   
    Return Getfirstdayofweek (Cal.gettime ());  
    /** * Get the last day of a certain week * * @param year * @param week * @return * * public static Date Getlastdayofweek (int year, int week) {Calendar c = new GregorianCalendar ();   
        C.set (Calendar.year, year);   
        C.set (Calendar.month, calendar.january);   
  
        C.set (calendar.date, 1);   
        Calendar cal = (GregorianCalendar) c.clone ();   
  
        Cal.add (calendar.date, week * 7);   
    Return Getlastdayofweek (Cal.gettime ()); /** * Get the first day of a certain month * @param year * @param month * @return * * * publi   
        C Static Date getfirestdayofmonth (int year,int month) {month = month-1;      
        Calendar C = calendar.getinstance ();   
        C.set (Calendar.year, year);   
           
        C.set (Calendar.month, MONTH);   
  
        int day = C.getactualminimum (c.day_of_month);   
        C.set (Calendar.day_of_month, day);   
           
    return C.gettime (); /** * refers to the last day of a certain month * @param year * @param month * @return * * * Publ IC STatic Date getlastdayofmonth (int year,int month) {month = month-1;      
        Calendar C = calendar.getinstance ();   
        C.set (Calendar.year, year);   
        C.set (Calendar.month, MONTH);   
        int day = C.getactualmaximum (c.day_of_month);   
        C.set (Calendar.day_of_month, day);   
    return C.gettime (); /** * Obtain the first day of the week of the current date * * @param date * @return/public static date   
        Getfirstdayofweek (date date) {Calendar c = new GregorianCalendar ();   
        C.setfirstdayofweek (Calendar.monday);   
        C.settime (date); C.set (Calendar.day_of_week, C.getfirstdayofweek ());   
    Monday return C.gettime ();  /** * Get the last day of the week with the current date * * @param date * @return/public static date   
        Getlastdayofweek (date date) {Calendar c = new GregorianCalendar ();   
C.setfirstdayofweek (Calendar.monday);        C.settime (date); C.set (Calendar.day_of_week, C.getfirstdayofweek () + 6);   
    Sunday return C.gettime ();   }   
  
}


 

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.