Java String 和 Date 对象之间的深拷贝区别

Java deep copy difference between a String and Date object

如何为日期对象创建深拷贝,例如学生的出生日期?如何复制与姓名或年龄不同的日期对象?

这是我从网上得到的克隆示例。

import java.util.Date;
public class Clone
{
    public static void main(String[] args)
    {
        Date d1 = new Date(90,10,5);
        Object d2=d1.clone();
        System.out.println("Original Date:" +d1.toString());
        System.out.println("Cloned Date:" +d2.toString());
    }
}

但这是深拷贝吗?

OUTPUT Original Date:Mon Jan 05 00:00:00 IST 2018 Cloned Date:Mon Jan 05 00:00:00 IST 2018 –

adding additional Info...

so how can I put the code inside my class ?

// insideCloneable class
/overriding clone() method to create a deep copy of an object. 
protected Object clone()throws CloneNotSupportedException{ 
        Employee employee = (Employee) super.clone();
        return employee;  
        }

//implementing  class - main method 
Employee employee1 = new Employee(01,"John","02-11-2017");
        Employee employee2 = null;
employee2=(Employee)employee1.clone();
Date original = new Date();
Date copy = new Date(original.getTime());

java 8+ new API

tl;博士

newEmployee.setHiredDate( oldEmployee.getHiredDate() ) ;  // No need to copy/clone an immutable `LocalDate` object.
  • 使用 java.time.Instant 而不是 java.util.Date
  • 使用 java.time.LocalDate 作为仅限日期的值。
  • 不可变,重新使用 java.time 对象而不是 copy/clone.

java.time

Date class 是麻烦的旧日期时间 class 之一,现在已被 java.time[= 取代114=] class 等。

Instant

Date 的现代版本是 InstantInstant class represents a moment on the timeline in UTC with a resolution of nanoseconds(最多九 (9) 位小数)。

Instant instant = Instant.now() ;  // Current moment in UTC.

您可以使用添加到旧 classes 的新方法进行转换。

Instant instant = myJavaUtilDate.toInstant() ;

LocalDate

但是您似乎对没有时间和时区的仅日期值感兴趣。如果是这样,请使用 LocalDate.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-dd-uuuu" ) ;
LocalDate ld = LocalDate.parse( "02-11-2017" , f ) ;

不可变对象可以重用而不是cloned/copied

java.time class 使用 immutable objects因此无需复制或克隆。 只需分配相同的对象即可。

新克隆的 Employee 对象引用原始 Employee 对象持有的相同 LocalDate 对象。

字符串

顺便说一句,您可以通过调用toStringjava.time对象序列化为标准ISO 8601格式的字符串。

String x = ld.toString() ;                        // Serialize object’s value as text in standard ISO 8601 format.

调用parse实例化。

LocalDate ld = LocalDate.parse( "2017-02-11" ) ;  // Parse text to instantiate object.

关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

从哪里获得java.time classes?

  • Java SE 8, Java SE 9,及以后
    • 内置。
    • 标准 Java API 的一部分,带有捆绑实施。
    • Java 9 添加了一些小功能和修复。
  • Java SE 6 and Java SE 7
  • Android
    • Android java.time classes.
    • 捆绑实施的更高版本
    • 对于较早的 Android,ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.