How to Clone a Date Object

How to clone a Date object?

Use the Date object's getTime() method, which returns the number of milliseconds since 1 January 1970 00:00:00 UTC (epoch time):

var date = new Date();
var copiedDate = new Date(date.getTime());

In Safari 4, you can also write:

var date = new Date();
var copiedDate = new Date(date);

...but I'm not sure whether this works in other browsers. (It seems to work in IE8).

Is there a way to copy Date object into another date Object without using a reference?

You could use getTime() and passing it into the Date(time) constructor. This is only required because Date is mutable.

Date original = new Date();
Date copy = new Date(original.getTime());

If you're using Java 8 try using the new java.time API which uses immutable objects. So no need to copy/clone.

How to copy the month value from one Date object to another?

The problem is that the setMonth() method has an optional second parameter which is the day (DOCS). If you don't provide a value for the day it will automatically use the one of the date.

So, your A date is 12 February 2018 while your B date is 29 August 2019.

By doing b.setMonth(a.getMonth()); you are implicitly saying b.setMonth(1,29); (1 is a.getMonth() while 29 is the day form the b date).

So you are trying to set the date to the 29 February which isn't possible in 2019 and it shift the month by 1 to March (month 2).

If you do b.setMonth(a.getMonth() -1); you are setting it to the 29 January, which is possible so you get January as a month (month 1).

Javascript deep copy without breaking dates

This will deep copy the object, let me know if this resolves your issue:

let a = [{ date: new Date() }, { name: 'John'}];let b = a.map(k=>({...k}));
a[1]="PETER";console.log(a);console.log(b);

Changing the value of a copy (Date variable) also changes the value of the original (JS)

Please note:

var Uhrzeit = new Date(); Creates a new Date Instance which is an object.

The moment you assign var u = Uhrzeit, this in javascript means that u refers to the same object of date that Uhrzeit refers to.

So variables for objects holds the reference in memory where the object is stored. And by assigned a variable that references an object is simply going to pass that reference to the new variable.

The moment you change either u or Uhrzeit they both reference the same object and they both change the same object.

You must do this instead:

var u = new Date(Uhrzeit); this will create u with the value of Uhrzeit. But they both will now reference different objects in memory.

How can I clone a DateTime object in C#?

DateTime is a value type (struct)

This means that the following creates a copy:

DateTime toBeClonedDateTime = DateTime.Now;
DateTime cloned = toBeClonedDateTime;

You can also safely do things like:

var dateReference = new DateTime(2018, 7, 29);
for (var h = 0; h < 24; h++) {
for (var m = 0; m < 60; m++) {
var myDateTime = dateReference.AddHours(h).AddMinutes(m);
Console.WriteLine("Now at " + myDateTime.ToShortDateString() + " " + myDateTime.ToShortTimeString());
}
}

Note how in the last example myDateTime gets declared anew in each cycle; if dateReference had been affected by AddHours() or AddMinutes(), myDateTime would've wandered off really fast – but it doesn't, because dateReference stays put:

Now at 2018-07-29 0:00
Now at 2018-07-29 0:01
Now at 2018-07-29 0:02
Now at 2018-07-29 0:03
Now at 2018-07-29 0:04
Now at 2018-07-29 0:05
Now at 2018-07-29 0:06
Now at 2018-07-29 0:07
Now at 2018-07-29 0:08
Now at 2018-07-29 0:09
...
Now at 2018-07-29 23:55
Now at 2018-07-29 23:56
Now at 2018-07-29 23:57
Now at 2018-07-29 23:58
Now at 2018-07-29 23:59

Should you clone Date object before get in Java

The Java Date object is not thread-safe. Your approach of creating a new Date via the getPostDate() method will work to circumvent this issue for the most part. In fact it is considered best practice.

Now regardless of thread safety it is recommended that you recreate (clone or whatever) the Date object when returned from an entity. FindBugs will actually look for this issue in your POJO's. The reason is that Date is mutable unlike the String class for example.

Thus returning the live reference from an Entity bean allows the Date object within the Entity to be mutated without the use of the corresponding set method in the Entity. The entity will have no idea if its internal state has changed and if it needs to be persisted to the data store. This sort of defect occurs more often than you would like to admit.

Hope this answers your question.

Don't think I'm getting .clone()

The instance contains two values. You need to copy those values from the original to the new instance. It would be something more like:

public Object clone() throws CloneNotSupportedException {
// Perform a shallow copy
House houseClone = (House)super.clone();

// Deep copy on whenBuilt and area
houseClone.whenBuilt = (java.util.Date)(this.whenBuilt.clone());
houseClone.area = this.area;

return houseClone;
}

We know that a date is a pretty simple object, and you could just construct a new date with the time value from the old date, however if the clone method on Date works then it would be in general more proper to use it.

Doesn't writing whenBuilt.clone() do a shallow copy of the Date object
and not a deep copy?

Clone is for doing deep copy. Apparently the precise meaning depends on the class. It is supposed to given you another instance which has all the same values, but is a different instance. If you examine the code for the Date class, you find it only holds a single long value, so there is no real difference between shallow and deep for the Date class.

If you don't do clone on the Date object, then both House objects, the original and the cloned will point to the same instance of Date. The clone operation will give you a new instance of Date, with the same date value within it. Date objects have mutator methods, like setDate and you would not want a setDate on one house to change the date of the other house, so you have to clone them.

Java deep copy difference between a String and Date object

Date original = new Date();
Date copy = new Date(original.getTime());

java 8+ new API

Original link



Related Topics



Leave a reply



Submit