I am creating Date object in java.
First way: By using Calender
Date today = Calendar.getInstance().getTime();
Second way:
with Date class
Date today1 = new Date();
Which one is most effective way here?
Since Java 8 you should go for LocalDateTime or LocalDate:
LocalDateTime timePoint = LocalDateTime.now(
); // The current date and time
LocalDate.of(2012, Month.DECEMBER, 12); // from values
LocalDate theDate = timePoint.toLocalDate(); // or
theDate = LocalDate.now(); //
Fromt the documentation:
[For example, the existing classes (such as java.util.Date and SimpleDateFormatter) aren’t thread-safe, leading to potential concurrency issues for users—not something the average developer would expect to deal with when writing date-handling code.
Some of the date and time classes also exhibit quite poor API design. For example, years in java.util.Date start at 1900, months start at 1, and days start at 0—not very intuitive.]1
You can use this too! Date newDate = new GregorianCalendar(year, month, day).getTime();
Within your options, I will prefer the first alternative.
java.util.Dateis extremely cheap (just a wrapper around a long-primitive). But using it in formatting (for example inSimpleDateFormat) has some performance impact due to lack of thread-safety, enforced conversion using complex gregorian calendar rules and/or use of synchronization overhead.