我在 Joda-Time 的两个日期之间得到错误的时间
I am getting wrong time between two dates in Joda-Time
无论我做什么,我都会得到不正确的结果。谁能给我一些建议:)。
这是我的程序代码,它负责获取两个日期之间的时间。
我得到了一个结果,但问题是它不正确,我很难找到问题所在。
我的项目中必须有 Joda 库。
if (timerightnow.isSelected()) {
DateFormat getdate = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Date getdate1 = new Date();
String datenow = getdate1.toString();
String monthnow = datenow.substring(4, 7);
String daynow = datenow.substring(8,10);
String hournow = datenow.substring(11,19);
String yearnow = datenow.substring(25, 29);
if (monthnow.equalsIgnoreCase("jan"))
monthnow = "01";
if (monthnow.equalsIgnoreCase("feb"))
monthnow = "02";
if (monthnow.equalsIgnoreCase("mar"))
monthnow = "03";
if (monthnow.equalsIgnoreCase("apr"))
monthnow = "04";
if (monthnow.equalsIgnoreCase("may"))
monthnow = "05";
if (monthnow.equalsIgnoreCase("jun"))
monthnow = "06";
if (monthnow.equalsIgnoreCase("jul"))
monthnow = "07";
if (monthnow.equalsIgnoreCase("agu"))
monthnow = "08";
if (monthnow.equalsIgnoreCase("sep"))
monthnow = "09";
if (monthnow.equalsIgnoreCase("oct"))
monthnow = "10";
if (monthnow.equalsIgnoreCase("nov"))
monthnow = "11";
if (monthnow.equalsIgnoreCase("dec"))
monthnow = "12";
String timenow = monthnow + "/" + daynow + "/" + yearnow + " " + hournow;
String timetotext = timeto.getText();
SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date datestart = null;
Date dateend = null;
try {
datestart = date.parse(timenow);
dateend = date.parse(timetotext);
DateTime datestartdaytime = new DateTime(datestart);
DateTime dateenddaytime = new DateTime(dateend);
JOptionPane.showMessageDialog(null, datestartdaytime + "\n" + dateenddaytime);
int monthoutput = Months.monthsBetween(datestartdaytime, dateenddaytime).getMonths();
int daysoutput = Days.daysBetween(datestartdaytime, datestartdaytime).getDays() % 30;
int hoursoutput = Hours.hoursBetween(datestartdaytime, datestartdaytime).getHours() % 24;
int minutsoutput = Minutes.minutesBetween(datestartdaytime, dateenddaytime).getMinutes() % 60;
int secondsoutput = Seconds.secondsBetween(datestartdaytime, dateenddaytime).getSeconds() % 60;
answer.setText(monthoutput + "Months" + daysoutput + "Days" + hoursoutput + "Hours" + minutsoutput + "Minutes" + secondsoutput + "Seconds");
非常感谢:)
你的错误在这两行:
int daysoutput = Days.daysBetween(datestartdaytime, datestartdaytime).getDays() % 30;
int hoursoutput = Hours.hoursBetween(datestartdaytime, datestartdaytime).getHours() % 24;
您打算使用 dateenddaytime
作为第二个参数:
int daysoutput = Days.daysBetween(datestartdaytime, dateenddaytime).getDays() % 30;
int hoursoutput = Hours.hoursBetween(datestartdaytime, dateenddaytime).getHours() % 24;
无论我做什么,我都会得到不正确的结果。谁能给我一些建议:)。
这是我的程序代码,它负责获取两个日期之间的时间。
我得到了一个结果,但问题是它不正确,我很难找到问题所在。
我的项目中必须有 Joda 库。
if (timerightnow.isSelected()) {
DateFormat getdate = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Date getdate1 = new Date();
String datenow = getdate1.toString();
String monthnow = datenow.substring(4, 7);
String daynow = datenow.substring(8,10);
String hournow = datenow.substring(11,19);
String yearnow = datenow.substring(25, 29);
if (monthnow.equalsIgnoreCase("jan"))
monthnow = "01";
if (monthnow.equalsIgnoreCase("feb"))
monthnow = "02";
if (monthnow.equalsIgnoreCase("mar"))
monthnow = "03";
if (monthnow.equalsIgnoreCase("apr"))
monthnow = "04";
if (monthnow.equalsIgnoreCase("may"))
monthnow = "05";
if (monthnow.equalsIgnoreCase("jun"))
monthnow = "06";
if (monthnow.equalsIgnoreCase("jul"))
monthnow = "07";
if (monthnow.equalsIgnoreCase("agu"))
monthnow = "08";
if (monthnow.equalsIgnoreCase("sep"))
monthnow = "09";
if (monthnow.equalsIgnoreCase("oct"))
monthnow = "10";
if (monthnow.equalsIgnoreCase("nov"))
monthnow = "11";
if (monthnow.equalsIgnoreCase("dec"))
monthnow = "12";
String timenow = monthnow + "/" + daynow + "/" + yearnow + " " + hournow;
String timetotext = timeto.getText();
SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date datestart = null;
Date dateend = null;
try {
datestart = date.parse(timenow);
dateend = date.parse(timetotext);
DateTime datestartdaytime = new DateTime(datestart);
DateTime dateenddaytime = new DateTime(dateend);
JOptionPane.showMessageDialog(null, datestartdaytime + "\n" + dateenddaytime);
int monthoutput = Months.monthsBetween(datestartdaytime, dateenddaytime).getMonths();
int daysoutput = Days.daysBetween(datestartdaytime, datestartdaytime).getDays() % 30;
int hoursoutput = Hours.hoursBetween(datestartdaytime, datestartdaytime).getHours() % 24;
int minutsoutput = Minutes.minutesBetween(datestartdaytime, dateenddaytime).getMinutes() % 60;
int secondsoutput = Seconds.secondsBetween(datestartdaytime, dateenddaytime).getSeconds() % 60;
answer.setText(monthoutput + "Months" + daysoutput + "Days" + hoursoutput + "Hours" + minutsoutput + "Minutes" + secondsoutput + "Seconds");
非常感谢:)
你的错误在这两行:
int daysoutput = Days.daysBetween(datestartdaytime, datestartdaytime).getDays() % 30;
int hoursoutput = Hours.hoursBetween(datestartdaytime, datestartdaytime).getHours() % 24;
您打算使用 dateenddaytime
作为第二个参数:
int daysoutput = Days.daysBetween(datestartdaytime, dateenddaytime).getDays() % 30;
int hoursoutput = Hours.hoursBetween(datestartdaytime, dateenddaytime).getHours() % 24;