java 使用 Joda 时间减去 1 小时到 DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17000375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:39:41  来源:igfitidea点击:

minus 1 hour to DateTime using Joda Time

javaswingdatetimejodatime

提问by Maguzu

I just want to subtract 1 hour from a DateTimeI tried looking it up on Google and I found that there is a method called minus that takes a copy of the date and take a specific duration right here: http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#minus(long)

我只是想从DateTime我尝试在 Google 上查找它时减去 1 小时,我发现有一种方法叫做减号,它获取日期的副本并在此处获取特定的持续时间:http: //www.joda.org /joda-time/apidocs/org/joda/time/DateTime.html#minus(long)

But I don't know how to use it and I can't an find a example on the internet.

但我不知道如何使用它,我在互联网上找不到一个例子。

Here's my code:

这是我的代码:

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);

Sample Data:

样本数据:

    dateTime1: 08:00 AM
    dateTime2: 05:00 PM

the period will be 9 hours. but i want it to be 8 hrs only because i want to subtract the lunch break in my program.

时间为9小时。但我希望它是 8 小时,因为我想在我的程序中减去午休时间。

i tried it with this stupid code:

我用这个愚蠢的代码试过了:

dateTime1.minus(-1) 

I also tried parsing string1to double so I can subtract it by one.

我还尝试将其解析string1为加倍,这样我就可以将其减一。

double strindtoD = Integer.parseInt(string1);

I also tried making another DateTimeand use period to get the difference of the two time

我也尝试制作另一个DateTime并使用期间来获得两次时间的差异

String stringOneHour = ("01:00 AM");
DateTime dateTime3 = dtf.parseDateTime(stringOneHour.toString());
Period timePeriod = new Period(dateTime3, dateTime1);

回答by fge

Just use:

只需使用:

dateTime.minusHours(1)

This is documented in the API.

在 API 中有记录

Note that DateTimeobjects are immutable, so the operation alone has no effect. You need to assign the result of this method to a new object (or replace itself):

请注意,DateTime对象是不可变的,因此单独的操作没有任何效果。您需要将此方法的结果分配给新对象(或替换自身):

dateTime = dateTime.minusHours(1);

As to how to obtain a Periodout of the difference between two DateTimes, you must first go through an Interval:

至于如何Period从两个DateTimes之间的差异中获得一个,首先要经过一个Interval

Period period = new Interval(begin, end).toPeriod();

Linkto a SO post explaining why there is both Periodand Interval.

链接到解释为什么同时存在Period和的 SO 帖子Interval

Side note: Joda Time uses a LOT of indirections in its API; as such reading the Javadoc not only requires one to read the methods for one class, but also look at the list of inherited methods from all the inherited abstract classes/interfaces; for instance, a DateTimeis also a ReadableInstant. One you get used to it, though, it's a breeze.

旁注:Joda Time 在其 API 中使用了大量间接引用;因此,阅读 Javadoc 不仅需要阅读一个类的方法,还需要查看所有继承的抽象类/接口的继​​承方法列表;例如, aDateTime也是 a ReadableInstant。一个你习惯了,不过,这是一件轻而易举的事。

回答by Lance Reid

If you are using an older version of org.joda.time.DateTime then you can use minus(ReadablePeriod period) method like this

如果您使用的是 org.joda.time.DateTime 的旧版本,那么您可以使用这样的 minus(ReadablePeriod period) 方法

        Date date = LocalDate.now().minus(new Period(1, 0, 0, 0)).toDate();

where Period accepts int hours, int minutes, int seconds, int millis parameters

其中 Period 接受整数小时、整数分钟、整数秒、整数毫秒参数

回答by Sandeep

By using Calender class object you can use this method to subtract hours.

通过使用 Calender 类对象,您可以使用此方法来减去小时数。

cal.add(Calendar.HOUR_OF_DAY, -numberOfHours);

cal.add(Calendar.HOUR_OF_DAY, -numberOfHours);

and also here is complete example link

还有这里是完整的示例链接