Java gets the current time, first and last day of the week, first and last day of the month, tool class

Source: Internet
Author: User

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

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.