Reputation: 675
How to create a deep copy for a date object, for example birthDate of a student? how will copying a date object different from Name or age?
Here is an example for cloning I got from net.
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());
}
}
But is this a deep copying?
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();
Upvotes: 0
Views: 1806
Reputation: 340350
newEmployee.setHiredDate( oldEmployee.getHiredDate() ) ; // No need to copy/clone an immutable `LocalDate` object.
java.time.Instant
instead of java.util.Date
. java.time.LocalDate
for a date-only value.The Date
class is among the troublesome old date-time classes than are now legacy, supplanted by the java.time classes.
Instant
The modern equivalent to Date
is Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = Instant.now() ; // Current moment in UTC.
You can convert using new methods added to the old classes.
Instant instant = myJavaUtilDate.toInstant() ;
LocalDate
But you seem to be interested in a date-only value without a time-of-day and without a time zone. If so, use LocalDate
.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-dd-uuuu" ) ;
LocalDate ld = LocalDate.parse( "02-11-2017" , f ) ;
The java.time classes use immutable objects. So no need to copy or clone. Simply assign the same object.
The new cloned Employee
object gets a reference to the same LocalDate
object held by the original Employee
object.
By the way, you can serialize the java.time objects to standard ISO 8601 formatted strings by calling toString
.
String x = ld.toString() ; // Serialize object’s value as text in standard ISO 8601 format.
Instantiate by calling parse
.
LocalDate ld = LocalDate.parse( "2017-02-11" ) ; // Parse text to instantiate object.
The 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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The 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.
Upvotes: 2
Reputation:
Date original = new Date();
Date copy = new Date(original.getTime());
Upvotes: 2