- My gut tells me, that clone() will be faster.
- Why not try it with a quick benchmark [*]?
- Consider using just the long value of
date.getTime()
, if you don't have to do calendar calculations.
[*]
private static final int N = 100000;
public static void main(final String[] args) throws Exception {
final Date date = new Date();
{
final long start = System.currentTimeMillis();
for (int i = 0; i < N; i ++) {
final Date date2 = (Date) date.clone();
}
final long end = System.currentTimeMillis();
System.out.println("Clone: " + (end - start) + " ms");
}
{
final long start = System.currentTimeMillis();
for (int i = 0; i < N; i ++) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final Date date2 = cal.getTime();
}
final long end = System.currentTimeMillis();
System.out.println("Caldendar.setTime: " + (end - start) + " ms");
}
}
Results:
Clone: 13 ms
Caldendar.setTime: 317 ms
PS I'm not sure, if you really need a Calendar
, or a Date
, so feel free to modify the test...
(In response to the comment: To improve test accuracy, you can also run the tests individually, increase the value of N, ...)