简体   繁体   English

如何在Java中将月份转换为确切的天数

[英]How to convert months to exact number of days in java

I have a requirement where I have to compare 2 variables. 我有一个必须比较2个变量的要求。 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. 另一个是“保修期限”,它来自UI月份。 Now, I am not able to compare these 2, as one is in months and other is in days. 现在,我无法比较这2个,因为一个是几个月,另一个是几天。 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. 假设您有29天的通话时间,而且还有1个月的保修期。 The 29 days could be from January 15 to February 13, less than 1 month. 这29天可能是1月15日至2月13日,不到1个月。 Or they could be from February 15, 2018, to March 16, more than a month. 或者可能是从2018年2月15日到3月16日,超过一个月。 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. 1个月可以是31天。 Two months may be 62 days (for example July and August). 两个月可能是62天(例如7月和8月)。 Three months cannot be more than 92 days (31 + 31 + 30). 三个月不能超过92天(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. 12个月可能是366天,但24个月只能是731天,因为连续没有两个leap年。 Fill out the rest yourself, please. 请自己填写其余部分。

Nerdy edit: I believe that you can build your conversion table by counting backward from January 2017, inclusive. 书呆子编辑:我相信您可以通过倒数2017年1月起(包括首尾两天)来建立转化表。 So 1 month is January = 31 days. 因此1个月等于1月= 31天。 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. 两个月是2016年12月+ 2017年1月= 31 + 31 =62。三个月是2016年11月至2017年1月。这种方式的窍门是:首先得到两个31天的小组。 You get two such groups as early as possible (July–August 2016 and December 2016–January 2017). 您会尽快得到两个这样的小组(2016年7月至2016年8月以及2016年12月至2017年1月)。 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). 您会获得尽可能短的月份,即二月,这是第一次,这是a年(2016年2月)。 Count up to 48 months. 数到48个月。 If the warranty is longer, say, 100 months, take that as 48 + 48 + 4 months, look those month counts up individually and sum. 如果保修期更长(例如100个月),则将其作为48 + 48 + 4个月,将这些月份逐一累加并加总。 Because the leap year cycle is 48 months long (= 4 years). 因为the年周期为48个月(= 4年)。 This is not always true, for example year 2100 will not be a leap year; 并非总是如此,例如2100年将不是a年; but if the warranty cannot be longer than 199 years, the numbers you get will be correct. 但是,如果保修期不能超过199年,则您得到的数字将是正确的。

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: 为了算出几个月可以有30/31/28天,我将采用以下方式以月为单位进行比较:

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! 请注意,一年恰好有365,25天!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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