Date help class

Source: Internet
Author: User
Tags date1

Package cn.com. eatlab. report. util; import Java. util. calendar; import Java. util. date; public class dateutils {/*** get the start and end dates of a month * @ Param monthnum 0 this month-1, last month, and so on * @ return */@ suppresswarnings ("deprecation") public static date [] getdatefirstdayandlastday (integer monthnum) {calendar calendar1 = calendar. getinstance (); int month = calendar1.get (calendar. month); // the current month monthnum =-1,-2... 2013-04-24 11: 30: 16system. out. println ("current month: Calendar. month: "+ month); calendar1.set (calendar. month, month + monthnum); // 2013-02-24 11:30:16 // get the last day of the month calendar ar1.set (calendar. month, calendar1.get (calendar. month) + 1); calendar1.set (calendar. date, 1); calendar1.add (calendar. date,-1); // int lastday = calendar1.get (calendar. date); calendar1.set (calendar. month, month + monthnum); calendar1.set (calendar. date, 1); calendar1.set (calendar. hour_of_day, 0); calendar1.set (calendar. minute, 0); calendar1.set (calendar. second, 0); Date date1 = calendar1.gettime (); calendar1.set (calendar. date, lastday); calendar1.set (calendar. hour_of_day, 23); calendar1.set (calendar. minute, 59); calendar1.set (calendar. second, 59); Date date2 = calendar1.gettime (); date [] Dates = new date [2]; dates [0] = date1; dates [1] = date2; return dates;}/*** get the number of days of the previous premonthnum month. 0 indicates this month, -1 last month * @ Param premonthnum * @ return */public static integer getdaynumofmonth (integer premonthnum) {calendar calendar1 = calendar. getinstance (); int month = calendar1.get (calendar. month); calendar1.set (calendar. month, month + premonthnum); calendar1.set (calendar. month, calendar1.get (calendar. month) + 1); calendar1.set (calendar. date, 1); calendar1.add (calendar. date,-1); // int lastday = calendar1.get (calendar. date); Return lastday;} public static string dateformat (Java. util. date) {// Java. text. simpledateformat SDF = new Java. text. simpledateformat ("yyyy/mm/dd hh: mm: SS"); // string C1 = SDF. format (date); // system. out. println ("Full Time Format:" + date. tolocalestring (); string temptimestr = ""; // obtain the year, month, and day of the time to be compared. Int tempdate = date. getdate (); int tempmonth = date. getmonth () + 1; int tempyear = date. getyear () + 1900; // system. out. println (tempdate + "" + tempmonth + "" + tempyear); // obtain the year, month, and day of the current time. getinstance (); // system. out. println ("now:" + now. tostring (); // system. out. println ("Date:" + now. get (calendar. date) + "-month:" + now. get (calendar. month) + "-year:" + now. get (calendar. year); int nowdate = now. get (calendar. date); int nowmonth = now. get (calendar. month) + 1; int nowyear = now. get (calendar. year); If (nowyear> tempyear) {// last year Java. text. simpledateformat SDF = new Java. text. simpledateformat ("yyyy/mm/DD"); temptimestr = SDF. format (date);} else if (nowmonth-tempmonth> = 1) {// Java. text. simpledateformat SDF = new Java. text. simpledateformat ("yyyy/mm/DD"); temptimestr = SDF. format (date);} else if (nowdate-tempdate> = 2) {// the day before yesterday or this month Java. text. simpledateformat SDF = new Java. text. simpledateformat ("mm/DD"); temptimestr = SDF. format (date);} else if (nowdate-tempdate = 1) {// Java yesterday. text. simpledateformat SDF = new Java. text. simpledateformat ("HH: mm"); temptimestr = "yesterday";} else {// Today Java. text. simpledateformat SDF = new Java. text. simpledateformat ("HH: mm"); temptimestr = SDF. format (date) ;}return temptimestr ;}}

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.