如何使用 Joda-Time 在 Java 中设置时间属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29380681/
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-08-11 07:53:49  来源:igfitidea点击:

How to set Time property in Java using Joda-Time

javadatetimejodatime

提问by vkrams

I want to set the hour, minute and seconds in Joda-Time. But when I set it's not changing the property.

我想在 Joda-Time 中设置小时、分钟和秒。但是当我设置它不会改变属性。

Here is my code:

这是我的代码:

import org.joda.time.DateTime;
public class JodaAkbar 
{
 public static void main(String args[])
 { 
    DateTime dt = new DateTime();
    System.out.println("Before:"+dt);
    dt.hourOfDay().setCopy(5);
    dt.minuteOfDay().setCopy(20);
    dt.secondOfDay().setCopy(0);
    System.out.println("After:"+dt);
 }
}

Here is the output.

这是输出。

Before:2015-04-01T11:01:38.277+11:00
After:2015-04-01T11:01:38.277+11:00

I am getting the same output. What's happening wrong here?

我得到相同的输出。这里发生了什么问题?

EDIT:

编辑:

Basically, I want to do something similar as shown in the below code. As the below code doesn't work properly for 24 hour format, I switched to Joda-Time.

基本上,我想做一些类似于下面的代码所示的事情。由于以下代码在 24 小时格式下无法正常工作,我切换到 Joda-Time。

 Calendar cal = Calendar.getInstance();
 cal.set(Calendar.HOUR, 13);
 cal.set(Calendar.MINUTE, 25);
 cal.set(Calendar.SECOND, 0);

采纳答案by John Kugelman

Joda-Time objects are immutable. The word "copy" in setCopyis telling you that it doesn't set these fields directly, but instead creates a copy of the DateTimewith that field modified.

Joda-Time 对象是不可变的。“复制”setCopy一词告诉您,它不会直接设置这些字段,而是创建DateTime修改该字段的的副本。

A quick fix is:

一个快速的解决方法是:

dt = dt.hourOfDay().setCopy(5);
dt = dt.minuteOfHour().setCopy(20);
dt = dt.secondOfMinute().setCopy(0);

A more fluent approach would be to chain several withmethods together:

更流畅的方法是将几种with方法链接在一起:

DateTime dt = new DateTime()
    .withHourOfDay(5)
    .withMinuteOfHour(20)
    .withSecondOfMinute(0);

Or do it all with a single withTimecall:

或者只需一个withTime电话即可完成所有操作:

DateTime dt = new DateTime().withTime(5, 20, 0, 0);

By the way, Java 8 introduces a new java.timepackage which was inspired by Joda-Time. The Joda-Time web site recommends, "From Java SE 8 onwards, users are asked to migrate to java.time(JSR-310)."

顺便说一下,Java 8 引入了一个java.time受 Joda-Time 启发的新包。Joda-Time 网站建议,“从 Java SE 8 开始,要求用户迁移到java.time(JSR-310)。”

回答by Edwin Buck

Look into immutable data structures. The modifiers on a JodaTime object don't really modify the object, but return different instances constructed from the original object with the desired field set as requested.

研究不可变数据结构。JodaTime 对象上的修饰符并没有真正修改对象,而是返回从原始对象构造的不同实例,并根据请求设置所需的字段。

So, in effect, you're constructing a lot of items, and not assigning them to anything, so they're getting garbage collected. Finally, you're printing out the same (immutable) item twice.

因此,实际上,您正在构建大量项目,而不是将它们分配给任何东西,因此它们正在被垃圾收集。最后,您将两次打印出相同的(不可变的)项目。