Java gets the start and end times of the month, the time of the first and last days of the month, and the current date push forward one week, one months

Source: Internet
Author: User

ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.GregorianCalendar;Importorg.junit.Test; Public classTestdateutil {//1. Get the first day of the month@Test Public voidtestfordate () {//Specify return date formatSimpleDateFormat sf=NewSimpleDateFormat ("Yyyy-mm-dd"); Calendar Calendar=calendar.getinstance ();D ate thedate=Calendar.gettime (); GregorianCalendar Gclast=(GregorianCalendar) calendar.getinstance (); Gclast.settime (thedate);//set to First dayGclast.set (Calendar.day_of_month, 1); String Day_first=Sf.format (Gclast.gettime ());//Print the first day of the monthSystem.out.println (Day_first);}//2. Get the last day of the month@Test Public voidTestfordatelast () {//Get CalendarCalendar calendar=calendar.getinstance ();//set the date to the maximum date of the monthCalendar.set (calendar.date, Calendar.getactualmaximum (calendar. DATE));//Set Date formatSimpleDateFormat sf=NewSimpleDateFormat ("Yyyy-mm-dd"); String SS=Sf.format (Calendar.gettime ()); SYSTEM.OUT.PRINTLN (SS+ "23:59:59");}//3, very simple and practical to get the first and last day of this month@Test Public voidTestt () {SimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd"); Calendar C=calendar.getinstance (); C.add (Calendar.month,0); C.set (Calendar.day_of_month,1);//set to number 1th, the current date is the first day of the monthString first =Format.format (C.gettime ()); System.out.println ("=============== Month first day:" +First ); //get the last day of the current monthCalendar CA =calendar.getinstance ();         Ca.set (Calendar.day_of_month, Ca.getactualmaximum (Calendar.day_of_month)); String Last=Format.format (Ca.gettime ()); System.out.println ("===============:" +Last );}//4. Get the first day of last month@Test Public voidGetbeforefirstmonthdate ()throwsException{simpledateformat Format=NewSimpleDateFormat ("Yyyy-mm-dd"); Calendar Calendar=calendar.getinstance (); Calendar.add (Calendar.month,-1); Calendar.set (Calendar.day_of_month,1); System.out.println ("First day of last month:" +Format.format (Calendar.gettime ()));}//5. Get the last day of last month@Test Public voidGetbeforelastmonthdate ()throwsException{simpledateformat SF=NewSimpleDateFormat ("Yyyy-mm-dd"); Calendar Calendar=calendar.getinstance ();intMonth=Calendar.get (Calendar.month); Calendar.set (Calendar.month, MONTH-1); Calendar.set (Calendar.day_of_month, Calendar.getactualmaximum (Calendar.day_of_month)); System.out.println ("Last Last day:" +Sf.format (Calendar.gettime ()));}6, get the previous week of the current date, or the previous one-month time Public StaticString getfirstdate () {SimpleDateFormat SDF=NewSimpleDateFormat ("YyyyMMdd"); Calendar CL=calendar.getinstance ();//Cl.settime (datenow);//Cl.add (Calendar.day_of_year,-1);//Day//Cl.add (Calendar.week_of_year,-1);//a weekCl.add (Calendar.month, 1);//from now on, before one months, if 2 months, then-1-----"changed to -2Date Datefrom =cl.gettime ();returnSdf.format (datefrom);} @Test Public voidteststartdate () {System.out.println ("The current date pushes forward one months is:" +getfirstdate ()); //if the current date is 2015.11.08, then the print date is: 20151008 }}

Java gets the start and end times of the month, the time of the first and last days of the month, and the current date push forward one week, one months

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.