我只想从DateTime
我尝试在 Google 上查找它的时间中减去 1 小时,我发现有一种名为减号的方法可以获取日期的副本并在此处获取特定的持续时间:http: //www.joda.org /joda-time/apidocs/org/joda/time/DateTime.html#minus(long)
但我不知道如何使用它,我无法在互联网上找到一个例子。
这是我的代码:
String string1 = (String) table_4.getValueAt(0, 1);
String string2= (String) table_4.getValueAt(0, 2);
DateTimeFormatter dtf = DateTimeFormat.forPattern("hh:mm a").withLocale(Locale.ENGLISH);
DateTime dateTime1 = dtf.parseDateTime(string1.toString());
DateTime dateTime2 = dtf.parseDateTime(string2.toString());
final String oldf = ("hh:mm a");
final String newf= ("hh.mm 0");
final String newf2= ("hh.mm a");
final String elapsedformat = ("hh.mm");
SimpleDateFormat format2 = new SimpleDateFormat(oldf);
SimpleDateFormat format2E = new SimpleDateFormat(newf);
Period timePeriod = new Period(dateTime1, dateTime2);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendHours().appendSuffix(".")
.appendMinutes().appendSuffix("")
.toFormatter();
String elapsed = formatter.print(timePeriod);
table_4.setValueAt(elapsed,0,3);
DecimalFormat df = new DecimalFormat("00.00");
System.out.println(dateTime1);
table_4.setValueAt("", 0, 4);
table_4.setValueAt("", 0, 5);
样本数据:
dateTime1: 08:00 AM
dateTime2: 05:00 PM
时间为 9 小时。但我只希望它是 8 小时,因为我想在我的程序中减去午休时间。
我用这个愚蠢的代码试了一下:
dateTime1.minus(-1)
我还尝试将解析string1
加倍,以便可以将其减一。
double strindtoD = Integer.parseInt(string1);
我还尝试制作另一个DateTime
并使用 period 来获得两个时间的差异
String stringOneHour = ("01:00 AM");
DateTime dateTime3 = dtf.parseDateTime(stringOneHour.toString());
Period timePeriod = new Period(dateTime3, dateTime1);