在Java中计算两个日期之间的日期 [英] Calculating days between two dates in Java

查看:112
本文介绍了在Java中计算两个日期之间的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将自己的练习输入到不同的字符串,整数等中。控制台并将输入写入文本文件。所有这一切都很好。



现在,我想在项目启动时和文本文件结束时输入文本文件。这样做没有任何问题。



现在这里是我的问题。我想计算项目从头到尾的持续时间。



目前,它只计算天数,忽略月份和年份。

 例如:(dd.mm.yyyy)
开始:07.07.2014结束:15.07.2014输出:8(几乎正确:))
另一个例子:
开始07.07。 2014结束:03.08.2014输出:-3(甚至不接近)

我只是无法弄清楚哪里我的问题是。



感谢您的帮助!
Chris

  System.out.print(Start project(dd.mm.yyyy):); 
String beginn = input.nextLine();
DateFormat df = new SimpleDateFormat(dd.mm.yyyy);
日期d = null;
try {
d = df.parse(beginn);
} catch(ParseException e){
System.out.println(Unable to parse+ beginn);
}
DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
String s3 = df3.format(d);

System.out.print(项目结束(dd.mm.yyyy):);
String ende = input.nextLine();
DateFormat df4 = new SimpleDateFormat(dd.mm.yyyy);
日期g = null;
try {
d = df.parse(ende);
} catch(ParseException f){
System.out.println(Unable to parse+ ende);
}
// DateFormat df5 = DateFormat.getDateInstance(DateFormat.LONG);
// String s4 = df5.format(d);


// String dauer = input.nextLine();
diffDays = 0;
try {
DateFormat dfa = new SimpleDateFormat(dd.mm.yyyy);
Date from = dfa.parse(beginn);
Date to = dfa.parse(ende);
long diffMillis = to.getTime() - from.getTime();
// diffDays = Math.round((double)diffMillis /(24. * 60. * 60. * 1000。));
diffDays = diffMillis /(1000 * 60 * 60 * 24);
System.out.println(diffDays);
} catch(ParseException ex){
ex.printStackTrace();
}


解决方案

您的格式错误/ p>

  DateFormat df = new SimpleDateFormat(dd.MM.yyyy); 

根据文档,MM指定月份,其中mm指定分钟。


I am new to Java and trying some practice exercises to get a better understanding of it.

I set myself the exercise to input different Strings, integers etc through the console and write the input to a textfile. All of this works fine.

Now I want to input into the textfile when the project started and when it will end. That works without any problem.

Now here is my problem. I want to calculate the time the project lasts from the beginning to the end in days.

Currently it only calculates the days, ignoring the month and the year.

For example: (dd.mm.yyyy)
Start: 07.07.2014   End: 15.07.2014  Output: 8 (almost correct :) )
Another example:
Start 07.07.2014   End: 03.08.2014  Output: -3 (not even close)

I just cannot figure out where my problem is.

Thanks for your help! Chris

System.out.print("Start project (dd.mm.yyyy): ");
String beginn = input.nextLine();
DateFormat df = new SimpleDateFormat("dd.mm.yyyy");
Date d = null;
try {
    d = df.parse(beginn);
} catch (ParseException e) {
    System.out.println("Unable to parse " + beginn);
}
DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
String s3 = df3.format(d);

System.out.print("End of project (dd.mm.yyyy): ");
String ende = input.nextLine();
DateFormat df4 = new SimpleDateFormat("dd.mm.yyyy");
Date g = null;
try {
    d = df.parse(ende);
} catch (ParseException f) {
    System.out.println("Unable to parse " + ende);
}
//DateFormat df5 = DateFormat.getDateInstance(DateFormat.LONG);
//String s4 = df5.format(d);


// String dauer = input.nextLine(); 
diffDays = 0;
try {
    DateFormat dfa = new SimpleDateFormat("dd.mm.yyyy");
    Date from = dfa.parse(beginn);
    Date to = dfa.parse(ende);
    long diffMillis = to.getTime() - from.getTime();
    //diffDays = Math.round( (double)diffMillis / (24. * 60.*60.*1000.) );
    diffDays = diffMillis / (1000 * 60 * 60 * 24);
    System.out.println(diffDays);
} catch (ParseException ex) {
    ex.printStackTrace();
}

解决方案

Your format is wrong.

DateFormat df = new SimpleDateFormat("dd.MM.yyyy");

According to the documentation, the MM specifies the months, where mm specifies minutes.

这篇关于在Java中计算两个日期之间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