简体   繁体   中英

Converting number of days(int) into days,months and years in java (including leap year)

  1. I used this code to convert number of days into respecting days,months and year
  2. But the result are not precise because i don't take account for leap year and month with 31 days
  3. What is the best way to solve/encounter this issue
  4. field_Date1 and field_Date2 are input from my program
  5. duration=field_Date2-field_Date1
  6. duration is the number of days(int b)
  7. In the if else i do the conversion(but the condition are not precise)

     import java.util.concurrent.TimeUnit; import java.math.RoundingMode; import java.text.DecimalFormat; if (field_Date1 == null || field_Date2 == null){ return ""; } else { Date startDate = (Date)field_Date1; Date endDate = (Date)field_Date2; long duration = endDate.getTime() - startDate.getTime(); long diffInDays = TimeUnit.MILLISECONDS.toDays(duration); long diff = duration - TimeUnit.DAYS.toMillis(diffInDays); double diffToHours = TimeUnit.MILLISECONDS.toHours(diff); float hoursToDay = (float) (diffToHours / 24.0); float a =hoursToDay+diffInDays; a=Math.floor(a) int b = (int)a if(b<30) { StringBuilder sb = new StringBuilder("Day: ") sb.append(b) String c = sb.toString() c } else if(b<366) { int months = b/30 int days_out=b%30 StringBuilder p1 = new StringBuilder("Days: ") StringBuilder p2 = new StringBuilder("Months: ") StringBuilder p3 = new StringBuilder(" ") p1.append(days_out) p2.append(months) p2.append(p3) p2.append(p1) String c=p2.toString() c } else { StringBuilder p1 = new StringBuilder("Months: ") StringBuilder p2 = new StringBuilder("Years: ") StringBuilder p3 = new StringBuilder(" ") StringBuilder p4 = new StringBuilder("Days: ") int years = b/365 int days_out=b%365 if(days_out>30) { int m1 = days_out/30 int m2 = days_out%30 p2.append(years) p1.append(m1) p4.append(m2) p2.append(p3) p2.append(p1) p2.append(p3) p2.append(p4) String hj = p2.toString() return hj } else { p4.append(days_out) p2.append(years) p2.append(p3) p2.append(p4) String c=p2.toString() return c } } } 

Joda-Time

Try using Joda-Time 2.5:

Snippet will look something like this:

import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;

Date startDate = (Date)field_Date1;
Date endDate = (Date)field_Date2;
int days = Days.daysBetween( new DateTime(startDate), new DateTime(endDate) ).getDays(); 

java.time

Or the following method from java.time (Java8) can be used:

public static Period between(LocalDate startDateInclusive,
                             LocalDate endDateExclusive)

This obtains a period between two dates, consisting of the number of years, months, and days.

If you want the difference between two dates in days, including taking into account leap years etc, the java.time package (new in Java 8) gives you:

LocalDate firstDate = LocalDate.of(2014, Month.DECEMBER, 1);
LocalDate secondDate = LocalDate.of(2016, Month.MARCH, 12);
long days = firstDate.until(secondDate,ChronoUnit.DAYS);

gives you 467 days.

Alternatively,

Period period = firstDate.until(secondDate);

will give you a Period object, which stores the time broken down into years, months and days ie. instead of 467 days, you get 1 year, 3 months and 11 days. This is good for human readability. However, if you want the total days, it's not easy to get that from the Period object, so you're better off going with the first option I gave.

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