Hello Joda time is not giving correct results for me
本问题已经有最佳答案,请猛点这里访问。
无论我做什么都会得到错误的结果。有人能给我一些建议吗:)
我以前发布过这个问题,但是我没有得到任何答案,我很高兴地想要一个答案。
这是我的程序代码,负责获取两个日期之间的时间。
我得到了一个结果,但问题是它是不正确的,我很难找到问题。
我必须在我的项目中加入Joda图书馆。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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 +" " + 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"); |
非常感谢:
我真的不明白你想达到什么目的。但是您应该明确地不要自己进行解析逻辑,因为它已经存在于jodatime中。
这样可以解决解析问题:
1 2 | DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yy HH:mm:ss"); DateTime dt = formatter.parseDateTime(string); |
如果这不能帮你解决问题,请详细说明你想要什么。
希望它有帮助!