[英]Converting number of days(int) into days,months and years in java (including leap year)
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 } } }
Try using Joda-Time 2.5: 尝试使用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();
Or the following method from java.time (Java8) can be used: 或者可以使用java.time(Java8)中的以下方法:
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: 如果您希望以天为单位的两个日期之间的差值(包括leap年在内),则java.time包 (Java 8中的新增功能)为您提供:
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. 给您467天。
Alternatively, 或者,
Period period = firstDate.until(secondDate);
will give you a Period object, which stores the time broken down into years, months and days ie. 将为您提供一个Period对象,该对象存储分解成年,月和日的时间,即。 instead of 467 days, you get 1 year, 3 months and 11 days. 而不是467天,而是1年3个月11天。 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. 但是,如果您想要总天数,那么从Period对象中获取该天数并不容易,因此最好选择我给的第一个选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.