本文介绍了减去日期并得到差异的日期。如何减去他们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉如何从今天中减去字符串after,以获得日期差异。

  import java.text 。*; 
import java.util。*;


public class Main {
public static void main(String args []){


SimpleDateFormat sdf = new SimpleDateFormat( yyyy / MM / dd);
日历cal = Calendar.getInstance();
String today = sdf.format(cal.getTime());
System.out.println(今天);
cal.add(Calendar.DATE,20);
String After = sdf.format(cal.getTime());
System.out.println(之后);

}
}


解决方案


$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ code> Date today = LocalDate.now();
Date futureDate = LocalDate.now()。plusDays(1);
long days = Period.between(today,futureDate).getDays();

& 类可用于#java8



LocalDate docs






如果您不使用java8,请使用joda-time库的实用程序来计算此

  Days day = Days.daysBetween(startDate,endDate); 
int days = d.getDays();


can anyone tell how to subtract string "after" from "today" to get days difference.

import java.text.*;
import java.util.*;


public class Main {
public static void main(String args[]){


SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
Calendar cal=Calendar.getInstance();
String today=sdf.format(cal.getTime());
System.out.println(today);
cal.add(Calendar.DATE, 20);
String After=sdf.format(cal.getTime());
System.out.println(After);

}
}
解决方案

It would be easier with java8 where you dont need to subtract long values represent of date and change back to days, hours and minutes.

Date today= LocalDate.now();
Date futureDate = LocalDate.now().plusDays(1);
long days = Period.between(today, futureDate).getDays();

Period & LocalDate class are available in #java8

LocalDate docs


If you are not using java8, use joda-time library's org.joda.time.Days utility to calculate this

Days day = Days.daysBetween(startDate, endDate);
int days = d.getDays();

这篇关于减去日期并得到差异的日期。如何减去他们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 12:51