用Java从当前日期减去一个日期。

huangapple go评论76阅读模式
英文:

Subtracting a date from the current day in Java

问题

以下是翻译好的部分:

我有一种特定的日期格式。我可以计算两个日期之间的差值,但我无法实现从将来的日期中减去今天的日期。

我找到了获取今天日期的方法:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now));

以下是我计算两个日期差值的方法(字符串形式):

public boolean DateDiff(String datep, String dater) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate date1 = LocalDate.parse(datep, formatter);
    LocalDate date2 = LocalDate.parse(dater, formatter);
    
    long elapsedDays = ChronoUnit.DAYS.between(date1, date2);

我无法将当前日期转换为字符串,这样我就可以拥有两个相同类型的变量进行减法运算。LocalDateTime now 是什么类型?

英文:

I have a particular date format.I can subtract 2 dates but I can't achieve to subtract today's date from a future one.

I found the way to get today's date

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
	LocalDateTime now = LocalDateTime.now();
	System.out.println(dtf.format(now));

Here is the way I subtract 2 dates(Strings)

public boolean DateDiff(String datep,String dater) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
		LocalDate date1 = LocalDate.parse(datep, formatter);
		LocalDate date2 = LocalDate.parse(dater, formatter);
		
		long elapsedDays = ChronoUnit.DAYS.between(date1, date2);

I can't turn the current day to a String so I can have 2 same types of variables to subtract.What kind of type is the LocalDateTime now

答案1

得分: 3

减去日期就像这样使用LocalDate的minusDays()函数一样简单:

LocalDate now = LocalDate.now();
LocalDate thirtyDaysAgo = now.minusDays(30);

如果你有一个标准的日期字符串,比如"2020-08-27",你可以这样解析它:

String date = "2020-08-27";
LocalDate thirtyDaysAgo = LocalDate.parse(date).minusDays(30);
System.out.println(thirtyDaysAgo); //输出 2020-07-28

如果你有一个特殊格式的日期,比如"2020/08/27",你可以这样解析它:

String date = "2020/08/27";
LocalDate thirtyDaysAgo = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd")).minusDays(30);
System.out.println(thirtyDaysAgo); //输出 2020-07-28

要将LocalDate转换为带有格式的字符串,你可以这样做:

LocalDate date = LocalDate.now();
String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
System.out.println(formattedDate); //输出 2020/08/27
英文:

To subtract dates is as easy as this one using the minusDays() function of LocalDate:

LocalDate now = LocalDate.now();
LocalDate thirtyDaysAgo = now.minusDays(30);

If you have a standard string of date, see "2020-08-27", you can parse it this way:

String date = "2020-08-27";
LocalDate thirtyDaysAgo = LocalDate.parse(date).minusDays(30);
System.out.println(thirtyDaysAgo); //prints 2020-07-28

If you have a special format of date, see "2020/08/27", you parse it this way:

String date = "2020/08/27";
LocalDate thirtyDaysAgo = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd")).minusDays(30);
System.out.println(thirtyDaysAgo); //prints 2020-07-28

To turn a LocalDate to a String with format, you do it this way:

LocalDate date = LocalDate.now();
String formatedDate = date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
System.out.println(formatedDate); //prints 2020/08/27

答案2

得分: 1

使用LocalDate替代LocalDateTime来获取今天的日期

LocalDate currentDate = LocalDate.now();

如果出于某种原因仍希望使用LocalDateTime来获取今天的日期,可以按照以下方式进行操作

LocalDateTime currentDateTime = LocalDateTime.now();
LocalDate currentDate = currentDateTime.toLocalDate();

如果你想将currentDate按照所需的格式转换为String,可以使用以下方法:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");   
String currentDateString = currentDate.format(formatter);

如果你手头的两个日期都是LocalDate类型,可以按照以下方式计算它们之间的天数差:

public long DateDiff(LocalDate date1, LocalDate date2) {
    return Duration.between(date1, date2).toDays();
}

如果你手头的两个日期都是String类型,可以按照以下方式计算它们之间的天数差:

public long DateDiff(String dateString1, String dateString2) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate date1 = LocalDate.parse(dateString1, formatter);
    LocalDate date2 = LocalDate.parse(dateString2, formatter);

    return Duration.between(date1, date2).toDays();
}

希望这能解决你的问题。

英文:

Use LocalDate instead of LocalDateTime to get today's date

LocalDate currentDate = LocalDate.now();

For some reason if you still want to use LocalDateTime to get today's date, this is how you can do it

LocalDateTime currentDateTime = LocalDateTime.now();
LocalDate currentDate = currentDateTime.toLocalDate();

if you want to convert currentDate to String in your desired format as follows:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");   
String currentDateString = currentDate.format(formatter);

if both the dates you have are of type LocalDate, then you can find the number of days in between them as follows:

public long DateDiff(LocalDate date1,LocalDate date2) {

    return Duration.between(date1, date2).toDays();
}

if both the dates you have are of type String, then you can find the number of days in between them as follows:

public long DateDiff(String dateString1,String dateString2) {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate date1 = LocalDate.parse(dateString1, formatter);
    LocalDate date2 = LocalDate.parse(dateString2, formatter);

   return Duration.between(date1, date2).toDays();
}

Hope that solves your problem

答案3

得分: 0

你可以使用joda-time库,在Java中处理日期时非常方便。

减去两个日期

DateTime yesterday = new DateTime().withTimeAtStartOfDay().minusMinutes(10);

DateTime twoDaysFromToday = new DateTime().withTimeAtStartOfDay().plusDays(2);

Days.daysBetween(yesterday.toLocalDate(), twoDaysFromToday.toLocalDate()).getDays();
英文:

You can use joda-time library its a bliss when working with dates in java.

Subtract 2 dates

DateTime yesterday = new DateTime().withTimeAtStartOfDay().minusMinutes(10);

DateTime twoDaysFromToday = new DateTime().withTimeAtStartOfDay().plusDays(2);

Days.daysBetween(yesterday.toLocalDate(), twoDaysFromToday.toLocalDate()).getDays();

答案4

得分: 0

使用 LocalDate(而不是 LocalDateTime)。

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

LocalDate today = LocalDate.now(ZoneId.of("Asia/Calcutta"));
LocalDate futureDate = LocalDate.parse("26/09/2020", formatter);
long elapsedDays = ChronoUnit.DAYS.between(today, futureDate);

System.out.println("未来日期前的天数:" + elapsedDays);

刚刚运行时的输出结果:

> 未来日期前的天数:29
英文:

Use LocalDate (not LocalDateTime).

	DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
	
	LocalDate today = LocalDate.now(ZoneId.of("Asia/Calcutta"));
    LocalDate futureDate = LocalDate.parse("26/09/2020", formatter);
    long elapsedDays = ChronoUnit.DAYS.between(today, futureDate);
    
    System.out.println("Days until future date: " + elapsedDays);

Output was when running just now:

> Days until future date: 29

huangapple
  • 本文由 发表于 2020年8月28日 02:07:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63621871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定