Creating a temporary copy of a Calendar object in Java(在 Java 中创建日历对象的临时副本)
问题描述
我需要弄清楚如何创建一个临时 Calendar 对象(已经存在的永久"日历的副本),以便我可以操作该副本:tempCal.add(单位,值).我需要保持原来的日历对象不变,所以我真的不想直接在它上面调用add(unit, value).
I need to figure out how to create a temporary Calendar object (a copy of a "permanent" calendar that already exists) so that I can manipulate the copy: tempCal.add(unit, value). I need to keep the original calendar object unchanged, so I really don't want to call add(unit, value) on it directly.
由于我创建副本的任何尝试都没有真正奏效,我目前的丑陋技巧是调用 permanentCal.add(unit, value),显示所需的结果,然后调用 permanentCal.add (unit, -value)——这看起来很不酷.
Since none of my attempts at creating a copy actually worked, my current ugly hack is to call permanentCal.add(unit, value), display the desired results, then call permanentCal.add (unit, -value) — which just seems, uncool.
推荐答案
java.util.Calendar 有一个 clone 方法,你可以使用它.里面的所有数据都是由原语组成的,所以你不会遇到麻烦.
java.util.Calendar has a clone method, you could use that. All data in it is made of primitives, so you will not run into troubles.
看看这些答案:
- 日历的防御性副本
- 克隆公历日历的最快方法?
这篇关于在 Java 中创建日历对象的临时副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中创建日历对象的临时副本
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
