# \[SOLVED\] Converting millis to Sec, Min, Hrs, Days

**URL:** https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473
**Category:** Coding Questions
**Created:** [April 20, 2019, 1:27am UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473 "2019-04-20T01:27:08Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [April 20, 2019, 1:27am UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/1 "2019-04-20T01:27:08Z")

</div>

I am well aware of the built in time functions of processing, the ones that get you the computer clock time.

However I have a situation where I get large amounts of milliseconds of timed processes , and I would like to convert those in a human reading format.  
Java has some library like:

```auto
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);

```

But that does not work for us.  
I wonder if someone wrote at least a function to take care of this.  
Thanks a lot,  
Mitch

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [April 20, 2019, 1:41am UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/2 "2019-04-20T01:41:26Z")

</div>

as you not say like " i want show ( nicely ) the time since start of the program"  
you possibly now aware that that is what millis mean?

and yes, i also not aware of a ready “data time” tool for this  
but try

```auto
int milli;
int hours;
int minutes; 
int seconds; 
int days;

void setup () {
  size(600, 400);
}

void draw() {
  background(0);
  get_time();
  text("millis from start = " + nf(days, 3) + "_"+ nf(hours, 2) + ":" + nf(minutes, 2) + ":" + nf(seconds, 2) + ":" + nf(milli, 3), 10, 20);

  text("Time: "+year()+"/"+nf(month(),2)+"/"+nf(day(),2)+"_"+nf(hour(),2)+":"+nf(minute(),2)+":"+nf(second(),2), width-170, 20);

}

void get_time() {
  milli = millis();
  seconds = milli / 1000;
  minutes = seconds / 60;
  hours = minutes / 60;
  days = hours / 24;

  //This text shows every number of millis, seconds, and minutes as if their independant numbers that don't reset to 0. 
  //aka don't loop around every particular number. millis should go to 999 then to 0, and repeat.  
  //Seconds should be based on every 1000 millis, and zero back to 0 at 60000. same for minutes.  

  //My theory is to create multiple functions which return a "looping" of each variable from 0 to it's own respective highest num.

  // point is you must calc all backward now:
  hours = hours - days * 24;
  minutes = minutes - days * 24 * 60 - hours * 60;
  seconds = seconds - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60;
  milli = milli - days * 24 * 60 * 60 * 1000 - hours * 60 * 60 * 1000 - minutes * 60 * 1000 - seconds * 1000;
}

```

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [April 20, 2019, 3:59am UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/3 "2019-04-20T03:59:39Z")

</div>

Thanks ,

I really meant “Converting Miliseconds in Sec, etc”

Here is what i came up with, it seems to work.

```auto
void MsConversion(int MS)

{
int totalSec= (MS / 1000);
int seconds = (MS / 1000) % 60;
int minutes = (MS / (1000*60)) % 60;
int hours = ((MS/(1000*60*60)) % 24);                      

String HumanTime= (hours+": " +minutes+ ": "+ seconds);
println (HumanTime);
}

```

Hope it helps other folks.

Mitch

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [April 20, 2019, 11:08am UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/4 "2019-04-20T11:08:48Z")

</div>

> [@laptophead](#):
>
> I really meant “Converting Miliseconds in Sec, etc”

now that is confusing, where you get “Miliseconds” from if not from millis()??  
and you say that in your question header!

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 20, 2019, 11:18am UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/5 "2019-04-20T11:18:01Z")

</div>

```auto

void setup() {
  size(600, 1000);
  fill(255);
}

void draw() {
  background (0);
  text(MsConversion(millis()) + "\n"+millis(), 44, 44);
}

String MsConversion(int MS) {
  int totalSec= (MS / 1000);
  int seconds = (MS / 1000) % 60;
  int minutes = (MS / (1000*60)) % 60;
  int hours = ((MS/(1000*60*60)) % 24);                      

  return hours+": " +minutes+ ": "+ seconds;
}

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 21, 2019, 4:27pm UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/6 "2019-04-21T16:27:33Z")

</div>

> [@laptophead](#):
>
> Java has some library like:
> 
> ```auto
> long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
> 
> ```
> 
> But that does not work for us.

I’m not sure why that didn’t work for you. I looked up the top TimeUnit example on stackoverflow based on your example code:

[https://stackoverflow.com/a/625624/7207622](https://stackoverflow.com/a/625624/7207622)

…and when I imported TimeUnit into a simple sketch, the code from the top answer worked just fine.

```auto
import java.util.concurrent.TimeUnit;

void draw() {
  int now = millis();
  String time = String.format("%d hrs, %d min, %d sec", 
    TimeUnit.MILLISECONDS.toHours(now), 
    TimeUnit.MILLISECONDS.toMinutes(now), 
    TimeUnit.MILLISECONDS.toSeconds(now) - 
    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(now))
    );
  println(time);
}

