Package Com.banksteel.openerp.commons.utils;import Java.text.simpledateformat;import Java.util.Calendar;public Class calendarutils{/** * @description: Gets the current time * @param pattern time format * @return Current time * @author: * @createTime: December 13, 2016 PM 4:36:32 */public static string Getnowtime (string pattern) {//When week start time Calendar currentdate = Calendar.getinstance (); SimpleDateFormat format = new SimpleDateFormat (pattern), if (pattern = = NULL | | pattern.equals ("")) {pattern = "YYYY-MM-DD" ;} Return Format.format (Currentdate.gettime ());} /** * @description: Gets the start time of the week * @param pattern time format * @return this week's start time * @author: * @createTime: December 13, 2016 PM 4:28:12 */publ IC static string Getweekstart (string pattern) {//When week start time Calendar currentdate = Calendar.getinstance (); Currentdate.setfirstdayofweek (Calendar.monday); Currentdate.set (calendar.hour_of_day, 0); CurrentDate.set ( Calendar.minute, 0); Currentdate.set (Calendar.second, 0); Currentdate.set (Calendar.day_of_week, Calendar.MONDAY); SimpleDateFormat format = new SimpleDateFormat(pattern); if (pattern = = NULL | | pattern.equals ("")) {pattern = "yyyy-mm-dd";} Return Format.format (Currentdate.gettime ());} /** * @description: Gets the end time of the week * @param pattern time format * @return end of the week * @author: * @createTime: December 13, 2016 PM 4:28:58 */publ IC static string Getweekend (string pattern) {//When week end time Calendar currentdate = Calendar.getinstance (); Currentdate.setfirstdayofweek (Calendar.monday); Currentdate.set (Calendar.hour_of_day); CurrentDate.set ( Currentdate.set (Calendar.second, Calendar.minute); Currentdate.set (Calendar.day_of_week, Calendar.SUNDAY); SimpleDateFormat format = new SimpleDateFormat (pattern), if (pattern = = NULL | | pattern.equals ("")) {pattern = "YYYY-MM-DD" ;} Return Format.format (Currentdate.gettime ());} /** * @description: Gets the first day of the current month * @param pattern time format * @return The first day of the current month * @author: * @createTime: December 13, 2016 PM 4:28:12 */public static string Getmonthstart (string pattern) {//Gets the first day of the current month Calendar currentdate = Calendar.getinstance (); Currentdate.add (calendar.month, 0); CurrentdatE.set (calendar.day_of_month,1);//set to number 1th, the current date is the first day of the month if (pattern = = NULL | | pattern.equals ("")) {pattern = "yyyy-mm-dd";} SimpleDateFormat format = new SimpleDateFormat (pattern); return Format.format (Currentdate.gettime ());} /** * @description: Gets the last day of the current month * @param pattern time format * @return The last day of the current month * @author: * @createTime: December 13, 2016 PM 4:28:58 */publ IC static string Getmonthend (string pattern) {//Gets the last day of the current month Calendar currentdate = Calendar.getinstance (); Currentdate.set (Calendar.day_of_month, Currentdate.getactualmaximum (Calendar.day_of_month)); if (pattern = = NULL | | pattern.equals (")) {pattern =" yyyy-mm-dd ";} SimpleDateFormat format = new SimpleDateFormat (pattern); return Format.format (Currentdate.gettime ());} public static void Main (string[] args) {//system.out.println ("Current Time:" +getnowtime ("Yyyy-mm-dd"));// System.out.println ("The first day of the Week:" +getweekstart ("Yyyy-mm-dd"))//system.out.println ("The last Day of the week:" +getweekend ("Yyyy-mm-dd" )); System.out.println ("The first day of the current month:" +getmonthstart ("Yyyy-mm-dd")); System.out.println ("WhenLast day of the previous month: "+getmonthend (" Yyyy-mm-dd "));}}
Java gets the current time, first and last day of the week, first and last day of the month, tool class