{"id":119,"date":"2019-01-19T12:47:25","date_gmt":"2019-01-19T12:47:25","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=119"},"modified":"2020-08-06T09:54:15","modified_gmt":"2020-08-06T09:54:15","slug":"19-java-date-and-time-part-2","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/","title":{"rendered":"Java &#8211; Date and Time &#8211; Part 2"},"content":{"rendered":"<p>So we would just continue with Date and Time. You can review <a href=\"https:\/\/www.kindsonthegenius.com\/java\/java-date-and-time-part-1\/\">Date and Time Part 1<\/a> if you have not.<\/p>\n<p>&nbsp;<\/p>\n<p>In this tutorial, we would cover the following<\/p>\n<ol>\n<li><a href=\"#t1\">Formatting Date Using print()<\/a><\/li>\n<li><a href=\"#t2\">Date and Time Conversion Codes<\/a><\/li>\n<li><a href=\"#t3\">Converting Dates to Strings<\/a><\/li>\n<li><a href=\"#t4\">Sleeping for Some Time<\/a><\/li>\n<li><a href=\"#t5\">How to Get Elapsed Time<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Formatting Date Using printf()<\/strong><\/h4>\n<p>Similar to the way you format string using printf, you can also format dates using printf. You can review <a href=\"https:\/\/www.kindsonthegenius.com\/java\/java-the-string-class\/\">Java Strings and Formatting.<\/a><\/p>\n<p>Unlike formatting string, you use a two-letter format specifier to format dates. The first letter is t, then followed by a second letter indicating the format. So instead of %c, we have %tc. Instead of %d, we have %td. And so on.<\/p>\n<p>For example, run the code in Listing 1.0 below.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.text.SimpleDateFormat<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Date<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">DateDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888;\">\/\/Create a new date<\/span>\r\n\t\t\tDate date <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Date<span style=\"color: #333333;\">();<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888;\">\/\/Format the date using String.format<\/span>\r\n\t\t\tString strDate <span style=\"color: #333333;\">=<\/span> String<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">format<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Today is %tc\"<\/span><span style=\"color: #333333;\">,<\/span> date<span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888;\">\/\/Print the date using printf()<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printf<\/span><span style=\"color: #333333;\">(<\/span>strDate<span style=\"color: #333333;\">);<\/span>\t\t\t\t\t\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.0: Formating date using String.Fomat and printf<\/p>\n<p>&nbsp;<\/p>\n<p>This code in Listing 1.0 uses %tc\u00a0 where c represents &#8216;complete date and time&#8217;. You can replace the %tc with %tD or %tF\u00a0 or %tr. All these gives the date in other formats. You can find the complete list of formats in the next section.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Date and Time Conversion Codes<\/strong><\/h4>\n<p>I would recommend you spend some time with the table below.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Character Code<\/th>\n<th>Brief description<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td>c<\/td>\n<td>Complete date and time<\/td>\n<td>Tue May 04 06:31:52 CDT 2019<\/td>\n<\/tr>\n<tr>\n<td>F<\/td>\n<td>ISO 8601 date format<\/td>\n<td>2018-02-09<\/td>\n<\/tr>\n<tr>\n<td>D<\/td>\n<td>U.S. formatted date (month\/day\/year)<\/td>\n<td>02\/04\/2018<\/td>\n<\/tr>\n<tr>\n<td>T<\/td>\n<td>24-hour time format<\/td>\n<td>13:05:19<\/td>\n<\/tr>\n<tr>\n<td>r<\/td>\n<td>12-hour time format<\/td>\n<td>05:05:19 am<\/td>\n<\/tr>\n<tr>\n<td>R<\/td>\n<td>24-hour time, no seconds<\/td>\n<td>15:06<\/td>\n<\/tr>\n<tr>\n<td>Y<\/td>\n<td>4-digit year (with leading zeroes)<\/td>\n<td>2015<\/td>\n<\/tr>\n<tr>\n<td>y<\/td>\n<td>Last 2 digits of the year (with leading zeroes)<\/td>\n<td>05<\/td>\n<\/tr>\n<tr>\n<td>C<\/td>\n<td>First 2 digits of the year (with leading zeroes)<\/td>\n<td>25<\/td>\n<\/tr>\n<tr>\n<td>B<\/td>\n<td>Full month name<\/td>\n<td>December<\/td>\n<\/tr>\n<tr>\n<td>b<\/td>\n<td>Abbreviated name of month<\/td>\n<td>Jul<\/td>\n<\/tr>\n<tr>\n<td>m<\/td>\n<td>2-digit month (with leading zeroes)<\/td>\n<td>04<\/td>\n<\/tr>\n<tr>\n<td>d<\/td>\n<td>2-digit day (with leading zeroes)<\/td>\n<td>01<\/td>\n<\/tr>\n<tr>\n<td>e<\/td>\n<td>2-digit day (without leading zeroes)<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>A<\/td>\n<td>Full name of weekday<\/td>\n<td>Wednesday<\/td>\n<\/tr>\n<tr>\n<td>a<\/td>\n<td>Abbreviated name of weekday<\/td>\n<td>Fri<\/td>\n<\/tr>\n<tr>\n<td>j<\/td>\n<td>3-digit day of year (with leading zeroes)<\/td>\n<td>057<\/td>\n<\/tr>\n<tr>\n<td>H<\/td>\n<td>2-digit hour (with leading zeroes), between 00 and 23<\/td>\n<td>11<\/td>\n<\/tr>\n<tr>\n<td>k<\/td>\n<td>2-digit hour (without leading zeroes), between 0 and 23<\/td>\n<td>19<\/td>\n<\/tr>\n<tr>\n<td>I<\/td>\n<td>2-digit hour (with leading zeroes), between 01 and 12<\/td>\n<td>04<\/td>\n<\/tr>\n<tr>\n<td>l<\/td>\n<td>2-digit hour (without leading zeroes), between 1 and 12<\/td>\n<td>8<\/td>\n<\/tr>\n<tr>\n<td>M<\/td>\n<td>2-digit minutes (with leading zeroes)<\/td>\n<td>03<\/td>\n<\/tr>\n<tr>\n<td>S<\/td>\n<td>2-digit seconds (with leading zeroes)<\/td>\n<td>18<\/td>\n<\/tr>\n<tr>\n<td>L<\/td>\n<td>3-digit milliseconds (with leading zeroes)<\/td>\n<td>038<\/td>\n<\/tr>\n<tr>\n<td>N<\/td>\n<td>9-digit nanoseconds (with leading zeroes)<\/td>\n<td>038000000<\/td>\n<\/tr>\n<tr>\n<td>P<\/td>\n<td>Uppercase morning or afternoon marker<\/td>\n<td>AM<\/td>\n<\/tr>\n<tr>\n<td>p<\/td>\n<td>Lowercase morning or afternoon marker<\/td>\n<td>am<\/td>\n<\/tr>\n<tr>\n<td>z<\/td>\n<td>RFC 822 numeric offset from GMT<\/td>\n<td>-0600<\/td>\n<\/tr>\n<tr>\n<td>Z<\/td>\n<td>Time zone<\/td>\n<td>PST<\/td>\n<\/tr>\n<tr>\n<td>s<\/td>\n<td>Seconds since 1970-01-01 00:00:00 GMT<\/td>\n<td>2078861332<\/td>\n<\/tr>\n<tr>\n<td>Q<\/td>\n<td>Milliseconds since 1970-01-01 00:00:00 GMT<\/td>\n<td>20788613328374<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.0: Date and Time Conversion Codes<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Converting Strings to Dates<\/strong><\/h4>\n<p>You can convert dates to string using the parsed() method. This method is provided by the SimpleDateFormat class. So the parse() method attempts to convert a string data to date data.<\/p>\n<p>The code in Listing 1.1 demonstrates this.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.text.ParseException<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.text.SimpleDateFormat<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Date<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Scanner<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">DateDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\r\n\t\t<span style=\"color: #888888;\">\/\/Create a new SimpleDateFormat<\/span>\r\n\t\tSimpleDateFormat ft <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> SimpleDateFormat<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"yyyy-MM-dd\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #888888;\">\/\/Get a new date from input<\/span>\r\n\t\tScanner scanner <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Scanner<span style=\"color: #333333;\">(<\/span>System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">in<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">print<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Please enter a date: \"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\tString strDate <span style=\"color: #333333;\">=<\/span> scanner<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">nextLine<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">try<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t<span style=\"color: #888888;\">\/\/Parse the string into a date<\/span>\r\n\t\t\tDate dtDate <span style=\"color: #333333;\">=<\/span> ft<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">parse<\/span><span style=\"color: #333333;\">(<\/span>strDate<span style=\"color: #333333;\">);<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"You entered \"<\/span> <span style=\"color: #333333;\">+<\/span> dtDate<span style=\"color: #333333;\">);<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">catch<\/span> <span style=\"color: #333333;\">(<\/span>ParseException e<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Unable to convert to date\"<\/span> <span style=\"color: #333333;\">+<\/span> ft<span style=\"color: #333333;\">);<\/span>\r\n\t\t\te<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printStackTrace<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span>\t\t\t\t\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.1: Converting a String to a Data<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Sleeping for some Time<\/strong><\/h4>\n<p>You will be able to delay for a period of time. This time is specified in milliseconds. But to do this, you need to use a the sleep method from the Thread class<\/p>\n<p>For example, the code below makes the program sleep for 2000 miliseconds (2 seconds)<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Date<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">DateSleepDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String args<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">try<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t<span style=\"color: #888888;\">\/\/Print current date<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">print<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">new<\/span> Date<span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"\\n\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888;\">\/\/Sleep for 2 seconds<\/span>\r\n\t\t\tThread<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">sleep<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">2000<\/span><span style=\"color: #333333;\">);<\/span>\t\t\t\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888;\">\/\/Print current date after the sleep<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">print<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">new<\/span> Date<span style=\"color: #333333;\">());<\/span>\r\n\t\t\t\r\n\t\t<span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">catch<\/span> <span style=\"color: #333333;\">(<\/span>InterruptedException e<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Error occured: \"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\te<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printStackTrace<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span>\t\t\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}\r\n<\/span><\/pre>\n<p>Listing 1.2: Sleeping for a time<\/p>\n<p>&nbsp;<\/p>\n<p>If you run the code above, you will have this result:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Sat Jan <span style=\"color: #0000dd; font-weight: bold;\">19<\/span> <span style=\"color: #0000dd; font-weight: bold;\">13<\/span><span style=\"color: #333333;\">:<\/span><span style=\"color: #0000dd; font-weight: bold;\">20<\/span><span style=\"color: #333333;\">:<\/span><span style=\"color: #0000dd; font-weight: bold;\">19<\/span> CET <span style=\"color: #0000dd; font-weight: bold;\">2019<\/span>\r\nSat Jan <span style=\"color: #0000dd; font-weight: bold;\">19<\/span> <span style=\"color: #0000dd; font-weight: bold;\">13<\/span><span style=\"color: #333333;\">:<\/span><span style=\"color: #0000dd; font-weight: bold;\">20<\/span><span style=\"color: #333333;\">:<\/span><span style=\"color: #0000dd; font-weight: bold;\">22<\/span> CET <span style=\"color: #0000dd; font-weight: bold;\">2019<\/span>\r\n<\/pre>\n<p>Note the the sleep code is surrounded by a try\/catch block. This is because its possible for error to occur when working with threads<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. How to get Elapsed Time<\/strong><\/h4>\n<p>Although the above code gives use the elapsed time, there is method used to get the real milliseconds that actually elapsed.<\/p>\n<p>This method is currentTimeMIllis. So instead of using new Date(), we use System.currentTimeMillis().<\/p>\n<p>The code below illustrates this.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Date<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ElapsedTimeDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">long<\/span> begin <span style=\"color: #333333;\">=<\/span> System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">currentTimeMillis<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">new<\/span> Date<span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"\\n\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">try<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\tThread<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">sleep<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">5000<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">new<\/span> Date<span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"\\n\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #333399; font-weight: bold;\">long<\/span> end <span style=\"color: #333333;\">=<\/span> System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">currentTimeMillis<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\t\t<span style=\"color: #333399; font-weight: bold;\">long<\/span> elapsed <span style=\"color: #333333;\">=<\/span> end <span style=\"color: #333333;\">-<\/span> begin<span style=\"color: #333333;\">;<\/span>\r\n\t\t\t\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Elapsed time is \"<\/span> <span style=\"color: #333333;\">+<\/span> elapsed<span style=\"color: #333333;\">);<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">catch<\/span> <span style=\"color: #333333;\">(<\/span>InterruptedException e<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Error occured: \"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\te<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printStackTrace<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\r\n\t\t\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.3: Measuring elapsed time.<\/p>\n<p>&nbsp;<\/p>\n<p>If you run the code in Listing 1.3 above, you will get the output:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Sat Jan <span style=\"color: #0000dd; font-weight: bold;\">19<\/span> <span style=\"color: #0000dd; font-weight: bold;\">13<\/span><span style=\"color: #333333;\">:<\/span><span style=\"color: #0000dd; font-weight: bold;\">39<\/span><span style=\"color: #333333;\">:<\/span><span style=\"color: #0000dd; font-weight: bold;\">53<\/span> CET <span style=\"color: #0000dd; font-weight: bold;\">2019<\/span>\r\n\r\nSat Jan <span style=\"color: #0000dd; font-weight: bold;\">19<\/span> <span style=\"color: #0000dd; font-weight: bold;\">13<\/span><span style=\"color: #333333;\">:<\/span><span style=\"color: #0000dd; font-weight: bold;\">39<\/span><span style=\"color: #333333;\">:<\/span><span style=\"color: #0000dd; font-weight: bold;\">58<\/span> CET <span style=\"color: #0000dd; font-weight: bold;\">2019<\/span>\r\n\r\nElapsed time is <span style=\"color: #0000dd; font-weight: bold;\">5028<\/span>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So we would just continue with Date and Time. You can review Date and Time Part 1 if you have not. &nbsp; In this tutorial, &hellip; <\/p>\n","protected":false},"author":395,"featured_media":120,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java - Date and Time - Part 2 - Java Tutorials<\/title>\n<meta name=\"description\" content=\"In this Tutorial we would learn how to work with Dates and Time. We see how to measure elapsed time using the Thread class.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/\" \/>\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 - Part 2 - Java Tutorials\" \/>\n<meta property=\"og:description\" content=\"In this Tutorial we would learn how to work with Dates and Time. We see how to measure elapsed time using the Thread class.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Java Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-19T12:47:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-06T09:54:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Date-and-Time-in-Java-Part-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"950\" \/>\n\t<meta property=\"og:image:height\" content=\"518\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Java &#8211; Date and Time &#8211; Part 2\",\"datePublished\":\"2019-01-19T12:47:25+00:00\",\"dateModified\":\"2020-08-06T09:54:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/\"},\"wordCount\":631,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Date-and-Time-in-Java-Part-2.jpg\",\"articleSection\":[\"Java Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/\",\"name\":\"Java - Date and Time - Part 2 - Java Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Date-and-Time-in-Java-Part-2.jpg\",\"datePublished\":\"2019-01-19T12:47:25+00:00\",\"dateModified\":\"2020-08-06T09:54:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"In this Tutorial we would learn how to work with Dates and Time. We see how to measure elapsed time using the Thread class.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Date-and-Time-in-Java-Part-2.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Date-and-Time-in-Java-Part-2.jpg\",\"width\":950,\"height\":518,\"caption\":\"Date and time in Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/19-java-date-and-time-part-2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java &#8211; Date and Time &#8211; Part 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\",\"name\":\"Java Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java - Date and Time - Part 2 - Java Tutorials","description":"In this Tutorial we would learn how to work with Dates and Time. We see how to measure elapsed time using the Thread class.","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:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/","og_locale":"en_US","og_type":"article","og_title":"Java - Date and Time - Part 2 - Java Tutorials","og_description":"In this Tutorial we would learn how to work with Dates and Time. We see how to measure elapsed time using the Thread class.","og_url":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/","og_site_name":"Java Tutorials","article_published_time":"2019-01-19T12:47:25+00:00","article_modified_time":"2020-08-06T09:54:15+00:00","og_image":[{"width":950,"height":518,"url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Date-and-Time-in-Java-Part-2.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Java &#8211; Date and Time &#8211; Part 2","datePublished":"2019-01-19T12:47:25+00:00","dateModified":"2020-08-06T09:54:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/"},"wordCount":631,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Date-and-Time-in-Java-Part-2.jpg","articleSection":["Java Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/","url":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/","name":"Java - Date and Time - Part 2 - Java Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Date-and-Time-in-Java-Part-2.jpg","datePublished":"2019-01-19T12:47:25+00:00","dateModified":"2020-08-06T09:54:15+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"In this Tutorial we would learn how to work with Dates and Time. We see how to measure elapsed time using the Thread class.","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Date-and-Time-in-Java-Part-2.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Date-and-Time-in-Java-Part-2.jpg","width":950,"height":518,"caption":"Date and time in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/java\/19-java-date-and-time-part-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/java\/"},{"@type":"ListItem","position":2,"name":"Java &#8211; Date and Time &#8211; Part 2"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/java\/#website","url":"https:\/\/www.kindsonthegenius.com\/java\/","name":"Java Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/java\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/java\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":5,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":209,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/119\/revisions\/209"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/120"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}