Java gets the number of days in a month

Source: Internet
Author: User

Java gets the number of days in a month

  1. Calendar cal = new GregorianCalendar ();
  2. //or with Calendar cal = Calendar.getinstance ();
  3. /** Setting date**/
  4. SimpleDateFormat osdf = new SimpleDateFormat ("", locale.english);
  5. Osdf.applypattern ("yyyymm");
  6. try {
  7. System.out.println (Osdf.parse (date));
  8. Cal.settime (Osdf.parse (date));
  9. } catch (ParseException e) {
  10. E.printstacktrace ();
  11. }
  12. /** or set the month, note that the month is counted from 0, so use the actual month-1 is the month you want **/
  13. //January: Cal.set (2009, 1-1, 1);
  14. /** If you want to get the **/last month
  15. //cal.set (Calendar.day_of_month, 1);
  16. //date minus one to get the last day of the month time object
  17. //cal.add (Calendar.day_of_month,-1);
  18. //Output last day of the month date
  19. //system.out.println (Cal.get (calendar.day_of_month));
  20. /** starts using this method to get the maximum number of days of the month, always getting is 31 days **/
  21. //int num = cal.getmaximum (calendar.day_of_month);
  22. /** start with this method to get the maximum number of days in the actual month **/
  23. int num2 = Cal.getactualmaximum (calendar.day_of_month);
  24. System.out.println (NUM2);

Java gets the number of days in a month

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.