简体   繁体   中英

How to convert months to exact number of days in java

I have a requirement where I have to compare 2 variables. One is difference between 2 dates ie, "Purchase Date" and "Date Call Received" which is coming as days. And the other one is Warranty length which is coming from UI as Months. Now, I am not able to compare these 2, as one is in months and other is in days. Could someone please help me how to convert months to days so that I can move forward.

for (ModelWarranty warr : modelWarranties) 
{

    if (null != warr.getWarrantyType() 
         && warr.getWarrantyType().equals("WARR") 
         && warr.getWarrantyPeriod().equals("0"))                           
         //WARRANTY_PERIOD "0" means value from UI saves in DB as days
    {

    }

    if (null != warr.getWarrantyType()
         && warr.getWarrantyType().equals("WARR")
         && warr.getWarrantyPeriod().equals("1")) 
         //WARRANTY_PERIOD "1" means value from UI saves in DB as months
    {
        Integer months = warr.getWarrantyLength();  
        //how to convert this months into days?
    }

    if (null != warr.getWarrantyType()
          && warr.getWarrantyType().equals("WARR")
          && warr.getWarrantyPeriod().equals("2")) 
          //WARRANTY_PERIOD "2" means value from UI saves in DB as years
    {
        Integer years = warr.getWarrantyLength();
        //how to convert this years into days?
    }

}

If all you've got are those two numbers, you can't. Say you've got 29 days until call was received, and 1 month warranty length. The 29 days could be from January 15 to February 13, less than 1 month. Or they could be from February 15, 2018, to March 16, more than a month. You need to know the purchase date or something else to anchor your days and month to the calendar.

If that were me, I might hand code a conversion table that shows the maximum number of days in a certain number of months, so as to be sure always to give the customer the credit they are entitled to. 1 month can be 31 days. Two months may be 62 days (for example July and August). Three months cannot be more than 92 days (31 + 31 + 30). 12 months may be 366 days, but 24 months can only be 731 days since there are never two leap years in a row. Fill out the rest yourself, please.

Nerdy edit: I believe that you can build your conversion table by counting backward from January 2017, inclusive. So 1 month is January = 31 days. Two months are December 2016 + January 2017 = 31 + 31 = 62. Three months are November 2016 through January 2017. The trick about this way is: You get a group of two 31 days months first. You get two such groups as early as possible (July–August 2016 and December 2016–January 2017). You get the short month, February, as late as possible, and the first time you get it, it's in a leap year (February 2016). Count up to 48 months. If the warranty is longer, say, 100 months, take that as 48 + 48 + 4 months, look those month counts up individually and sum. Because the leap year cycle is 48 months long (= 4 years). This is not always true, for example year 2100 will not be a leap year; but if the warranty cannot be longer than 199 years, the numbers you get will be correct.

In order to take in count that months can have 30/31/28 days, I would make the comparison in months in the next way:

double numMonths;  //number of months you get from your program
double numDays;   //number of days you get from your program for the second date

double daysInMonths = numDays * (12/365.25);
double difference = Math.abs(daysInMonths-numMonths);

That way, you can compare the date in months with the date in days without any kind of problem.

Note that a year has 365,25 days, exactly!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM