Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8155383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:39:25+00:00 2026-06-06T16:39:25+00:00

Is there a fast, low-garbage way to do it? I can’t just do simple

  • 0

Is there a fast, low-garbage way to do it? I can’t just do simple modulus arithmetic since that doesn’t account for leap seconds and other date/time funny business.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:39:26+00:00Added an answer on June 6, 2026 at 4:39 pm

    This is a fast, zero-garbage solution. It is of key importance not to create a new instance of Calendar on each call because it’s quite a heavyweight object, taking 448 bytes of heap and almost a microsecond to initialize (Java 6, 64-bit HotSpot, OS X).

    HmsCalculator is intended for use from a single thread (each thread must use a different instance).

    public class HmsCalculator
    {
      private final Calendar c = Calendar.getInstance();
    
      public Hms toHms(long t) { return toHms(t, new Hms()); }
      public Hms toHms(long t, Hms hms) {
        c.setTimeInMillis(t*1000);
        return hms.init(c);
      }
      public static class Hms {
        public int h, m, s;
        private Hms init(Calendar c) {
          h = c.get(HOUR_OF_DAY); m = c.get(MINUTE); s = c.get(SECOND);
          return this;
        }
        public String toString() { return String.format("%02d:%02d:%02d",h,m,s); }
      }
    
      public static void main(String[] args) {
        System.out.println(new HmsCalculator().toHms(
           System.currentTimeMillis()/1000));
      }
    }
    

    P.S. I didn’t paste all those static imports (boring).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a fast algorithm, similar to power of 2, which can be used
Is there any fast algorithm that allows to compare two files (for verification purpose)
Is there a fast/efficiency way to check if a table is empty? DECLARE @StartEndTimes
Is there a fast way to count the amount of successive null bytes starting
On this page , I see something interesting: Note that there is a fast-path
It seems that there are several really fast prime factorization algorithms around (one that
I need to check in some fast way if there is any text nodes
I'm making a homebrew video-game console (simple low spec pc) that will constantly run
Is there any fast and memory efficient way to read specific lines of large
Is there a fast way to find if a 32-bit integer is a multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.