java – 减去1小时到使用Joda Time的DateTime
|
我只想从DateTime中减去1小时,我试着在Google上查找,我发现有一个方法称为minus,需要一个日期的副本,并在这里采取特定的时间: 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,3);
DecimalFormat df = new DecimalFormat("00.00");
System.out.println(dateTime1);
table_4.setValueAt("",4);
table_4.setValueAt("",5);
样品数据: dateTime1: 08:00 AM
dateTime2: 05:00 PM
期限为9小时.但是我想要8小时,因为我想在我的节目中减去午餐时间. 我试着用这个愚蠢的代码: dateTime1.minus(-1) 我也尝试解析string1加倍,所以我可以减1. double strindtoD = Integer.parseInt(string1); 我也尝试制作另一个DateTime和使用期来获得两个时间的差异 String stringOneHour = ("01:00 AM");
DateTime dateTime3 = dtf.parseDateTime(stringOneHour.toString());
Period timePeriod = new Period(dateTime3,dateTime1);
解决方法只需使用:dateTime.minusHours(1) 这是documented in the API. 请注意,DateTime对象是不可变的,因此单独的操作没有任何效果.您需要将此方法的结果分配给一个新对象(或替换自己): dateTime = dateTime.minusHours(1); 至于如何从两个DateTimes之间的差异中获取一个Period,您必须先经过一个Interval: Period period = new Interval(begin,end).toPeriod(); Link到一个SO职位,解释为什么有两个时期和间隔. 旁注:Joda Time在其API中使用了大量的定义;因此,读取Javadoc不仅需要读取一个类的方法,还要查看所有继承的抽象类/接口的继承方法的列表;例如,DateTime也是一个ReadableInstant.一个你习惯了它,但是,这是一个微风. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
