Java programming to calculate the code of the Two-date month difference instance, java instance

Source: Internet
Author: User

Java programming to calculate the code of the Two-date month difference instance, java instance

This example mainly calculates the month difference between two dates, as shown in the following code:

Package com. forezp. util; import org. joda. time. dateTime; import org. joda. time. months; import org. joda. time. format. dateTimeFormat; import org. joda. time. format. dateTimeFormatter;/*** in JAVA, how do I calculate the month difference between two dates? <Br> *** @ author Administrator **/public class Demo1 {public static void main (String [] args) {DateTimeFormatter formatter = DateTimeFormat. forPattern ("yyyy-MM-dd"); DateTime start = formatter. parseDateTime ("2017-07-11"); DateTime end = formatter. parseDateTime ("2017-11-19"); int months = Months. monthsBetween (start, end ). getMonths (); System. out. println (months );}}

Running result:

Maven import dependency:

 <dependency>  <groupId>joda-time</groupId>  <artifactId>joda-time</artifactId>  <version>2.9.6</version></dependency>

Summary

The above is all about the Java programming implementation of the month difference instance code for two dates, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.