Java 如何计算到日期之间的天数
所以我想计算一下两个约会挑拣者之间的间隔时间。 所以我试着做了两个数组,但没有成功 有什么想法吗 这是我的约会对象Java 如何计算到日期之间的天数,java,android,date,days,date-difference,Java,Android,Date,Days,Date Difference,所以我想计算一下两个约会挑拣者之间的间隔时间。 所以我试着做了两个数组,但没有成功 有什么想法吗 这是我的约会对象 将日历字符串日、月、年转换为日期类 更多讨论如下: e、 g 创建一个方法getDates() private static ArrayList<Date> getDates(String dateString1, String dateString2) { ArrayList<Date> arrayofdates = new ArrayList&l
将日历字符串日、月、年转换为日期类 更多讨论如下: e、 g
创建一个方法getDates()
private static ArrayList<Date> getDates(String dateString1, String dateString2)
{
ArrayList<Date> arrayofdates = new ArrayList<Date>();
DateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
Date date1 = null;
Date date2 = null;
try {
date1 = df1 .parse(dateString1);
date2 = df1 .parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calender1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calender1.setTime(date1);
calender2.setTime(date2);
while(!calender1.after(calender2))
{
arrayofdates.add(calender1.getTime());
calender1.add(Calendar.DATE, 1);
}
return arrayofdates;
}
私有静态数组列表getDates(字符串dateString1,字符串dateString2)
{
ArrayList arrayofdates=新的ArrayList();
DateFormat df1=新的简化格式(“dd-MM-yyyy”);
Date date1=null;
日期2=空;
试一试{
date1=df1.parse(dateString1);
date2=df1.parse(dateString2);
}捕获(解析异常){
e、 printStackTrace();
}
Calendar calender1=Calendar.getInstance();
Calendar calendar2=Calendar.getInstance();
压延机1.设定时间(日期1);
压延机2.设定时间(日期2);
而(!压延机1.之后(压延机2))
{
add(calender1.getTime());
日历1.添加(Calendar.DATE,1);
}
返回数组日期;
}
然后在此方法中传递参数以获取日期数组
当您使用DatePicker时
DateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
ArrayList<Date> mBaseDateList = getDates(df1.format(cal1.time), df1.format(cal2.time))
DateFormat df1=新的SimpleDataFormat(“dd-MM-yyyy”);
ArrayList=getDates(df1.format(cal1.time),df1.format(cal2.time))
Scanner in=新扫描仪(System.in);
int n=in.nextInt();
日期d1、d2;
Calendar cal=Calendar.getInstance();
SimpleDataFormat sdf=新SimpleDataFormat(“yyyy-MM-dd”);
sdf.setTimeZone(TimeZone.getTimeZone(“UTC”));
对于(int i=0;i
欢迎来到堆栈溢出。我不理解你的问题,因为我在你的代码中没有看到任何数组。请编辑并解释。也不要将代码作为图像发布。请粘贴为文本,格式为代码。抱歉,如果我们要求的是::-)我建议您不要使用日历
。那门课设计得很糟糕,而且早已过时。改为使用中的LocalDate
from。这也会使你的任务变得非常简单:只需使用。但是我如何把字符串和日历拿出来让我的日期选择器。。。正如你在我上传的照片中看到的。oh和ty为日历创建两个全局变量<代码>calA和calB
。当您从datepicker中选择date时,只需执行以下操作-calA.setTime(cal1.getTime())代码>。这将改变calA的时间。对calB也要这样做。将参数传递给getDates()
时。format(cal1.time)将自动将其转换为字符串。
DateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
ArrayList<Date> mBaseDateList = getDates(df1.format(cal1.time), df1.format(cal2.time))
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Date d1, d2;
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
for (int i = 0; i < n; i++) {
try {
d2 = sdf.parse(in.next());
d1 = sdf.parse(in.next());
long differene = (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24);
System.out.println(Math.abs(differene));
} catch (Exception e) {
}
}
public static int getDaysBetweenDates(Date fromDate, Date toDate) {
return Math.abs((int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24)));
}