Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between new Date() and Calendar date

What is the difference between the two dates below in practice?

  1. Date date = new Date();

  2. Date date = Calendar.getInstance().getTime();

What I understand is that new Date() is a UTC/GMT based date while calendar's getTime() is based on TimeZone & System time. Am I right? Do I miss something still?

Moreover, if my above understanding is correct, can I say that the end results of the following two functions are exactly the same ?

1.

public String getDate1(){
   SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
   //I set the time zone & pass the new Date()
   sdf.setTimeZone(TimeZone.getDefault()); 
   return sdf.format(new Date());
}

2.

public String getDate2(){
  SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
  //I didn't set the time zone because I think calendar instance will handle timezone change
  return sdf.format(Calendar.getInstance().getTime());
}

I appreciate if you could point out where I understand wrongly & explain to me clearly. Because I feel this thing is confused to me. Thanks!

like image 645
Leem.fin Avatar asked Dec 19 '13 22:12

Leem.fin


People also ask

What is the use of new date () in Java?

Date() : Creates date object representing current date and time. Date(long milliseconds) : Creates a date object for the given milliseconds since January 1, 1970, 00:00:00 GMT.

What is Calendar date in Java?

Calendar class in Java is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable, Serializable, Cloneable interfaces.

What is the difference between Calendar and Gregorian Calendar in Java?

The major difference between GregorianCalendar and Calendar classes are that the Calendar Class being an abstract class cannot be instantiated. So an object of the Calendar Class is initialized as: Calendar cal = Calendar. getInstance();

How do I compare Calendar dates in Java?

In Java, two dates can be compared using the compareTo() method of Comparable interface. This method returns '0' if both the dates are equal, it returns a value "greater than 0" if date1 is after date2 and it returns a value "less than 0" if date1 is before date2.


1 Answers

Practical info about Java Calendar and Date

If you want to operate with different dates in your Java program you will use Java Calendar class.

I will try to give you some overview of not widely known facts about Java Calendar and Date classes, working code examples, which you can try right away.

The basic information about Calendar class is provided by Java API. The Calendar class is about days, months and years. One could ask: is not Date class about the same? Not exactly...

What is difference between Java Date and Calendar classes?

The difference between Date and Calendar is that Date class operates with specific instant in time and Calendar operates with difference between two dates. The Calendar class gives you possibility for converting between a specific instant in time and a set of calendar fields such as HOUR, YEAR, MONTH, DAY_OF_MONTH. You can also manipulate with the calendar fields, for example getting the date of your grandmother birthday :).

I would like to point some things about Calendar and Date which you should know and which are not obvious...

Leap seconds.

Years, months, dates and hours are in "normal" range like:

A year y - 1900. A month from 0 to 11 A date (day of month) from 1 to 31 in the usual manner. calendar leap seconds An hour 0 to 23. A minute from 0 to 59 in the usual manner. But, attention!! A second is represented by an integer from 0 to 61. Looks strange - 61 second, but do not forget about leap second. About once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second.

Lenient fields.

Another funny feature is lenient and non-lenient fields in calendar. What is that? Example:

32 January 2006. Actually if you set your calendar lenient it will be 1 February 2006 and no problem for your program :). If it is non-lenient ArrayIndexOutOfBoundsException exception will be thrown.

Another question is 00:00 end or beginning of day? Is 00:00 A.M. or P.M.? Are midnight and noon A.M. or P.M?

Answer: 23:59 is the last minute of the day and 00:00 is the first minute of the next day. Midnight belongs to "am", and noon belongs to "pm", so on the same day, 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm.

And probably last question: what is epoch? and why this Epoch since January 1, 1970 00:00:00.000 GMT.

Actually it is Unix time, or POSIX time, is a system for describing points in time: it is the number of seconds after 00:00:00 UTC, January 1, 1970.

Wait, one question more!

"If we use the time which is counted since Epoch, how can I know which years had leap seconds and which not?"

Answer: To make life easier leap seconds are not counted. Java Date class takes actual time from OS and most of modern computers can not use leap seconds, their's internal clocks are not so precised. That's why periodical time synchronization is required.

like image 62
jimagic Avatar answered Sep 19 '22 09:09

jimagic