JAVA Calender Use implementation query the 1th and last day of one months, learning skills.

Source: Internet
Author: User

Use of calender:
Package Test;import Java.text.parseexception;import Java.text.simpledateformat;import java.util.ArrayList;import Java.util.calendar;import Java.util.list;public class Test {public static void main (string[] args) throws ParseException {list<string> List = GetTime ("2015-06-26", "2014-02-14"); SYSTEM.OUT.PRINTLN (list); GetTime ("2015-06-26");} The month period in which the query period is located. This could use a monthly target performance public static list<string> GetTime (String starttime,string endTime) throws parseexception{list< string> list = new arraylist<string> (); SimpleDateFormat startsdf = new SimpleDateFormat ("Yyyy-mm-dd"); Calendar can = Calendar.getinstance (); Can.settime (Startsdf.parse (StartTime)); Can.set (Calendar.day_of_month, 1); System.out.println (Startsdf.format (Can.gettime ())), List.add (Startsdf.format (Can.gettime ())), Can.settime ( Startsdf.parse (EndTime)); Can.set (Calendar.day_of_month, 1); Can.roll (Calendar.day_of_month,-1); System.out.println (Startsdf.format (Can.gettime ()) + "23:59:59") List.add (Startsdf.format (Can.gettime ()) +"23:59:59"); return list;} public static void GetTime (String nowtime) throws parseexception{//time format session SimpleDateFormat SDF = new SimpleDateFormat ("yyy Y-mm-dd ");//Generate Calender Object Calendar can = Calendar.getinstance ();//Turn the calendar to the current time Can.settime (Sdf.parse (Nowtime));// Set Day is the first day of the month Can.set (Calendar.day_of_month, 1); System.out.println ("The first day of the nowtime month of the time:" +sdf.format (Can.gettime ()));/** * Output Result: * Time Nowtime first day of month: 2015-06-01 */// If you want to add a day or a day, it is possible to use Add, and roll these two functions//add method Can.add (Calendar.day_of_month, 1); System.out.println ("Time of Nowtime plus one day:" +sdf.format (Can.gettime ())); Can.add (Calendar.day_of_month,-1); System.out.println ("Time Nowtime minus one day:" +sdf.format (Can.gettime ()));//roll method Can.roll (Calendar.day_of_month, 1); System.out.println ("Time of Nowtime plus one day:" +sdf.format (Can.gettime ())); Can.roll (Calendar.day_of_month,-1); System.out.println ("Time Nowtime minus one day:" +sdf.format (Can.gettime ()));/** * Output Result: * Time of Nowtime plus day: 2015-06-02 * Time of Nowtime Minus Day: 2015-06-01 * Time Nowtime plus day: 2015-06-02 * Time Nowtime minus one day: 2015-06-01 *///Someone will ask add and RolL What is the difference//This is I will say to him roll is the meaning of the loop, which is also the difference between his and add roll will be based on the Calendar.day_of_month this only loop//How a loop it? For example, the following code: Now I will adjust the calendar to 2015-06-01 I do a day of Operation Can.roll (Calendar.day_of_month,-1); System.out.println ("Time Nowtime minus one day:" +sdf.format (Can.gettime ())); Can.add (Calendar.day_of_month, 1); System.out.println ("Time Nowtime minus one day:" +sdf.format (Can.gettime ()));/** * This is the result of the output: * Time Nowtime minus one day: 2015-06-30 * Time Nowtime minus one day: 2015-07-01 *///So you can use this roll to determine the first and last day of a month. }}
Thank you for your support and love I will continue to refuel! ~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JAVA Calender Use implementation query the 1th and last day of one months, learning skills.

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.