```

> 16 hrs, 963 min, 6 sec

Of course, you have to do the same subtraction math (look at mins) that you do in a native Processing sketch for each step, based on which is the highest time unit and which is a remainder. To me this means that your native solution `msConversion` is more readable for a beginner than using TimeUnit.

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [April 21, 2019, 4:44pm UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/7 "2019-04-21T16:44:32Z")

</div>

Thanks a lot,  
Did you have to install the “TimeUnit” library? Manually?

While on this subject:  
How many days of millis do I get before Processing re-sets it to 0?

I have a long running application. I wonder if there’s a way to reset millis without making the user re-start the sketch?

My other option is to write my algorithms based on the computer date - time.  
That seems really hard. During the same day, I could deduct an end time from a starting time.  
such as (ending)23:33:55 minus (Starting) 18:55:33 . I would convert everything to seconds and get a difference.  
But what do I do for timing a process that starts on one date and ends the next day?

Thanks

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 21, 2019, 4:50pm UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/8 "2019-04-21T16:50:23Z")

</div>

No, just add the import command line to you sketch.

It is part of java (`java.util.concurrent`) and thus already available to import with no additional install needed.

[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/TimeUnit.html](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/TimeUnit.html)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 21, 2019, 4:57pm UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/9 "2019-04-21T16:57:59Z")

</div>

> [@laptophead](#):
>
> How many days of **millis()** do I get before Processing resets it to `0`?

```java
import java.util.concurrent.TimeUnit;

// Max millis() in days:
println(TimeUnit.MILLISECONDS.toDays(MAX_INT)); // 24

// Max frameCount in days (60 FPS):
println(TimeUnit.SECONDS.toDays(MAX_INT/60)); // 414

exit();

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 21, 2019, 5:01pm UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/10 "2019-04-21T17:01:23Z")

</div>

> [@laptophead](#):
>
> How many days of millis do I get before Processing re-sets it to 0?

```auto
println(MAX_INT);

```

> 2147483647

So, if you plug that into now, that many milliseconds is about:

> 596 hrs

~24 days

Here are the details on frameCount overflow (not millis) and restarting:

- [How big is the systemvariable frameCount? - Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/comment/87073/#Comment_87073)

Here is an example of creating your own resettable millis (the core millis cannot be reset).

- [Restart - Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/comment/114971/#Comment_114971)

As an alternative, you could keep your own higher resolution millis timer (e.g. a double) and increment it whenever millis overflows, every 24 days or so. Or when framecount overflows, if you plan to run for 2.3 years.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 21, 2019, 5:09pm UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/11 "2019-04-21T17:09:10Z")

</div>

> [@laptophead](#):
>
> My other option is to write my algorithms based on the computer date - time.

You can also use System.**currentTimeMillis()** if need more than 24 days nonstop running: ⏰  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#currentTimeMillis()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#currentTimeMillis())

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [April 21, 2019, 5:33pm UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/12 "2019-04-21T17:33:37Z")

</div>

> [@GoToLoop](#):
>
> **TimeMillis()** i

That is cool, I never knew about CTM=System.currentTimeMillis();

It is a huge number, so I declared it in setup and then I made the difference in the loop,

It became a similar number with the millis.  
Declarations:  
public static long CTM;  
long CTM\_Ms;

Setup:  
CTM=System.currentTimeMillis();

Draw:  
CTM\_Ms =System.currentTimeMillis()-CTM;  
println (CTM\_Ms);

Should I assume the “System.currentTimeMillis();” will run for ever?  
Will never max out? For years to come?

Thanks, that got me out of trouble.

I read somewhere that millis reset in 9 days. True? That would mess up my data recordings.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 21, 2019, 7:24pm UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/13 "2019-04-21T19:24:54Z")

</div>

> [@laptophead](#):
>
> Should I assume the “System.currentTimeMillis();” will run for ever?  
> Will never max out? For years to come?

It is a long, so just like we did with int:

```auto
System.out.println(new Date(Long.MAX_VALUE));

```

> Sun Aug 17 03:12:55 GMT-04:00 292278994

So, only 292276975 years to go.

For discussion see:

> <https://stackoverflow.com/questions/2978452/when-will-system-currenttimemillis-overflow>

Note that, realistically, if you plan on having a personal computer – or server – that executes a continuous process with no interruption for even 20 years: the chances are incredibly small that you will achiece that runtime, even with a good hardware and infrastructure plan. You will probably have a forced software update, soft reboot, hack, software failure, hardware failure, power failure, natural disaster, theft, et cetera. Odds are your hardware alone just won’t last ten years without a stop.

If you DID want a multi-decade continuous process, PDE probably isn’t a good way to do it. Most approaches (outside exceptional requirements like space probe engineering) don’t even try to do single-process, single-machine – they instead start by making a process distributed so that one can replace hardware and software incrementally over time to keep the process alive as things break and and become rapidly obsolete.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 21, 2019, 7:44pm UTC](https://discourse.processing.org/t/solved-converting-millis-to-sec-min-hrs-days/10473/14 "2019-04-21T19:44:07Z")

</div>

> [@laptophead](#):
>
> I read somewhere that **millis()** resets in 9 days. True?

Scroll up and take a look at my posted code sample: 📜

> [@\[SOLVED\] Converting millis to Sec, Min, Hrs, Days](https://discourse.processing.org/t/converting-millis-to-sec-min-hrs-days-solved/10473/9):
>
> import java.util.concurrent.TimeUnit; // Max millis() in days: println(TimeUnit.MILLISECONDS.toDays(MAX\_INT)); // 24 // Max frameCount in days (60 FPS): println(TimeUnit.SECONDS.toDays(MAX\_INT/60)); // 414 exit();

It’s 24 days and some hours for **millis()**. 😳

Or use _frameCount_ for more than 1 year (414 days) uninterrupted: 📅

> **[frameCount / Reference](https://processing.org/reference/frameCount.html)**
>
> The system variable frameCount contains the number o frames displayed since the program started. Inside setup() the value is 0 and during the first iteration of draw it is 1, etc.
