Java Time Tool Class (Project utility)

Source: Internet
Author: User
Tags add time

Package Com.eabax.plugin.yundada.utils;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.arraylist;import Java.util.calendar;import Java.util.date;import Java.util.gregoriancalendar;import Java.util.linkedhashmap;public class Timeutil {/** * resolves all months between a date * * @param begindatestr * @param enddatestr * @return */public static arraylist<string> getmonthlist (String begindatestr,string enddatestr) {// Specifies the time format to parse SimpleDateFormat f = new SimpleDateFormat ("yyyy-mm");//The month list returned by string sret = "";//define some variables date begindate = Null;d ate endDate = null; GregorianCalendar BEGINGC = null; GregorianCalendar ENDGC = null; arraylist<string> list = new arraylist<string> (); try {//String parse to 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 time same while (Begingc.gettime (). CompareTo (Endgc.gettime ()) <= 0) {Sret = BEGINGC. Get (Calendar.year) + "-" + (Begingc.get (calendar.month) + 1); List.add (Sret);//month, increase time Begingc.add (Calendar.month, 1);} return list;} catch (Exception e) {e.printstacktrace (); return null;}} /** * Resolves all dates between a date period * * @param begindatestr * Start date * @param enddatestr * End Date * @return */public s Tatic arraylist<string> getdaylist (String begindatestr,string enddatestr) {//Specify the time format to resolve simpledateformat f = new SimpleDateFormat ("Yyyy-mm-dd");//define some variables Date begindate = null;date endDate = null; Calendar BEGINGC = null; Calendar ENDGC = null; arraylist<string> list = new arraylist<string> (); try {//String parse to 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 (Sdf.format (Begingc.gettime ()));//Day, add time Begingc.add (Calendar.day_of_month, 1);} return list;} catch (Exception e) {e.printstacktrace (); return null;}} public static arraylist<integer> getyearlist () {arraylist<integer> list = new arraylist<integer> (); 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 weeks in a year * * @param years * @return */public static Linkedhashmap<integer, string> getweeklist (int) {Lin Kedhashmap<integer, string> map = new Linkedhashmap<integer, string> (); Calendar C = new GregorianCalendar (); C.set (year, Calendar.december, D, N, D, d); 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));d ayofweekend = Sdf.format (Getlastdayofweek (year, i)); Map.put (New Integer (i), "first" + i + "week (from" + Dayofweekstart + "to" + Dayofweekend + ")"); return map;} /** * Gets the total number of weeks in a year * * @param years * @return */public static int getweekcountinyear (int year) {Calendar c = new Gregoriancale Ndar (); C.set (year, Calendar.december, N, +, +); int count = Getweekofyear (C.gettime ()); return count;} /** * Gets the current date is how many weeks * * @param date * @return */public static int getweekofyear (date date) {Calendar c = new Gregoriancalend AR (); C.setfirstdayofweek (Calendar.monday); C.setminimaldaysinfirstweek (7); C.settime (date); return C.get ( Calendar.week_of_year);} /** * Get the first day of a certain week * * @param year * @param week * @return */public static Date getfirstdayofweek (int year, int week) {Cale Ndar 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) {Cale Ndar 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 */public static Date getfirestdayofmonth (int year, int month) { month = month-1; Calendar C = calendar.getinstance (); C.set (Calendar.year, year); C.set (Calendar.month, MONTH); @SuppressWarnings (" Static-access ") int day = C.getactualminimum (c.day_of_month); C.set (Calendar.day_of_month, day); return C.gettime ();} /** * mentions the last day of a certain month * * @param year * @param month * @return */public static Date getlastdayofmonth (int year, int month) {m Onth = month-1; Calendar C = Calendar.getiNstance (); C.set (Calendar.year, year); C.set (Calendar.month, MONTH); @SuppressWarnings ("static-access") int day = C.getactualmaximum (C.day_of_month); C.set (Calendar.day_of_month, day); return C.gettime ();} /** * Gets the first day of the week of the current date * * @param date * @return */public static date Getfirstdayofweek (date date) {Calendar c = new Gregori Ancalendar (); C.setfirstdayofweek (Calendar.monday); c.settime (date); C.set (Calendar.day_of_week, C.getfirstdayofweek ()); Mondayreturn 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 Gregori Ancalendar (); C.setfirstdayofweek (Calendar.monday); c.settime (date); C.set (Calendar.day_of_week, C.getfirstdayofweek () + 6); Sundayreturn c.gettime ();} /** * Get the first day of a certain quarter * * @param year * @param quarter * @return */public static Date getfirstdayofquarter (int years, int quart ER) {int month = 0;if (Quarter > 4) {return null;} else {month = (quarter-1) * 3 + 1;} Return Getfirstdayofmonth (year, month);} /** *Get the last day of a certain quarter * * @param year * @param quarter * @return */public static Date getlastdayofquarter (int year, int quarter) {i NT month = 0;if (Quarter > 4) {return null;} else {month = quarter * 3;} Return Getlastdayofmonth (year, month);} /** * Get the first day of the year * * @param years * @return */public static Date getfirstdayofyear (int) {return getfirstdayofquarter (yea R, 1);} /** * Get the last day of the year * * @param years * @return */public static Date getlastdayofyear (int year) {return getlastdayofquarter , 4);} /** * Get the first day of a certain month * * @param year * @param month * @return */public static Date getfirstdayofmonth (int years, int month) {C Alendar C = calendar.getinstance (); C.set (Calendar.year, year); C.set (Calendar.month, month-1); C.set (calendar.day_of_ MONTH, C.getactualminimum (Calendar.day_of_month)); return C.gettime ();} /** * Gets the first and last day of the week of a certain month (Monday is the first day) (flag=true) * * @param year * @param month * @param week * @param flag * @return */ public static String Getdaybyweek (int year, int month, int week,boolean flag{Calendar cal = Calendar.getinstance (); Cal.set (Calendar.year, year); Cal.set (Calendar.month, month-1); Cal.set ( Calendar.week_of_month, WEEK); int dw = Cal.get (Calendar.day_of_week); if (!flag) Cal.settimeinmillis ( Cal.gettimeinmillis () + (8-DW) * 24 * 60*) Elsecal.settimeinmillis (Cal.gettimeinmillis ()-(dw-2) * 60 * 60* 1000); SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd"); String showTime = Formatter.format (Cal.gettime ()); return showtime.tostring ();} public static String getstringtodate (date date) {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd"); return Sdf.format (date);} /** * Convert String time format * * @param strtime * @return * @throws parseexception */public static string formattimestring (String strt IME) throws ParseException {SimpleDateFormat getsdf = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss.sss"); SimpleDateFormat resdf = new SimpleDateFormat ("yyyy year mm DD day"); return Resdf.format (Getsdf.parse (Strtime));} /** * Gets the number of weeks for a one month * @param date * @throws parseexceptIon * @throws Exception */public static int Getmonthweek (String date) throws ParseException {SimpleDateFormat SDF = new S Impledateformat ("yyyy-mm");D ate s = sdf.parse (Date); Calendar CA = calendar.getinstance (); Ca.settime (s); Ca.setfirstdayofweek (calendar.monday); int weeks = Ca.getactualmaximum (calendar.week_of_month); return weeks;};}

  

Java Time Tool Class (Project utility)

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.