Calculate the month Difference tool class

Source: Internet
Author: User

Meet the requirements is the type of timestamp, calculate the time difference, if full of less than one months according to one months calculation. Users May 27 ordered, until June 27 expires, even in May there are 31 days.

  

Package File;import Java.sql.timestamp;public class Timeutil {public static void main (string[] args) {String time1= "2000- 02-29 01:12:12 "; String time2= "2000-03-30 12:12:12"; Timestamp a=timestamp.valueof (time1); Timestamp b=timestamp.valueof (time2); System.out.println (A + "| |" +B); int y1=a.getyear () +1900; int m1=a.getmonth () +1; int d1=a.getdate (); int y2=b.getyear () +1900; int m2=b.getmonth () +1; int d2=b.getdate (); System.out.println (y1+ "-" +m1+ "-" +d1 "); System.out.println (y2+ "-" +m2+ "-" +d2 "), int month= (y2-y1) *12+m2-m1;if (m1==2&&m2!=2) {if (d2>d1) {month= month+1;}} if (m1!=2&&m2==2) {if (d2>d1) {month=month+1;}} else if ((m2==1| | m2==3| | m2==5| | m2==7| | m2==8| |m2==10| | m2==12) && (m1==2| | m1==4| | m1==6| | m1==9| | m1==11) {//Calculate leap year 29 days if (m1==2&& (y1%4==0&&y1%100!=0) | | y1%400==0)) {if (d2==29| | d2==30| | d2==31| | d1==29) {}else{month=month+1;}} Else{if (d2==29| | d2==30| | d2==31| | d1==28) {}else{month=month+1;}}} else if ((m1==1| | m1==3| | m1==5| | m1==7| | m1==8| |m1==10| | m1==12) && (m2==4| | m2==6||m2==9| | m2==11) {if (d2>d1) {month=month+1;}} SYSTEM.OUT.PRINTLN (month);}}

  

Calculate the month Difference tool class

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.