English 中文(简体)
Java两个日期之间的计算日 8
原标题:Calculate days between two Dates in Java 8

我知道,在如何获得<代码”方面,有许多问题。 日期:s in Java,但我希望有一例使用新的Java 8 代码。 我也知道Jodatime图书馆,但我想采用一种不依靠外部图书馆的方法。

该职能必须符合这些限制:

  1. Prevent errors from date savetime
  2. Inputs are two Date objects (without time, I know about LocalDateTime, but I need to do this with Date instances)
最佳回答
问题回答

根据移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民等问题。

ChronoUnit.DAYS.between(firstDate, secondDate)

http://docs.oracle.com/javase/8/docs/api/java/time/ localDate.html#until-java.time.Temporal-java.time.TemporalUnit-"rel=“nofollow noreferer”>until :

LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
LocalDate christmas = LocalDate.of(2014, Month.DECEMBER, 25);

System.out.println("Until christmas: " + independenceDay.until(christmas));
System.out.println("Until christmas (with crono): " 
   + independenceDay.until(christmas, ChronoUnit.DAYS));

<<>Output>:

Until christmas: P5M21D
Until christmas (with crono): 174

如评论所述,如果没有具体单位:until 回归Period

文件摘要:

A date-based amount of time in the ISO-8601 calendar system, such as 2 years, 3 months and 4 days .
This class models a quantity or amount of time in terms of years, months, and days. See Duration for the time-based equivalent to this class.

DAYS.between

时间:ChronoUnit

e.g.

import java.time.temporal.ChronoUnit;
...

long totalDaysBetween(LocalDate dateBefore, LocalDate dateAfter) {
    return DAYS.between(dateBefore, dateAfter);

如果<>endDatejavautil. 日期:

我们可以使用<><><>>><>>>>>>>> 方法,从<><>>>>ChronoUnit< enum:

public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) {
    //..
}

www.un.org/Depts/DGACM/index_spanish.htm 纽约总部 <><>>> 填满24小时。

import java.time.temporal.ChronoUnit;

ChronoUnit.DAYS.between(startDate.toInstant(), endDate.toInstant());

//OR 

ChronoUnit.DAYS.between(Instant.ofEpochMilli(startDate.getTime()), Instant.ofEpochMilli(endDate.getTime()));

时间:ChronoUnit。 如下:

Output : *Number of days between the start date : 2015-03-01 and end date : 2016-03-03 is ==> 368. **Number of days between the start date : 2016-03-03 and end date : 2015-03-01 is ==> -368*

package com.bitiknow.date;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

/**
 * 
 * @author pradeep
 *
 */
public class LocalDateTimeTry {
    public static void main(String[] args) {

        // Date in String format.
        String dateString = "2015-03-01";

        // Converting date to Java8 Local date
        LocalDate startDate = LocalDate.parse(dateString);
        LocalDate endtDate = LocalDate.now();
        // Range = End date - Start date
        Long range = ChronoUnit.DAYS.between(startDate, endtDate);
        System.out.println("Number of days between the start date : " + dateString + " and end date : " + endtDate
                + " is  ==> " + range);

        range = ChronoUnit.DAYS.between(endtDate, startDate);
        System.out.println("Number of days between the start date : " + endtDate + " and end date : " + dateString
                + " is  ==> " + range);

    }

}

每个人都说是使用ChronoUnit.BERS.between,但只有代表你可以称之为 yourself。 因此,你还可以做<条码>第1版。

这两份文件实际上都提到两种做法,并说使用哪一种方法更可读。

圣诞节前几天,从现在起,试图这样做。

System.out.println(ChronoUnit.DAYS.between(LocalDate.now(),LocalDate.of(Year.now().getValue(), Month.DECEMBER, 25)));

各位:

public class DemoDate {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        System.out.println("Current date: " + today);

        //add 1 month to the current date
        LocalDate date2 = today.plus(1, ChronoUnit.MONTHS);
        System.out.println("Next month: " + date2);

        // Put latest date 1st and old date 2nd in  between  method to get -ve date difference
        long daysNegative = ChronoUnit.DAYS.between(date2, today);
        System.out.println("Days : "+daysNegative);

        // Put old date 1st and new date 2nd in  between  method to get +ve date difference
        long datePositive = ChronoUnit.DAYS.between(today, date2);
        System.out.println("Days : "+datePositive);
    }
}

在两个日期之间,休息日。 日期

public static long daysBetweenTwoDates(Date dateFrom, Date dateTo) {
            return DAYS.between(Instant.ofEpochMilli(dateFrom.getTime()), Instant.ofEpochMilli(dateTo.getTime()));
        }

如果目标只是要在几天内实现区别,而且由于上述答复提到了代表方法,希望指出,也可以简单地使用——

public long daysInBetween(java.time.LocalDate startDate, java.time.LocalDate endDate) {
  // Check for null values here

  return endDate.toEpochDay() - startDate.toEpochDay();
}

我知道这个问题涉及 Java8,但 Java9可使用:

public static List<LocalDate> getDatesBetween(LocalDate startDate, LocalDate endDate) {
    return startDate.datesUntil(endDate)
      .collect(Collectors.toList());
}
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

LocalDate dateBefore =  LocalDate.of(2020, 05, 20);
LocalDate dateAfter = LocalDate.now();
    
long daysBetween =  ChronoUnit.DAYS.between(dateBefore, dateAfter);
long monthsBetween= ChronoUnit.MONTHS.between(dateBefore, dateAfter);
long yearsBetween= ChronoUnit.YEARS.between(dateBefore, dateAfter);
    
System.out.println(daysBetween);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签