{"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:\/\/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:\/\/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:\/\/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"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":5,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":209,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/119\/revisions\/209"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/120"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}