

{"id":13859,"date":"2018-04-18T12:08:58","date_gmt":"2018-04-18T12:08:58","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=13859"},"modified":"2026-05-28T14:24:34","modified_gmt":"2026-05-28T08:54:34","slug":"java-date-and-time","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/","title":{"rendered":"Java Date and Time &#8211; GregorianCalendar Class with Example"},"content":{"rendered":"<p>The definition of an efficient programming language depends on how well it can handle dates and Time. Java provides us with built-in classes like Date and Calendar to handle operations related to date and time. In this article, we will discuss at length how these classes and their methods can be implemented into a Java program.<\/p>\n<h3>The Date class in Java<\/h3>\n<p>The java.util package in Java contains many utilities that are very important for a program. One such class is the Date class. This class deals with operations related to date and time. This class implements the Cloneable, Serializable, and Comparable interfaces of Java.<\/p>\n<h3>Java Date class constructors<\/h3>\n<p>The Date class consists of six Java constructors, but only two of them are in use; the other four are deprecated.<\/p>\n<h4>1. Date()<\/h4>\n<p>This is the default constructor of the Date class. It is used to initialize the Date class objects with the present date and time.<\/p>\n<h4>2. Date(long millisecond)<\/h4>\n<p>It is a parameterized constructor of the Date class. The argument passed is in the form of milliseconds. It initializes the object with the total milliseconds that have passed since midnight, January 1, 1970.<\/p>\n<h3>Java Date class Methods<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>SL. No.\u00a0<\/b><\/td>\n<td><b>Method Name<\/b><\/td>\n<td><b>Description\u00a0<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">boolean after(Date date)<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method returns true if the called Date object contains a date that is later than the one passed through the parameter. Otherwise, it returns false.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<td><span style=\"font-weight: 400\">boolean before(Date date)<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method returns true if the called Date object contains a date that is earlier than the one passed through the parameter. Otherwise, it returns false.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">3<\/span><\/td>\n<td><span style=\"font-weight: 400\">Object clone()<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method clones the Date object that calls it.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">4<\/span><\/td>\n<td><span style=\"font-weight: 400\">int compareTo(Date date)<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method is used to compare the called object\u2019s date to the date passed through the parameter. It returns 0 if the dates are equal. It returns a negative value if the object\u2019s date is earlier and a positive value if the object\u2019s date is later.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">5<\/span><\/td>\n<td><span style=\"font-weight: 400\">int compareTo(Object obj)<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method works in the same way as the compareTo(Date date) method if the Object obj is an object of the Date class, else it throws an exception(ClassCastException).<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">6<\/span><\/td>\n<td><span style=\"font-weight: 400\">boolean equals(Object date)<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method performs an equality check. It returns true if the object&#8217;s date is the same as the date passed through the parameter. Otherwise, it returns false.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">7<\/span><\/td>\n<td><span style=\"font-weight: 400\">long getTime()<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method returns the number of milliseconds that have passed since January 1, 1970.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">8<\/span><\/td>\n<td><span style=\"font-weight: 400\">int hashCode()<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method creates a hash code and returns it for the object that calls it.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">9<\/span><\/td>\n<td><span style=\"font-weight: 400\">void setTime(long time)<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method sets the time to the time passed through the parameter and represents it in milliseconds of time that elapsed since January 1, 1970.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">10<\/span><\/td>\n<td><span style=\"font-weight: 400\">String toString()<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method changes the date to a string and returns it.\u00a0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Getting Current Date and Time<\/h3>\n<p>We will use two techniques to get the current date and time of our system.<br \/>\n1. Using the Date class<br \/>\n2. Using the Calendar class<\/p>\n<h4>1. Using the Date class in Java<\/h4>\n<p>In this method, we will create an object of the Date class and then call the toString() method using the object to get the present date and time of the system.<\/p>\n<p><strong>Code to get the current date and time using the Date Class:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.DateAndTime;\r\nimport java.util.Date;\r\npublic class CurrDateUsingDateClass\r\n{\r\n      public static void main(String args[]) {\r\n      Date date = new Date();\r\n      System.out.println(date.toString());\r\n   }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">Thu Aug 19 11:16:20 IST 2021<\/div>\n<h4>2. Using Calendar Class<\/h4>\n<p>We will use the getInstance() method of the Calendar class to create an instance, and use it to call the getTime() method to get the current time of the system.<\/p>\n<p><strong>Code to get the current date and time using the Calendar Class:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.DateAndTime;\r\nimport java.util.Calendar;\r\npublic class CurrDateUsingCalenderClass\r\n{\r\n    public static void main(String args[]) \r\n    {\r\n        Calendar current = Calendar.getInstance();\r\n        System.out.println(current.getTime());\r\n    }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code is:<\/strong><\/p>\n<div class=\"code-output\">Thu Aug 19 11:21:52 IST 2021<\/div>\n<h3>Comparison of Date<\/h3>\n<p><strong>We can compare two dates in three ways:<\/strong><\/p>\n<ul>\n<li>Using the getTime() function to get the number of milliseconds since January 1, 1970, for both dates, and then comparing the two values.<\/li>\n<li>Using the before(), after(), and equals() methods to compare the boolean values returned by the methods.<\/li>\n<li>There is also the compareTo() method, which can be used to compare two dates. It returns 0, negative, and positive values, depending on the dates compared.<\/li>\n<\/ul>\n<h3>Formatting Date Using the SimpleDateFormat class<\/h3>\n<p>We can format dates using the concrete class called SimpleDateFormat. We can start by choosing any user-defined pattern for date formatting.<\/p>\n<p><strong>Code to understand the use of SimpleDateFormat:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.DateAndTime;\r\nimport java.util.*;\r\nimport java.text.*;\r\npublic class DateFormatting\r\n{\r\n    public static void main(String args[]) \r\n   {\r\n      Date CurrDate = new Date( );\r\n      SimpleDateFormat formatDate = new SimpleDateFormat (\"E yyyy.MM.dd 'at' hh:mm:ss a zzz\");\r\n      System.out.println(\"Current Date(Formatted): \" + formatDate.format(CurrDate));\r\n   }\r\n}\r\n\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">Current Date(Formatted): Thu 2021.08.19 at 11:58:26 AM IST<\/div>\n<h4>SimpleDateFormat formatting codes<\/h4>\n<p>In the above program, we used codes like E, yyyy, etc. These codes are all specific pattern codes defined inside the SimpleDateFormat. Let us discuss all the codes through the table below.<\/p>\n<table style=\"height: 1317px\" width=\"781\">\n<tbody>\n<tr>\n<td><b>Character<\/b><\/td>\n<td><b>Meaning<\/b><\/td>\n<td><b>Example<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">G<\/span><\/td>\n<td><span style=\"font-weight: 400\">Era designator<\/span><\/td>\n<td><span style=\"font-weight: 400\">AD, BC<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">y<\/span><\/td>\n<td><span style=\"font-weight: 400\">Year in digits<\/span><\/td>\n<td><span style=\"font-weight: 400\">2021<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">M<\/span><\/td>\n<td><span style=\"font-weight: 400\">Month in Year<\/span><\/td>\n<td><span style=\"font-weight: 400\">August or 08<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">d<\/span><\/td>\n<td><span style=\"font-weight: 400\">Day in Month<\/span><\/td>\n<td><span style=\"font-weight: 400\">19<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Hour in A.M.\/P.M.(12-hour clock)<\/span><\/td>\n<td><span style=\"font-weight: 400\">10<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">H<\/span><\/td>\n<td><span style=\"font-weight: 400\">Hour in day(24 hour clock[0-23])<\/span><\/td>\n<td><span style=\"font-weight: 400\">13<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">m<\/span><\/td>\n<td><span style=\"font-weight: 400\">Minutes in an hour<\/span><\/td>\n<td><span style=\"font-weight: 400\">05<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">s<\/span><\/td>\n<td><span style=\"font-weight: 400\">Second in a minute<\/span><\/td>\n<td><span style=\"font-weight: 400\">57<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">S<\/span><\/td>\n<td><span style=\"font-weight: 400\">Millisecond<\/span><\/td>\n<td><span style=\"font-weight: 400\">100<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">E<\/span><\/td>\n<td><span style=\"font-weight: 400\">Day of the Week<\/span><\/td>\n<td><span style=\"font-weight: 400\">Thursday<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">D<\/span><\/td>\n<td><span style=\"font-weight: 400\">Day in year<\/span><\/td>\n<td><span style=\"font-weight: 400\">225<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">F<\/span><\/td>\n<td><span style=\"font-weight: 400\">Day of the week in the month<\/span><\/td>\n<td><span style=\"font-weight: 400\">3(Third Wednesday in August)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">w<\/span><\/td>\n<td><span style=\"font-weight: 400\">Week in year<\/span><\/td>\n<td><span style=\"font-weight: 400\">35<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">W<\/span><\/td>\n<td><span style=\"font-weight: 400\">Week in the month<\/span><\/td>\n<td><span style=\"font-weight: 400\">3<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">a<\/span><\/td>\n<td><span style=\"font-weight: 400\">A.M.\/P.M. marker<\/span><\/td>\n<td><span style=\"font-weight: 400\">P.M.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">k<\/span><\/td>\n<td><span style=\"font-weight: 400\">Hour in day(1-24)<\/span><\/td>\n<td><span style=\"font-weight: 400\">24<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">K<\/span><\/td>\n<td><span style=\"font-weight: 400\">Hour in A.M.\/P.M.(0-11)<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">z<\/span><\/td>\n<td><span style=\"font-weight: 400\">Time Zone<\/span><\/td>\n<td><span style=\"font-weight: 400\">Indian Standard Time<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u2018<\/span><\/td>\n<td><span style=\"font-weight: 400\">Escape for text<\/span><\/td>\n<td><span style=\"font-weight: 400\">Delimiter<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u201c<\/span><\/td>\n<td><span style=\"font-weight: 400\">Single Quote<\/span><\/td>\n<td><span style=\"font-weight: 400\">\u2018<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Characters to convert Java Date and Time<\/h4>\n<table style=\"height: 2005px\" width=\"799\">\n<tbody>\n<tr>\n<td><b>Character<\/b><\/td>\n<td><b>Meaning<\/b><\/td>\n<td><b>Example<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">c<\/span><\/td>\n<td><span style=\"font-weight: 400\">Complete date and time<\/span><\/td>\n<td><span style=\"font-weight: 400\">Thu August 19 13:50:45 IST 2021<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">F<\/span><\/td>\n<td><span style=\"font-weight: 400\">ISO 8601 date format<\/span><\/td>\n<td><span style=\"font-weight: 400\">2021-08-19<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">D<\/span><\/td>\n<td><span style=\"font-weight: 400\">United States formatted date (month\/day\/year)<\/span><\/td>\n<td><span style=\"font-weight: 400\">08\/19\/2021<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">T<\/span><\/td>\n<td><span style=\"font-weight: 400\">24- hour time<\/span><\/td>\n<td><span style=\"font-weight: 400\">13:50:45<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">r<\/span><\/td>\n<td><span style=\"font-weight: 400\">12-hour time<\/span><\/td>\n<td><span style=\"font-weight: 400\">01:50:45 pm<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">R<\/span><\/td>\n<td><span style=\"font-weight: 400\">24-hour time without seconds<\/span><\/td>\n<td><span style=\"font-weight: 400\">13:45<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Y<\/span><\/td>\n<td><span style=\"font-weight: 400\">Four-digit year<\/span><\/td>\n<td><span style=\"font-weight: 400\">2021<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">y<\/span><\/td>\n<td><span style=\"font-weight: 400\">Last two digits of the year<\/span><\/td>\n<td><span style=\"font-weight: 400\">21<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">C<\/span><\/td>\n<td><span style=\"font-weight: 400\">First two digits of the year<\/span><\/td>\n<td><span style=\"font-weight: 400\">20<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">B<\/span><\/td>\n<td><span style=\"font-weight: 400\">Full name of the month<\/span><\/td>\n<td><span style=\"font-weight: 400\">August<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">b<\/span><\/td>\n<td><span style=\"font-weight: 400\">Abbreviation of the month name<\/span><\/td>\n<td><span style=\"font-weight: 400\">Aug<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">m<\/span><\/td>\n<td><span style=\"font-weight: 400\">Two-digit month number<\/span><\/td>\n<td><span style=\"font-weight: 400\">08<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">d<\/span><\/td>\n<td><span style=\"font-weight: 400\">Two-digit day of the month(With leading Zero)<\/span><\/td>\n<td><span style=\"font-weight: 400\">19<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">e<\/span><\/td>\n<td><span style=\"font-weight: 400\">Two-digit day of the month(Without leading zero)<\/span><\/td>\n<td><span style=\"font-weight: 400\">9<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">A<\/span><\/td>\n<td><span style=\"font-weight: 400\">Full form of the day name<\/span><\/td>\n<td><span style=\"font-weight: 400\">Thursday<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">a<\/span><\/td>\n<td><span style=\"font-weight: 400\">Abbreviated form of the day name<\/span><\/td>\n<td><span style=\"font-weight: 400\">Thu<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">j<\/span><\/td>\n<td><span style=\"font-weight: 400\">Three-digit day of the year with leading zero<\/span><\/td>\n<td><span style=\"font-weight: 400\">015<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">H<\/span><\/td>\n<td><span style=\"font-weight: 400\">Two-digit hour with leading zero(00-23)<\/span><\/td>\n<td><span style=\"font-weight: 400\">14<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">k<\/span><\/td>\n<td><span style=\"font-weight: 400\">Two-digit hour without leading zero(0-23)<\/span><\/td>\n<td><span style=\"font-weight: 400\">14<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">I<\/span><\/td>\n<td><span style=\"font-weight: 400\">Two-digit hour with leading zeros (01-12)<\/span><\/td>\n<td><span style=\"font-weight: 400\">02<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">l<\/span><\/td>\n<td><span style=\"font-weight: 400\">Two-digit hour without leading zeros (1-12)<\/span><\/td>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">M<\/span><\/td>\n<td><span style=\"font-weight: 400\">Two-digit minutes with leading zeroes<\/span><\/td>\n<td><span style=\"font-weight: 400\">03<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">S<\/span><\/td>\n<td><span style=\"font-weight: 400\">Two-digit seconds with leading zeroes<\/span><\/td>\n<td><span style=\"font-weight: 400\">07<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">L<\/span><\/td>\n<td><span style=\"font-weight: 400\">Three-digit milliseconds with leading zeroes<\/span><\/td>\n<td><span style=\"font-weight: 400\">077<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">N<\/span><\/td>\n<td><span style=\"font-weight: 400\">Nine-digit nanoseconds with leading zeroes<\/span><\/td>\n<td><span style=\"font-weight: 400\">000002234<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">P<\/span><\/td>\n<td><span style=\"font-weight: 400\">Uppercase AM or PM<\/span><\/td>\n<td><span style=\"font-weight: 400\">PM<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">p<\/span><\/td>\n<td><span style=\"font-weight: 400\">Lowercase am or pm<\/span><\/td>\n<td><span style=\"font-weight: 400\">pm<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">z<\/span><\/td>\n<td><span style=\"font-weight: 400\">RFC 822 numeric offset from GMT<\/span><\/td>\n<td><span style=\"font-weight: 400\">+0530<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Z<\/span><\/td>\n<td><span style=\"font-weight: 400\">Time Zone\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">IST<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">s<\/span><\/td>\n<td><span style=\"font-weight: 400\">Seconds since 1970-01-01 00:00:00 GMT<\/span><\/td>\n<td><span style=\"font-weight: 400\">1629362389<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Q<\/span><\/td>\n<td><span style=\"font-weight: 400\">Milliseconds since 1970-01-01 00:00:00 GMT<\/span><\/td>\n<td><span style=\"font-weight: 400\">1629362311015<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Date formatting Using printf<\/h3>\n<p>The above characters can be used easily to format a date using the printf method. We just have to give a two-letter format starting with t and any of the above characters.<\/p>\n<p><strong>Code to understand the implementation of printf:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.DateAndTime;\r\nimport java.util.Date;\r\npublic class DateFormattingusingPrintf\r\n{\r\n    public static void main(String args[]) \r\n    {\r\n      Date date = new Date();\r\n      String str = String.format(\"Current Date\/Time of the system : %tc\", date );\r\n      System.out.printf(str);\r\n   }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">Current Date\/Time of the system : Thu Aug 19 14:13:20 IST 2021<\/div>\n<p>We can apply multiple formatting at the same time as well. We just have to give % before every character and terminate with $<\/p>\n<p><strong>Code to implement multiple formatting using printf:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.DateAndTime;\r\nimport java.util.Date;\r\npublic class DateFormattingusingPrintf\r\n{\r\n    public static void main(String args[]) \r\n    {\r\n      Date date_of_system = new Date();\r\n      System.out.printf(\"%1$s %2$tB %2$td, %2$tY\", \"Current date of the system:\", date_of_system);\r\n   }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">Current date of the system: August 19, 2021<\/div>\n<p>We can also use &lt; flag to indicate that the same argument as the preceding should be used again.<\/p>\n<p><strong>Code to understand flag in printf:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.DateAndTime;\r\nimport java.util.Date;\r\npublic class DateFormattingusingPrintf\r\n{\r\n    public static void main(String args[]) \r\n    {\r\n      Date date = new Date();\r\n      System.out.printf(\"%s %tB %&lt;te, %&lt;tY\", \"Current date of the system:\", date);\r\n   }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">Current date of the system: August 19, 2021<\/div>\n<h3>Parsing strings into dates<\/h3>\n<p>The SimpleDateFormat has a method called parse() which tries to parse a string according to the format stored in the given SimpleDateFormat object.<\/p>\n<p><strong>Code to understand the parse method:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.DateAndTime;\r\nimport java.util.*;\r\nimport java.text.*;\r\npublic class parseDate\r\n{\r\n     public static void main(String args[]) {\r\n      SimpleDateFormat formatofDate = new SimpleDateFormat (\"yyyy-MM-dd\"); \r\n      String input = args.length == 0 ? \"2021-08-19\" : args[0]; \r\n      System.out.print(input + \" is Parsed as \"); \r\n      Date t;\r\n      try {\r\n         t = formatofDate.parse(input); \r\n         System.out.println(t); \r\n      } catch (ParseException e) { \r\n         System.out.println(\"The string is unparseable \" + formatofDate); \r\n      }\r\n   }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">2021-08-19 is Parsed as Thu Aug 19 00:00:00 IST 2021<\/div>\n<h3>Making the program sleep for a while<\/h3>\n<p>We can make a program sleep for any period of time from one millisecond up to the lifetime of our computer.<\/p>\n<p><strong>Code to make our system sleep for 6 seconds:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.DateAndTime;\r\nimport java.util.*;\r\npublic class Sleeping\r\n{\r\n   public static void main(String args[]) {\r\n      try { \r\n         System.out.println(new Date( ) + \"\\n\"); \r\n         Thread.sleep(10*60*10); \r\n         System.out.println(new Date( ) + \"\\n\"); \r\n      } catch (Exception e) {\r\n         System.out.println(\"Got an exception!\"); \r\n      }\r\n   }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">\n<p>Thu Aug 19 14:35:45 IST 2021<\/p>\n<p>Thu Aug 19 14:35:51 IST 2021<\/p>\n<\/div>\n<p>Now let us try to calculate the time for which our program slept.<\/p>\n<p><strong>Code to calculate the time for which our program sleeps:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.DateAndTime;\r\nimport java.util.*;\r\npublic class Sleeping\r\n{\r\n     public static void main(String args[]) {\r\n      try {\r\n         long start = System.currentTimeMillis( );\r\n         System.out.println(new Date( ) + \"\\n\");\r\n         Thread.sleep(10*60*10);\r\n         System.out.println(new Date( ) + \"\\n\");\r\n         long end = System.currentTimeMillis( );\r\n         long diff = end - start;\r\n         System.out.println(\"Difference is : \" + diff);\r\n      } catch (Exception e) {\r\n         System.out.println(\"Got an exception!\");\r\n      }\r\n   }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">\n<p>Thu Aug 19 14:38:29 IST 2021<\/p>\n<p>Thu Aug 19 14:38:35 IST 2021<\/p>\n<p>Difference is : 6002<\/p>\n<\/div>\n<h3>GregorianCalendar Class<\/h3>\n<p>Another concrete implementation of the calendar class is the GregorianCalendar class. It implements the normal Gregorian Calendar. We can instantiate the GregorianCalendar class using the getInstance() method. It can define two fields: AD(Anno Domini)and BC(Before Christ).<\/p>\n<p>There are several constructors in the GregorianCalendar class:<br \/>\n<strong>1. GregorianCalendar():<\/strong> It is the default constructor of the GregorianCalendar class. The default constructor constructs a default calendar using the current time in the default time zone with the default location.<\/p>\n<p><strong>2. GregorianCalendar(int<\/strong> <strong>year, int month, int date):<\/strong> It is a parameterized constructor of the GregorianCalendar class that constructs a GregorianCalendar with the mentioned date in the default time zone with the default location.<\/p>\n<p><strong>3. GregorianCalendar(int<\/strong> <strong>year, int month, int date, int hour, int minute):<\/strong> It is a parameterized constructor of the GregorianCalendar class that constructs a GregorianCalendar with the mentioned date and time in the default time zone with the default location.<\/p>\n<p><strong>4. GregorianCalendar(int<\/strong> <strong>year, int month, int date, int hour, int minute, int second):<\/strong> It is a parameterized constructor of the GregorianCalendar class that constructs a GregorianCalendar with the given date and time in the default time zone with the default location.<\/p>\n<p><strong>5. GregorianCalendar(Locale location):<\/strong> It is a parameterized constructor of the GregorianCalendar class that constructs a GregorianCalendar based on the current time in the default time zone with the given location.<\/p>\n<p><strong>6. GregorianCalendar(TimeZone<\/strong> <strong>zone):<\/strong> It is a parameterized constructor of the GregorianCalendar class that constructs a GregorianCalendar based on the current time in the given time zone with the default location.<\/p>\n<p><strong>7. GregorianCalendar(TimeZone<\/strong> <strong>zone, Locale location):<\/strong> It is a parameterized constructor of the GregorianCalendar class that constructs a GregorianCalendar based on the current time in the given time zone with the given location.<\/p>\n<h4>GregorianCalendar Methods:<\/h4>\n<table>\n<tbody>\n<tr>\n<td><b>SL. No.<\/b><\/td>\n<td><b>Syntax<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">void add(int field, int amount)<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method adds the given amount of time to the specified field, depending on calendar rules.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<td><span style=\"font-weight: 400\">protected void computeFields()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can convert UTC as milliseconds to time field values.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">3<\/span><\/td>\n<td><span style=\"font-weight: 400\">protected void computeTime()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can override Calendar Converts time field values to UTC as milliseconds.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">4<\/span><\/td>\n<td><span style=\"font-weight: 400\">boolean equals(Object obj)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can compare the GregorianCalendar to an object reference.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">5<\/span><\/td>\n<td><span style=\"font-weight: 400\">int get(int field)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can get the value for a given time field.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">6<\/span><\/td>\n<td><span style=\"font-weight: 400\">int getActualMaximum(int field)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can get the maximum value that this field could have, given the current date.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">7<\/span><\/td>\n<td><span style=\"font-weight: 400\">int getGreatestMinimum(int field)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can get the highest minimum value for the given field if it varies.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">8<\/span><\/td>\n<td><span style=\"font-weight: 400\">int getActualMinimum(int field)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can get the minimum value that this field could have, given the current date.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">9<\/span><\/td>\n<td><span style=\"font-weight: 400\">Date getGregorianChange()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can get the Gregorian Calendar change date.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">10<\/span><\/td>\n<td><span style=\"font-weight: 400\">int getLeastMaximum(int field)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can get the lowest maximum value for the given field if it varies<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">11<\/span><\/td>\n<td><span style=\"font-weight: 400\">int getMaximum(int field)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can get the maximum value for the given field.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">12<\/span><\/td>\n<td><span style=\"font-weight: 400\">Date getTime()<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method gets this Calendar&#8217;s current time.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">13<\/span><\/td>\n<td><span style=\"font-weight: 400\">long getTimeInMillis()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can get this Calendar&#8217;s current time as a long variable.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">14<\/span><\/td>\n<td><span style=\"font-weight: 400\">TimeZone getTimeZone()<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method gets the time zone.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">15<\/span><\/td>\n<td><span style=\"font-weight: 400\">int getMinimum(int field)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can get the minimum value for the given field.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">16<\/span><\/td>\n<td><span style=\"font-weight: 400\">int hashCode()<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method overrides the hashCode<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">17<\/span><\/td>\n<td><span style=\"font-weight: 400\">boolean isLeapYear(int year)<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method helps us determine if the given year is a leap year.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">18<\/span><\/td>\n<td><span style=\"font-weight: 400\">void roll(int field, boolean up)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can add or subtract (up\/down) a single unit of time on the given time field without changing larger fields.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">19<\/span><\/td>\n<td><span style=\"font-weight: 400\">void set(int field, int value)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can set the time field with the given value.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">20<\/span><\/td>\n<td><span style=\"font-weight: 400\">void set(int year, int month, int date)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can set the values for the fields year, month, and date.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">21<\/span><\/td>\n<td><span style=\"font-weight: 400\">void set(int year, int month, int date, int hour, int minute)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can set the values for the fields year, month, date, hour, and minute.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">22<\/span><\/td>\n<td><span style=\"font-weight: 400\">void set(int year, int month, int date, int hour, int minute, int second)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can set the values for the fields year, month, date, hour, minute, and second.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">23<\/span><\/td>\n<td><span style=\"font-weight: 400\">Void setGregorianChange(Date date)<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method sets the GregorianCalendar change date.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">24<\/span><\/td>\n<td><span style=\"font-weight: 400\">void setTime(Date date)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can set the Calendar&#8217;s current time with the given Date.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">25<\/span><\/td>\n<td><span style=\"font-weight: 400\">Void setTimeInMillis(long millis)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can set the Calendar&#8217;s current time from the given long value.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">26<\/span><\/td>\n<td><span style=\"font-weight: 400\">void setTimeZone(TimeZone value)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Using this method, we can set the time zone with the given time zone value.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">27<\/span><\/td>\n<td><span style=\"font-weight: 400\">String toString()<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method can return a string representation of the calendar.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Code to understand the implementation of GregorianCalendar:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.DateAndTime;\r\nimport java.util.*;\r\npublic class GregoriancalendarImplementation\r\n{\r\n    public static void main(String args[]) {\r\n      String Month[] = {\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \r\n         \"October\", \"November\", \"December\"};\r\n      int year;\r\n      GregorianCalendar calendar = new GregorianCalendar();\r\n      System.out.print(\"Date: \");\r\n      System.out.print(Month[calendar.get(Calendar.MONTH)]);\r\n      System.out.print(\" \" + calendar.get(Calendar.DATE) + \" \");\r\n      System.out.println(year = calendar.get(Calendar.YEAR));\r\n      System.out.print(\"Time: \");\r\n      System.out.print(calendar.get(Calendar.HOUR) + \":\");\r\n      System.out.print(calendar.get(Calendar.MINUTE) + \":\");\r\n      System.out.println(calendar.get(Calendar.SECOND));\r\n      if(calendar.isLeapYear(year)) {\r\n         System.out.println(\"The current year is a leap year\");\r\n      }else {\r\n         System.out.println(\"The current year is not a leap year\");\r\n      }\r\n   }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">Date: August 19 2021<br \/>\nTime: 3:53:13<br \/>\nThe current year is not a leap year<\/div>\n<h3>Use of the Java Date &amp; Time class<\/h3>\n<p>The Date and Time class in Java is mostly useful when the application works by tracking the time. E-commerce websites are used the most when it comes to providing sales over a particular period of time. Almost all the work of the industries is based on the scheduled task. Integrating the current date and time into the application results in tracking the changes at every point in time to benefit the business solution.<\/p>\n<h3>Conclusion<\/h3>\n<p>In this article, we saw how Java can be used to handle various operations using dates and times. This is a very important concept, as many software require proper handling of date and time to be implemented smoothly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The definition of an efficient programming language depends on how well it can handle dates and Time. Java provides us with built-in classes like Date and Calendar to handle operations related to date and&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":108849,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[5160,7453,7457,7458,7459,7462,7724,7738],"class_list":["post-13859","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-gregoriancalendar-class-in-java","tag-java-date-and-time","tag-java-date-class-example","tag-java-date-comparison","tag-java-date-format","tag-java-dateformat","tag-java-time-example","tag-java-util-date"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Date and Time - GregorianCalendar Class with Example - DataFlair<\/title>\n<meta name=\"description\" content=\"Java Date and Time shows different formatting of date &amp; time in Java with Class. Learn working of GregorianCalendar Class in Java.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/java-date-and-time\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Date and Time - GregorianCalendar Class with Example - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Java Date and Time shows different formatting of date &amp; time in Java with Class. Learn working of GregorianCalendar Class in Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-date-and-time\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-18T12:08:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-28T08:54:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/java-date-and-time.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Date and Time - GregorianCalendar Class with Example - DataFlair","description":"Java Date and Time shows different formatting of date & time in Java with Class. Learn working of GregorianCalendar Class in Java.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/","og_locale":"en_US","og_type":"article","og_title":"Java Date and Time - GregorianCalendar Class with Example - DataFlair","og_description":"Java Date and Time shows different formatting of date & time in Java with Class. Learn working of GregorianCalendar Class in Java.","og_url":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-04-18T12:08:58+00:00","article_modified_time":"2026-05-28T08:54:34+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/java-date-and-time.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Java Date and Time &#8211; GregorianCalendar Class with Example","datePublished":"2018-04-18T12:08:58+00:00","dateModified":"2026-05-28T08:54:34+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/"},"wordCount":2284,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/java-date-and-time.webp","keywords":["GregorianCalendar Class in Java","Java Date and Time","Java date Class example","Java Date Comparison","java date format","java dateformat","java time example","java util date"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-date-and-time\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/","url":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/","name":"Java Date and Time - GregorianCalendar Class with Example - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/java-date-and-time.webp","datePublished":"2018-04-18T12:08:58+00:00","dateModified":"2026-05-28T08:54:34+00:00","description":"Java Date and Time shows different formatting of date & time in Java with Class. Learn working of GregorianCalendar Class in Java.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-date-and-time\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/java-date-and-time.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/java-date-and-time.webp","width":1200,"height":628,"caption":"java date and time"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-date-and-time\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Java Date and Time &#8211; GregorianCalendar Class with Example"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=13859"}],"version-history":[{"count":19,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13859\/revisions"}],"predecessor-version":[{"id":148450,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13859\/revisions\/148450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/108849"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=13859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=13859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=13859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}