我知道,在如何获得<代码”方面,有许多问题。 日期:s in Java,但我希望有一例使用新的Java 8 代码
该职能必须符合这些限制:
- Prevent errors from date savetime
- Inputs are two
Date
objects (without time, I know aboutLocalDateTime
, but I need to do this withDate
instances)
我知道,在如何获得<代码”方面,有许多问题。 日期:s in Java,但我希望有一例使用新的Java 8 代码
该职能必须符合这些限制:
Date
objects (without time, I know about LocalDateTime
, but I need to do this with Date
instances)如果你想要<>逻辑日历日,则使用 http://docs.oracle.com/javase/8/docs/api/java/time/time/ChronoUnit.html” rel=“nofollow noreferer”
:java.time.ChronoUnit
LocalDate dateBefore;
LocalDate dateAfter;
long daysBetween = DAYS.between(dateBefore, dateAfter);
如果您希望<literal 24小时,(a)duration,请您使用rel=“nofollow noreferer”>Duration
。 而分类:
LocalDate today = LocalDate.now()
LocalDate yesterday = today.minusDays(1);
// Duration oneDay = Duration.between(today, yesterday); // throws an exception
Duration.between(today.atStartOfDay(), yesterday.atStartOfDay()).toDays() // another option
根据移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民、移民等问题。
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.
时间:ChronoUnit
e.g.
import java.time.temporal.ChronoUnit;
...
long totalDaysBetween(LocalDate dateBefore, LocalDate dateAfter) {
return DAYS.between(dateBefore, dateAfter);
如果<>
和endDate 是javautil. 日期:
我们可以使用<><><>>><>>>>>>>>> 方法,从<><>>>>>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());
}
2. 使用最能满足你们需要的类别或方法:
期限用基于时间的价值衡量一定时间(秒、纳米秒)。
期间使用基于日期的数值(年、月、日)。
如果你想要在一个时间的单一单位衡量一定时间,例如天数或秒,那么ChronoUnit.between方法就是有益的。
rel=“nofollow noreferer”>https://docs.oracle.com/javase/tutorial/datetime/iso/Term.html
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);
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...