复制 joda LocalTime 的直接方法是什么?

发布于 2024-11-07 11:21:27 字数 911 浏览 1 评论 0原文

我想要一个新实例,它是一个副本。我可以从整数实例化,但似乎应该有一种更直接的方法。我还可以使用某种方法,例如 copy = original.minus(zero) ,但这也是间接的。

接受 Java 对象参数(我使用原始 LocalTime)的 LocalTime 构造函数不起作用。我想它只是不支持它。

        LocalTime start = new LocalTime(9, 0, 0);
        LocalTime stop = new LocalTime(17, 0, 0);

        //LocalTime time = start.minusSeconds(0);  // GOOD VERSION
        LocalTime time = new LocalTime(start);     // THE BAD VERSION

        assert time == start: "does not work";

        // EXTRANEOUS STUFF TO JUSTIFY COPYING AN IMMUTABLE, FOLLOWS...
        while (time.compareTo(stop) <= 0)
        {
            //method(time, new LocalTime(9, 0, 0), stop); // MESSY
            method(time, start, stop);                    // NICER
            time = time.plusMinutes(1);
        }

我还尝试了 copy = new LocalTime(original.getLocalMillis()) 但我无权访问 getLocalMillis 因为它受到保护。

I want a new instance that is a copy. I could instantiate from integers but it seems there should be a more direct way. I could also use some sort of approach like copy = original.minus(zero) but that is also indirect.

The LocalTime constructor that accepts a Java Object argument (for which I used the original LocalTime) does not work. I guess it just does not support it.

        LocalTime start = new LocalTime(9, 0, 0);
        LocalTime stop = new LocalTime(17, 0, 0);

        //LocalTime time = start.minusSeconds(0);  // GOOD VERSION
        LocalTime time = new LocalTime(start);     // THE BAD VERSION

        assert time == start: "does not work";

        // EXTRANEOUS STUFF TO JUSTIFY COPYING AN IMMUTABLE, FOLLOWS...
        while (time.compareTo(stop) <= 0)
        {
            //method(time, new LocalTime(9, 0, 0), stop); // MESSY
            method(time, start, stop);                    // NICER
            time = time.plusMinutes(1);
        }

I also tried copy = new LocalTime(original.getLocalMillis()) but I do not have access to getLocalMillis as it is protected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2024-11-14 11:21:27

LocalTime 是不可变的,因此没有必要保留两个具有相同值的实例。它们可以共享(甚至跨线程)。突变方法(例如加/减)将返回一个新值,因此当您需要修改的值时,您可以“按需”创建副本。

LocalTime start = new LocalTime(9, 0, 0);
LocalTime stop = new LocalTime(17, 0, 0);
LocalTime time = start;     // Just use the reference

while (time.compareTo(stop) <= 0)
{
    method(time, start, stop);
    time = time.plusMinutes(1);
}

LocalTime is immutable, so there is no point holding on to 2 instances with the same value. They can be shared (even across threads). The mutation methods, e.g. plus/minus will return a new value, so you can create your copy "on demand" when you need the modified value.

LocalTime start = new LocalTime(9, 0, 0);
LocalTime stop = new LocalTime(17, 0, 0);
LocalTime time = start;     // Just use the reference

while (time.compareTo(stop) <= 0)
{
    method(time, start, stop);
    time = time.plusMinutes(1);
}
故乡的云 2024-11-14 11:21:27

这对我来说效果很好:

LocalTime t1 = new LocalTime();
try {
   // Sleep for a bit just to make sure the current system time moves on
   Thread.sleep(5000);
} catch (InterruptedException e) { }
LocalTime t2 = new LocalTime(t1);
assertEquals(t1, t2);

请注意倒数第二行 - 我认为这就是您正在寻找的内容。 t2 获取与 t1 相同的纪元以来的时间(以毫秒为单位)。

那么,当您说复制构造函数(我在上面使用过“OK”)“不起作用”时,您到底是什么意思?

This works just fine for me:

LocalTime t1 = new LocalTime();
try {
   // Sleep for a bit just to make sure the current system time moves on
   Thread.sleep(5000);
} catch (InterruptedException e) { }
LocalTime t2 = new LocalTime(t1);
assertEquals(t1, t2);

Note the second-to-last line - I think that's what you're looking for. t2 gets the same time in milliseconds since the epoch as t1.

So what do you mean exactly when you say that the copy constructor (which I've used OK above) "does not work"?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文