{"id":2111,"date":"2026-04-20T09:30:05","date_gmt":"2026-04-20T13:30:05","guid":{"rendered":"http:\/\/www.mymiller.name\/wordpress\/?p=2111"},"modified":"2026-04-20T09:30:05","modified_gmt":"2026-04-20T13:30:05","slug":"unitoftime","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/unitoftime\/","title":{"rendered":"UnitOfTime &#8211; Measure and convert you milliseconds"},"content":{"rendered":"<p>Continuing my series on advanced classes that are handy to have around when needed. &nbsp;I have created the UnitOfTime. &nbsp;This class is used to convert a number of milliseconds over to a more understandable period of time.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>package name.mymiller.extensions.lang;\n\nimport java.math.BigDecimal;\nimport java.math.BigInteger;\n\n\/**\n * Measure UnitOfTime based on milliseconds\n *\n * @author jmiller\n *\/\npublic enum UnitOfTime {\n    \/\/@formatter:off\n    \/**\n     * Millieseconds\n     *\/\n    Millisecond(\"ms\", new BigInteger(\"1\")),\n    \/**\n     * Seconds\n     *\/\n    Second(\"secs\", new BigInteger(\"1000\")),\n    \/**\n     * Minutes\n     *\/\n    Minute(\"mins\", new BigInteger(\"60000\")),\n    \/**\n     * Hours\n     *\/\n    Hour(\"hours\", new BigInteger(\"3600000\")),\n    \/**\n     * Days\n     *\/\n    Day(\"days\", new BigInteger(\"86400000\")),\n    \/**\n     * Weeks\n     *\/\n    Week(\"weeks\", new BigInteger(\"604800000\")),\n    \/**\n     * Years\n     *\/\n    Year(\"years\", new BigInteger(\"31536000000\")),\n    \/**\n     * Millennium\n     *\/\n    Millennium(\"millennium\", new BigInteger(\"31536000000000\"));\n\n    \/\/@formatter:on\n\n    \/**\n     * Label to use on this UnitOfTime\n     *\/\n    private String label = null;\n    \/**\n     * Total Millieseonds\n     *\/\n    private BigInteger milliseconds = null;\n\n    \/**\n     * Constructor for this UnitOfTime\n     *\n     * @param label        Label to use\n     * @param milliseconds Number of milliseconds to make this unit of time.\n     *\/\n    UnitOfTime(String label, BigInteger milliseconds) {\n        this.label = label;\n        this.milliseconds = milliseconds;\n    }\n\n    \/**\n     * Convert a number of milliseconds into a Years Days Hours Minutes Seconds\n     * Milliseconds format\n     *\n     * @param milliseconds Number of milliseconds to convert\n     * @return String containing the conversion\n     *\/\n    public static String convert(BigInteger milliseconds) {\n        BigInteger working = milliseconds;\n        final StringBuilder builder = new StringBuilder();\n\n        final BigInteger years = UnitOfTime.Year.getMilliseconds().divide(working);\n        working = working.subtract(years.multiply(UnitOfTime.Year.getMilliseconds()));\n\n        final BigInteger days = UnitOfTime.Day.getMilliseconds().divide(working);\n        working = working.subtract(days.multiply(UnitOfTime.Day.getMilliseconds()));\n\n        final BigInteger hours = UnitOfTime.Hour.getMilliseconds().divide(working);\n        working = working.subtract(hours.multiply(UnitOfTime.Hour.getMilliseconds()));\n\n        final BigInteger minutes = UnitOfTime.Minute.getMilliseconds().divide(working);\n        working = working.subtract(minutes.multiply(UnitOfTime.Minute.getMilliseconds()));\n\n        final BigInteger seconds = UnitOfTime.Second.getMilliseconds().divide(working);\n        working = working.subtract(seconds.multiply(UnitOfTime.Second.getMilliseconds()));\n\n        if (years.longValue() != 0L) {\n            builder.append(years);\n            builder.append(\" years \");\n        }\n        if (days.longValue() != 0L) {\n            builder.append(days);\n            builder.append(\" days \");\n        }\n        if (hours.longValue() != 0L) {\n            builder.append(hours);\n            builder.append(\" hours \");\n        }\n        if (minutes.longValue() != 0L) {\n            builder.append(minutes);\n            builder.append(\" minutes \");\n        }\n        if (seconds.longValue() != 0L) {\n            builder.append(seconds);\n            builder.append(\" seconds \");\n        }\n        if (working.longValue() != 0L) {\n            builder.append(working);\n            builder.append(\" milliseconds \");\n        }\n\n        return builder.toString();\n    }\n\n    \/**\n     * Determine what unit to use based on the number of milliseconds\n     *\n     * @param milliseconds Total number of milliseconds\n     * @return UnitOfTime you should use.\n     *\/\n    public static UnitOfTime useUnit(BigInteger milliseconds) {\n        if (milliseconds.compareTo(UnitOfTime.Millennium.getMilliseconds()) >= 0) {\n            return UnitOfTime.Millennium;\n        } else if (milliseconds.compareTo(UnitOfTime.Year.getMilliseconds()) >= 0) {\n            return UnitOfTime.Year;\n        } else if (milliseconds.compareTo(UnitOfTime.Week.getMilliseconds()) >= 0) {\n            return UnitOfTime.Week;\n        } else if (milliseconds.compareTo(UnitOfTime.Day.getMilliseconds()) >= 0) {\n            return UnitOfTime.Day;\n        } else if (milliseconds.compareTo(UnitOfTime.Hour.getMilliseconds()) >= 0) {\n            return UnitOfTime.Hour;\n        } else if (milliseconds.compareTo(UnitOfTime.Minute.getMilliseconds()) >= 0) {\n            return UnitOfTime.Minute;\n        } else if (milliseconds.compareTo(UnitOfTime.Second.getMilliseconds()) >= 0) {\n            return UnitOfTime.Second;\n        } else {\n            return UnitOfTime.Millisecond;\n        }\n    }\n\n    \/**\n     * Determine what unit to use based on the number of milliseconds\n     *\n     * @param milliseconds Total number of milliseconds\n     * @return UnitOfTime you should use.\n     *\/\n    public static UnitOfTime useUnit(Integer milliseconds) {\n        return UnitOfTime.useUnit(BigInteger.valueOf(milliseconds));\n    }\n\n    \/**\n     * Determine what unit to use based on the number of milliseconds\n     *\n     * @param milliseconds Total number of milliseconds\n     * @return UnitOfTime you should use.\n     *\/\n    public static UnitOfTime useUnit(Long milliseconds) {\n        return UnitOfTime.useUnit(BigInteger.valueOf(milliseconds));\n    }\n\n    \/**\n     * @return the label\n     *\/\n    public final String getLabel() {\n        return this.label;\n    }\n\n    \/**\n     * @return the milliseconds\n     *\/\n    public final BigInteger getMilliseconds() {\n        return this.milliseconds;\n    }\n\n    \/**\n     * Convert to the UnitOfTime\n     *\n     * @param milliseconds Milliseconds to convert\n     * @return BigDecimal of the number of units\n     *\/\n    public BigDecimal getUnits(BigInteger milliseconds) {\n        return new BigDecimal(milliseconds.divide(this.getMilliseconds()));\n    }\n\n    \/**\n     * Convert to the UnitOfTime\n     *\n     * @param milliseconds Milliseconds to convert\n     * @return BigDecimal of the number of units\n     *\/\n    public BigDecimal getUnits(Integer milliseconds) {\n        return this.getUnits(BigInteger.valueOf(milliseconds));\n    }\n\n    \/**\n     * Convert to the UnitOfTime\n     *\n     * @param milliseconds Milliseconds to convert\n     * @return BigDecimal of the number of units\n     *\/\n    public BigDecimal getUnits(Long milliseconds) {\n        return this.getUnits(BigInteger.valueOf(milliseconds));\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Continuing my series on advanced classes that are handy to have around when needed. &nbsp;I have created the UnitOfTime. &nbsp;This class is used to convert a number of milliseconds over to a more understandable period of time.<\/p>\n","protected":false},"author":1,"featured_media":2342,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[457],"tags":[69],"series":[248],"class_list":["post-2111","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java_extra","tag-java-2","series-advanced-classes"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/03\/time-3222267_640.jpg?fit=640%2C426&ssl=1","jetpack-related-posts":[{"id":2067,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/unitofmemory\/","url_meta":{"origin":2111,"position":0},"title":"UnitOfMemory &#8211; Measure and convert you bytes","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Not very often do I need to take a number of bytes and convert them into the most appropriate measurement. \u00a0This came up recently on a project and I thought it would be nice to just create a class to handle this for me. \u00a0So I created UnitOfMemory as enum\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/info-1641937_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/info-1641937_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/info-1641937_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3249,"url":"https:\/\/www.mymiller.name\/wordpress\/java_tips\/java-tips-part-5\/","url_meta":{"origin":2111,"position":1},"title":"Java Tips Part 5","author":"Jeffery Miller","date":"August 19, 2026","format":false,"excerpt":"Tip 21: Use Prepared Statements When working with JPA\/Hibernate make use of Prepared Statements that can be reused. Basically, I'm saying do not do the following: Query query = JPA.em().createNativeQuery(\"select count(*) from user u inner join\" + \"address a where a.user_id=u.id and a.city='\" + city + \"'\"); BigInteger val =\u2026","rel":"","context":"In &quot;Java Tips&quot;","block_context":{"text":"Java Tips","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_tips\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/12\/sam-dan-truong-rF4kuvgHhU-unsplash-scaled-e1640791434235.jpg?fit=640%2C427&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/12\/sam-dan-truong-rF4kuvgHhU-unsplash-scaled-e1640791434235.jpg?fit=640%2C427&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/12\/sam-dan-truong-rF4kuvgHhU-unsplash-scaled-e1640791434235.jpg?fit=640%2C427&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2072,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/unitofdistance\/","url_meta":{"origin":2111,"position":2},"title":"UnitOfDistance &#8211; Measure and Convert your distance","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Well I was bored one night after creating the UnitOfMemory and decided what else could be useful in the future. \u00a0I came up with a UnitOfDistance to convert distances over land. \u00a0Miles v's kilometers and everything in between. \u00a0The class was easy to come up with, finding the exact conversion\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"UnitOfDistance","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/tape-measure-1860811_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/tape-measure-1860811_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/tape-measure-1860811_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3380,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/synchronous-to-asynchronous\/","url_meta":{"origin":2111,"position":3},"title":"Synchronous to Asynchronous","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Converting a synchronous method to an asynchronous one in Java involves modifying the code to allow other tasks to execute while it is waiting for input\/output operations to complete. Here's an example of how to convert a synchronous method to an asynchronous one in Java: Let's say we have a\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/multitasking-ga6749a2b2_640.jpg?fit=640%2C394&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/multitasking-ga6749a2b2_640.jpg?fit=640%2C394&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/multitasking-ga6749a2b2_640.jpg?fit=640%2C394&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3153,"url":"https:\/\/www.mymiller.name\/wordpress\/architecture\/performance-nanoseconds-now-not-milliseconds\/","url_meta":{"origin":2111,"position":4},"title":"Performance?  nanoseconds, not milliseconds!","author":"Jeffery Miller","date":"March 12, 2022","format":false,"excerpt":"In today's time of high-performance computing, we no longer have the luxury of measuring our time in milliseconds, we need to move past this into nanoseconds. When I first started programming in the '90s we measured our code in seconds. That quickly gave way to milliseconds, even before the '90s\u2026","rel":"","context":"In &quot;Architecture&quot;","block_context":{"text":"Architecture","link":"https:\/\/www.mymiller.name\/wordpress\/category\/architecture\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2022\/01\/superhero-g470982bed_640.jpg?fit=640%2C418&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2022\/01\/superhero-g470982bed_640.jpg?fit=640%2C418&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2022\/01\/superhero-g470982bed_640.jpg?fit=640%2C418&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":4011,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_ai\/harnessing-transformer-models-with-spring-ai-a-developers-guide-to-enterprise-ai-architecture\/","url_meta":{"origin":2111,"position":5},"title":"Harnessing Transformer Models with Spring AI: A Developer&#8217;s Guide to Enterprise AI Architecture","author":"Jeffery Miller","date":"August 20, 2026","format":false,"excerpt":"Introduction Since the groundbreaking 2017 paper \"Attention Is All You Need\" introduced the Transformer architecture, deep learning and natural language processing (NLP) have experienced a monumental shift. From Large Language Models (LLMs) like GPT-4, LLaMA, and Claude to specialized embedding models like BERT and MiniLM, Transformers form the bedrock of\u2026","rel":"","context":"In &quot;AI&quot;","block_context":{"text":"AI","link":"https:\/\/www.mymiller.name\/wordpress\/category\/ai\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/07\/1786883676314.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/07\/1786883676314.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/07\/1786883676314.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/07\/1786883676314.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/07\/1786883676314.avif 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=2111"}],"version-history":[{"count":8,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2111\/revisions"}],"predecessor-version":[{"id":2906,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2111\/revisions\/2906"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/2342"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=2111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=2111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=2111"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=2111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}