{"id":1933,"date":"2017-09-12T12:03:47","date_gmt":"2017-09-12T04:03:47","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1933"},"modified":"2017-09-12T12:04:12","modified_gmt":"2017-09-12T04:04:12","slug":"java-and-time-with-processing","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2017\/09\/12\/java-and-time-with-processing\/","title":{"rendered":"Java and time with Processing"},"content":{"rendered":"<p>Instead of using the Processing <a href=\"https:\/\/processing.org\/reference\/millis_.html\">millis()<\/a> function or the Java <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Timer.html\">Timer<\/a> class, we can also make use of the relatively new <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/time\/Instant.html\">Instant<\/a> and <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/time\/Duration.html\">Duration<\/a> classes in Java 8. Here is one simple example for demonstration.<\/p>\n<p>The program uses 2 <em>Instant<\/em> variables: <em>start<\/em>, <em>end<\/em>. It computes the time duration between them using the <em>Duration.between()<\/em> function.<\/p>\n<pre lang=\"java\">\r\nimport java.time.Instant;\r\nimport java.time.Duration;\r\n\r\nInstant start;\r\nPFont font;\r\n\r\nvoid setup() {\r\n  size(640, 480);\r\n  start = Instant.now();\r\n  frameRate(30);\r\n  font = loadFont(\"AmericanTypewriter-24.vlw\");\r\n  textFont(font, 24);\r\n}\r\n\r\nvoid draw() {\r\n  background(0);\r\n  Instant end = Instant.now();\r\n  Duration elapsed = Duration.between(start, end);\r\n  long ns = elapsed.toNanos();\r\n  text(Long.toString(ns), 230, 230);\r\n}\r\n<\/pre>\n<p>&nbsp;<br \/>\n<a href=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2017\/09\/JavaTime.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2017\/09\/JavaTime-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" class=\"alignnone size-medium wp-image-1934\" srcset=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2017\/09\/JavaTime-300x225.jpg 300w, http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2017\/09\/JavaTime.jpg 640w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Instead of using the Processing millis() function or the Java Timer class, we can also make use of the relatively new Instant and Duration classes in Java 8. Here is one simple example for demonstration. The program uses 2 Instant variables: start, end. It computes the time duration between them using the Duration.between() function. import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[111,62,155],"class_list":["post-1933","post","type-post","status-publish","format-standard","hentry","category-testing","tag-java","tag-processing-org","tag-time"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=1933"}],"version-history":[{"count":2,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1933\/revisions"}],"predecessor-version":[{"id":1936,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1933\/revisions\/1936"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1933"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}