How can I calculate the difference between two dates and show it in the format hours:minutes:seconds?
Example:
StartTime : 2016-12-20T04:30
EndTime : 2016-12-22T05:00
The output should be "48hours 30minutes 0 seconds".
This is what I've tried:
Long diff = (endDate.get time() -startDate.gettime())/1000;
Log.d("App","difference in hour is"+diff/1000/60/60);
Mins = diff/1000/60;
Seconds = diff/1000;
Using this code I'm getting hours as a correct value. But the minute and seconds values are not getting their proper values.
Try this function:-
//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public void printDifference(Date startDate, Date endDate){
//milliseconds
long different = endDate.getTime() - startDate.getTime();
System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);
System.out.println("different : " + different);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
//long elapsedDays = different / daysInMilli;
//different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
System.out.printf(
"%d hours, %d minutes, %d seconds%n",
elapsedHours, elapsedMinutes, elapsedSeconds);
}
Try
1. Add following methods first, then use parseDate.
Date startDate = parseDate("2016-12-20T04:30");
Date endDate = parseDate("2016-12-22T05:00");
2. Calculate difference b/w these two
long differenceInMillis = endDate.getTime() - startDate.getTime();
3. Use formatElapsedTime method to formatted difference
String formattedText = formatElapsedTime(differenceInMillis/1000); //divide by 1000 to get seconds from milliseconds
//Result will be 48hours 30minutes 0 seconds
public static Date parseDate (String strDate) {
DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm");
Date date1 = null;
try {
date1 = dateFormat.parse (strDate);
}
catch (ParseException e) {
e.printStackTrace ();
}
return date1;
}
public static String formatElapsedTime (long seconds) {
long hours = TimeUnit.SECONDS.toHours(seconds);
seconds -= TimeUnit.HOURS.toSeconds (hours);
long minutes = TimeUnit.SECONDS.toMinutes (seconds);
seconds -= TimeUnit.MINUTES.toSeconds (minutes);
return String.format ("%dhr:%dmin:%dsec", hours, minutes, seconds);
}
import java.util.Calendar;
public class DateDifferenceExample {
public static void main(String[] args) {
// Creates two calendars instances
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
// Set the date for both of the calendar instance
cal1.set(2006, Calendar.DECEMBER, 30);
cal2.set(2007, Calendar.MAY, 3);
// Get the represented date in milliseconds
long millis1 = cal1.getTimeInMillis();
long millis2 = cal2.getTimeInMillis();
// Calculate difference in milliseconds
long diff = millis2 - millis1;
// Calculate difference in seconds
long diffSeconds = diff / 1000;
// Calculate difference in minutes
long diffMinutes = diff / (60 * 1000);
// Calculate difference in hours
long diffHours = diff / (60 * 60 * 1000);
// Calculate difference in days
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.println("In milliseconds: " + diff + " milliseconds.");
System.out.println("In seconds: " + diffSeconds + " seconds.");
System.out.println("In minutes: " + diffMinutes + " minutes.");
System.out.println("In hours: " + diffHours + " hours.");
System.out.println("In days: " + diffDays + " days.");
}
}
New answer to an old question using a newer API: java.time
You can write a method that actually accepts the datetimes as Strings along with a time zone and then calculates the difference by means of a class designed for such purpose: java.time.Duration
Here's a code example:
public static String getDifference(String firstDt, String secondDt, String zone) {
// create the zone for the calculation just to respect daylight saving time
ZoneId zoneId = ZoneId.of(zone);
// then parse the datetimes passed and add the time zone
ZonedDateTime firstZdt = ZonedDateTime.of(
LocalDateTime.parse(firstDt), zoneId
);
ZonedDateTime secondZdt = ZonedDateTime.of(
LocalDateTime.parse(secondDt), zoneId
);
// calculate the duration between the two datetimes
Duration duration;
/*
* the JavaDocs of Duration tell us the following:
*
* "The result of this method can be a negative period
* if the end is before the start.".
*
* So we need to make sure the older datetime will be
* the "start" in the method "between(start, end)"
*/
if (firstZdt.isAfter(secondZdt)) {
duration = Duration.between(secondZdt, firstZdt);
} else {
duration = Duration.between(firstZdt, secondZdt);
}
// store the amount of full hours the duration has
long hoursBetween;
hoursBetween = duration.toHours();
// calculate the minutes left from the full duration in minutes
long minutesBetween;
minutesBetween = duration.toMinutes() - (hoursBetween * 60);
// calculate the seconds left from the full duration in seconds
long secondsBetween;
secondsBetween = duration.getSeconds() - (duration.toMinutes() * 60);
// build the result String, take care of possibly missing leading zeros
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.append(hoursBetween).append(" hours ");
if (minutesBetween < 10 && minutesBetween > 0)
resultBuilder.append("0");
resultBuilder.append(minutesBetween).append(" minutes ");
if (secondsBetween < 10 && secondsBetween > 0)
resultBuilder.append("0");
resultBuilder.append(secondsBetween).append(" seconds");
return resultBuilder.toString();
}
If you use it in a main...
public static void main(String[] args) {
String timeDiff = getDifference("2016-12-20T04:30", "2016-12-22T05:00", "UTC");
System.out.println(timeDiff);
}
... you will get the following output:
48 hours 30 minutes 0 seconds
The code above is the one to be used in Java 8, later on, Duration got the methods toHoursPart(), toMinutesPart() and toSecondsPart() which actually do the necessary calculation internally.
The code that would change (tried with Java 11):
// store the amount of full hours the duration has
long hoursBetween;
hoursBetween = duration.toHoursPart();
// calculate the minutes left from the full duration in minutes
long minutesBetween;
minutesBetween = duration.toMinutesPart();
// calculate the seconds left from the full duration in seconds
long secondsBetween;
secondsBetween = duration.toSecondsPart();
i have some problem while finding difference between times, if i try to find difference in todays time (say t1 = "08:00:00" and t2 = "10:00:00" then it is giving correct output,)but when i try to find the difference like t1 = "20:00:00"(which is todays time) and t2 ="08:00:00"(which is next day morning),i want the output as 12 hours but i a getting wrong outputs. kindly help me.
String t1 = "20:00:00";
String t2 = "12:00:00";
String t3 = "24:00:00";
SimpleDateFormat sDf = new SimpleDateFormat("HH:mm:ss");
Date d1 = sDf.parse(t1);
Date d2 = sDf.parse(t2);
Date d3 = sDf.parse(t3);
long s1,s2,s3,s4,dif1,dif2,dif3,minutes,hrs;
dif1 = d2.getTime() - d1.getTime();
s1 = dif1/1000;
System.out.println("s1 "+s1);
if(s1<0){
dif2 = d3.getTime() - d1.getTime();
s2 = dif2/1000;
System.out.println("s2 "+s2);
dif3 = d2.getTime();
s3 = dif3/1000;
System.out.println("s3 "+s3);
s4 = s2+s3;
minutes = s4 / 60;
s4 = s4 % 60;
hrs = minutes / 60;
minutes = minutes % 60;
System.out.println("time difference is : "+hrs+": "+minutes+" :"+s4);
}else{
minutes = s1 / 60;
s1 = s1 % 60;
hrs = minutes / 60;
minutes = minutes % 60;
System.out.println("time difference is : "+hrs+": "+minutes+" :"+s1);
}
Any date/time manipulation/calculation should be done though the use of well defined and tested APIs like Java 8's Time API or Joda Time
Java 8
public class TestTime {
public static void main(String[] args) {
// Because of the ability for the time to roll over to the next
// day, we need the date component to make sense of it, for example
// 24:00 is actually 00:00 of the next day ... joy
LocalDateTime t1 = LocalTime.of(20, 00).atDate(LocalDate.now());
LocalDateTime t2 = LocalTime.of(12, 00).atDate(LocalDate.now());
LocalDateTime t3 = LocalTime.MIDNIGHT.atDate(LocalDate.now()).plusDays(1);
if (t1.isAfter(t2)) {
System.out.println("Before");
Duration duration = Duration.between(t2, t3);
System.out.println(format(duration));
} else {
System.out.println("After");
Duration duration = Duration.between(t2, t1);
System.out.println(format(duration));
}
}
public static String format(Duration duration) {
long hours = duration.toHours();
duration = duration.minusHours(hours);
return String.format("%02d hours %02d minutes", hours, duration.toMinutes());
}
}
Which outputs
12 hours 00 minutes
The question I can't seem to answer in your code is why you did this s4 = s2+s3;, basically adding 12:00 to 24:00
See some options here
such as :
LocalDate today = LocalDate.now()
LocalDate yeasterday = today.minusDays(1);
Duration oneDay = Duration.between(today, yesterday);
Duration.between(today.atTime(0, 0), yesterday.atTime(0, 0)).toDays() // another option
String time1 = "08:00:00";
String time2 = "17:00:00";
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1 = format.parse(time1);
Date date2 = format.parse(time2);
long difference = date2.getTime() - date1.getTime();
Not using Java 8 or Joda-Time, and ensuring that local time zone DST is not involved.
public static void printDiff(String time1, String time2) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
int seconds = (int)(df.parse(time2).getTime() - df.parse(time1).getTime()) / 1000;
if (seconds < 0)
seconds += 24 * 60 * 60;
int minutes = seconds / 60;
int hours = minutes / 60;
seconds %= 60;
minutes %= 60;
System.out.printf("There are %d hours %d minutes %d seconds from %s to %s%n",
hours, minutes, seconds, time1, time2);
}
Test
public static void main(String[] args) throws Exception {
printDiff("08:00:00", "10:00:00");
printDiff("20:00:00", "08:00:00");
printDiff("15:47:22", "11:12:38");
printDiff("08:00:00", "07:59:59");
printDiff("08:00:00", "08:00:00");
}
Output
There are 2 hours 0 minutes 0 seconds from 08:00:00 to 10:00:00
There are 12 hours 0 minutes 0 seconds from 20:00:00 to 08:00:00
There are 19 hours 25 minutes 16 seconds from 15:47:22 to 11:12:38
There are 23 hours 59 minutes 59 seconds from 08:00:00 to 07:59:59
There are 0 hours 0 minutes 0 seconds from 08:00:00 to 08:00:00
I want to calculate the difference between a start time and an end time. In HH:mm format.
I receive a negative value when, for example, the start time is 22.00 and the end time is 1.00 the next day.
How do I let the program know the end time is on the next day?
My script:
public void setBeginTijd()
{
String dateStart = "22:00";
String dateEnd = "1:00";
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date d1 = null;
Date d2 = null;
try
{
d1 = format.parse(dateStart);
d2 = format.parse(dateEnd);
long diff = d2.getTime() - d1.getTime();
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
System.out.println(diffMinutes);
System.out.println(diffHours);
}
catch (Exception e)
{
e.printStackTrace();
}
}
If you can assume that, when the time is negative, the second time must be on the next day, then you can simply say
if (diff < 0)
{
diff = (24 * 60 * 60 * 1000) + diff;
}
EDIT to elaborate this, also in response to the comments: Of course this is a very simplistic solution. It can not handle the case where the second date is two days later. It does not handle DST switches. It does not handle the time zone change on December 31st, 1927 in Shanghai. It is no replacement for a properly modelled date with all its caveats. It is a best-effort approach to derive what can (probably) be derived from the given information.
Try this
SimpleDateFormat formatNextDay = new SimpleDateFormat("dd:HH:mm");
boolean isNextDay=false;
try {
if (d1.after(d2)) {
isNextDay=true;
d1 = formatNextDay.parse("1:" + dateStart);
d2 = formatNextDay.parse("2:" + dateEnd);
}
As already mentioned by some people, it is important to also know day, month and year of each event to calculate periods for events that are not on the same day.
I modified your method the way I think it could help you:
public void setBeginTijd()
{
String dateStart = "22.08.2014 22:00";
String dateEnd = "25.08.2014 01:00";
SimpleDateFormat fullFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
Date d1 = null;
Date d2 = null;
try
{
d1 = fullFormat.parse(dateStart);
d2 = fullFormat.parse(dateEnd);
long diff = d2.getTime() - d1.getTime();
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.println("Delta minutes: " + diffMinutes);
System.out.println("Delta hours: " + diffHours);
System.out.println("Delta days: " + diffDays);
}
catch (Exception e)
{
e.printStackTrace();
}
}
You should include day, month and year in date.
This are dates in Java after ran program:
d1 = Thu Jan 01 22:00:00 CET 1970
d2 = Thu Jan 01 01:00:00 CET 1970
Here is the correct math for time difference in hours & minutes. Stripping of the decimal fraction is happening automatically when you operate on int/long values.
long diff = d2.getTime() - d1.getTime();
long diffHours = diff / 1000 / 60 / 60;
long diffMinutes = diff / 1000 / 60 - diffHours * 60;
I want to calculate the difference between a certain date and the current time.
int month = 9;
int day = 17;
int year = 2013;
Calendar date = new GregorianCalendar(year, month, day);
int miliseconds= (int) (System.currentTimeMillis() - calendar.getTimeInMillis());
System.out.println(msToString(second));
String msToString(int ms) {
return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")).format(new Date(ms));
}
the output is
13091-13091/? D/GTA: 1970-01-08 15:00:20.287
I want to get the amount of days, hours,minutes and seconds remaining.
What do I wrong?
you could try something like the following method
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TimeToGoCalculator {
/**
* #param args
*/
public static void main(String[] args) {
int month = 8;
int day = 19;
int year = 2013;
Calendar calendar = new GregorianCalendar(year, month, day);
int timeToGo = (int) (calendar.getTimeInMillis() - System.currentTimeMillis())/1000;
System.out.println(secondsToString(timeToGo));
}
private static String secondsToString(int seconds) {
int days = seconds / 24 / 3600;
int hours = (seconds - (days * 24 * 3600 )) / 3600;
int minutes = (seconds - (days * 24 * 3600 + hours * 3600)) / 60;
seconds = (seconds - (days * 24 * 3600 + hours * 3600 + minutes * 60));
return "The remaining time is "+days+" days, "+hours+" hours, "+minutes+
" minutes, and "+seconds+" seconds.";
}
}
That should give you the output you're looking for.
Notice that, when creating the GregorianCalendar object, the month is 0-indexed, so September would be = 8.
Use Joda Time library
Period class can help.
int month = 9;
int day = 17;
int year = 2013;
int hour= 0;
int minute =0;
int second =0;
int millisecond = 0;
DateTime dt1 = new DateTime(); //now
DateTime dt2 = new DateTime(year, month, day, hour, minute, second, millisecond);
//assuming dt1 is before dt2:
Period period = new Period(dt1, dt2, PeriodType.dayTime());
/*
periodType.dayTime()):
Gets a type that defines all standard fields from days downwards.
days
hours
minutes
seconds
milliseconds
*/
PeriodFormatter periodFormatter = new PeriodFormatterBuilder()
.printZeroAlways()
.minimumPrintedDigits(2)
.appendDays().appendSuffix("days ")
.appendHours().appendSuffix("hours ")
.appendMinutes().appendSuffix("minutes ")
.appendSeconds().appendSuffix("seconds ");
.toFormatter();
System.out.println(periodFormatter.print(period));
Are you able to use external libraries? Then Joda Time can help you, especially the Period class.
It has a constructor for two time instants and gives you the difference between the time instants in years/months/days/hours/seconds/milliseconds.
Your second variable holds an amount of milliseconds between the two dates, not a new date. You need to do some calculation using these milliseconds to get an amount of days, for instance.
You could do something like this:
int minutes = second/1000/60; // millis to seconds, seconds to minutes
to get an amount of minutes, then convert to hours, and so on.
I'm confused. After stumbling upon this thread, I tried to figure out how to format a countdown timer that had the format hh:mm:ss.
Here's my attempt -
//hh:mm:ss
String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
So, when I try a value like 3600000ms, I get 01:59:00, which is wrong since it should be 01:00:00. Obviously there's something wrong with my logic, but at the moment, I cannot see what it is!
Can anyone help?
Edit -
Fixed it. Here's the right way to format milliseconds to hh:mm:ss format -
//hh:mm:ss
String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
The problem was this TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)). It should have been this TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)) instead.
You were really close:
String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), // The change is in this line
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
You were converting hours to millisseconds using minutes instead of hours.
BTW, I like your use of the TimeUnit API :)
Here's some test code:
public static void main(String[] args) throws ParseException {
long millis = 3600000;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
}
Output:
01:00:00
I realised that my code above can be greatly simplified by using a modulus division instead of subtraction:
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1));
Still using the TimeUnit API for all magic values, and gives exactly the same output.
The generic method for this is fairly simple:
public static String convertSecondsToHMmSs(long seconds) {
long s = seconds % 60;
long m = (seconds / 60) % 60;
long h = (seconds / (60 * 60)) % 24;
return String.format("%d:%02d:%02d", h,m,s);
}
If you are using apache commons:
DurationFormatUtils.formatDuration(timeInMS, "HH:mm:ss,SSS");
I used this:
String.format("%1$tH:%1$tM:%1$tS.%1$tL", millis);
See description of class Formatter.
See runnable example using input of 2400 ms.
// New date object from millis
Date date = new Date(millis);
// formattter
SimpleDateFormat formatter= new SimpleDateFormat("HH:mm:ss.SSS");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
// Pass date object
String formatted = formatter.format(date );
You can also use new DateTime API
var formatted = DateTimeFormatter.ofPattern("HH:mm:ss.SSS")
.withZone(ZoneId.of("UTC"))
.format(Instant.ofEpochMilli(millis));
DateFormat df = new SimpleDateFormat("HH:mm:ss");
String formatted = df.format(aDateObject);
this worked for me, with kotlin
fun formatToDigitalClock(miliSeconds: Long): String {
val hours = TimeUnit.MILLISECONDS.toHours(miliSeconds).toInt() % 24
val minutes = TimeUnit.MILLISECONDS.toMinutes(miliSeconds).toInt() % 60
val seconds = TimeUnit.MILLISECONDS.toSeconds(miliSeconds).toInt() % 60
return when {
hours > 0 -> String.format("%d:%02d:%02d", hours, minutes, seconds)
minutes > 0 -> String.format("%02d:%02d", minutes, seconds)
seconds > 0 -> String.format("00:%02d", seconds)
else -> {
"00:00"
}
}
}
Test results for the 4 implementations
Having to do a lot of formatting for huge data, needed the best performance, so here are the (surprising) results:
for (int i = 0; i < 1000000; i++) {
FUNCTION_CALL
}
Durations:
combinationFormatter: 196 millis
formatDuration: 272 millis
apacheFormat: 754 millis
formatTimeUnit: 2216 millis
public static String apacheFormat(long millis) throws ParseException {
return DurationFormatUtils.formatDuration(millis, "HH:mm:ss");
}
public static String formatTimeUnit(long millis) throws ParseException {
String formatted = String.format(
"%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
return formatted;
}
public static String formatDuration(final long millis) {
long seconds = (millis / 1000) % 60;
long minutes = (millis / (1000 * 60)) % 60;
long hours = millis / (1000 * 60 * 60);
StringBuilder b = new StringBuilder();
b.append(hours == 0 ? "00" : hours < 10 ? String.valueOf("0" + hours) :
String.valueOf(hours));
b.append(":");
b.append(minutes == 0 ? "00" : minutes < 10 ? String.valueOf("0" + minutes) :
String.valueOf(minutes));
b.append(":");
b.append(seconds == 0 ? "00" : seconds < 10 ? String.valueOf("0" + seconds) :
String.valueOf(seconds));
return b.toString();
}
public static String combinationFormatter(final long millis) {
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis));
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis));
long hours = TimeUnit.MILLISECONDS.toHours(millis);
StringBuilder b = new StringBuilder();
b.append(hours == 0 ? "00" : hours < 10 ? String.valueOf("0" + hours) :
String.valueOf(hours));
b.append(":");
b.append(minutes == 0 ? "00" : minutes < 10 ? String.valueOf("0" + minutes) :
String.valueOf(minutes));
b.append(":");
b.append(seconds == 0 ? "00" : seconds < 10 ? String.valueOf("0" + seconds) :
String.valueOf(seconds));
return b.toString();
}
Java 9
Duration timeLeft = Duration.ofMillis(3600000);
String hhmmss = String.format("%02d:%02d:%02d",
timeLeft.toHours(), timeLeft.toMinutesPart(), timeLeft.toSecondsPart());
System.out.println(hhmmss);
This prints:
01:00:00
You are doing right in letting library methods do the conversions involved for you. java.time, the modern Java date and time API, or more precisely, its Duration class does it more elegantly and in a less error-prone way than TimeUnit.
The toMinutesPart and toSecondsPart methods I used were introduced in Java 9.
Java 6, 7 and 8
long hours = timeLeft.toHours();
timeLeft = timeLeft.minusHours(hours);
long minutes = timeLeft.toMinutes();
timeLeft = timeLeft.minusMinutes(minutes);
long seconds = timeLeft.toSeconds();
String hhmmss = String.format("%02d:%02d:%02d", hours, minutes, seconds);
System.out.println(hhmmss);
The output is the same as above.
Question: How can that work in Java 6 and 7?
In Java 8 and later and on newer Android devices (from API level 26, I’m told) java.time comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.timeto Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
The answer marked as correct has a little mistake,
String myTime = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), // The change is in this line
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
for example this is an example of the value that i get:
417474:44:19
This is the solution to get the right format is:
String myTime = String.format("%02d:%02d:%02d",
//Hours
TimeUnit.MILLISECONDS.toHours(millis) -
TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millis)),
//Minutes
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
//Seconds
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
getting as a result a correct format:
18:44:19
other option to get the format hh:mm:ss is just :
Date myDate = new Date(timeinMillis);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String myTime = formatter.format(myDate);
public String millsToDateFormat(long mills) {
Date date = new Date(mills);
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String dateFormatted = formatter.format(date);
return dateFormatted; //note that it will give you the time in GMT+0
}
Going by Bohemian's answer we need need not use TimeUnit to find a known value.
Much more optimal code would be
String hms = String.format("%02d:%02d:%02d", millisLeft/(3600*1000),
millisLeft/(60*1000) % 60,
millisLeft/1000 % 60);
Hope it helps
String string = String.format("%02d:%02d:%02d.%03d",
TimeUnit.MILLISECONDS.toHours(millisecend), TimeUnit.MILLISECONDS.toMinutes(millisecend) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisecend)),
TimeUnit.MILLISECONDS.toSeconds(millisecend) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisecend)), millisecend - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisecend)));
Format: 00:00:00.000
Example: 615605 Millisecend
00:10:15.605
The code below does the conversion in both way
23:59:58:999 to 86398999
and than
86398999 to 23:59:58:999
import java.util.concurrent.TimeUnit;
public class TimeUtility {
public static void main(String[] args) {
long currentDateTime = System.currentTimeMillis();
String strTest = "23:59:58:999";
System.out.println(strTest);
long l = strToMilli(strTest);
System.out.println(l);
l += 1;
String str = milliToString(l);
System.out.println(str);
}
/**
* convert a time string into the equivalent long milliseconds
*
* #param strTime string fomratted as HH:MM:SS:MSMS i.e. "23:59:59:999"
* #return long integer like 86399999
*/
public static long strToMilli(String strTime) {
long retVal = 0;
String hour = strTime.substring(0, 2);
String min = strTime.substring(3, 5);
String sec = strTime.substring(6, 8);
String milli = strTime.substring(9, 12);
int h = Integer.parseInt(hour);
int m = Integer.parseInt(min);
int s = Integer.parseInt(sec);
int ms = Integer.parseInt(milli);
String strDebug = String.format("%02d:%02d:%02d:%03d", h, m, s, ms);
//System.out.println(strDebug);
long lH = h * 60 * 60 * 1000;
long lM = m * 60 * 1000;
long lS = s * 1000;
retVal = lH + lM + lS + ms;
return retVal;
}
/**
* convert time in milliseconds to the corresponding string, in case of day
* rollover start from scratch 23:59:59:999 + 1 = 00:00:00:000
*
* #param millis the number of milliseconds corresponding to tim i.e.
* 34137999 that can be obtained as follows;
* <p>
* long lH = h * 60 * 60 * 1000; //hour to milli
* <p>
* long lM = m * 60 * 1000; // minute to milli
* <p>
* long lS = s * 1000; //seconds to milli
* <p>
* millis = lH + lM + lS + ms;
* #return a string formatted as HH:MM:SS:MSMS i.e. "23:59:59:999"
*/
private static String milliToString(long millis) {
long hrs = TimeUnit.MILLISECONDS.toHours(millis) % 24;
long min = TimeUnit.MILLISECONDS.toMinutes(millis) % 60;
long sec = TimeUnit.MILLISECONDS.toSeconds(millis) % 60;
//millis = millis - (hrs * 60 * 60 * 1000); //alternative way
//millis = millis - (min * 60 * 1000);
//millis = millis - (sec * 1000);
//long mls = millis ;
long mls = millis % 1000;
String toRet = String.format("%02d:%02d:%02d:%03d", hrs, min, sec, mls);
//System.out.println(toRet);
return toRet;
}
}
I tried as shown in the first answer. It works, but minus brought me into confusion. My answer by Groovy:
import static java.util.concurrent.TimeUnit.*
...
private static String formatElapsedTime(long millis) {
int hrs = MILLISECONDS.toHours(millis) % 24
int min = MILLISECONDS.toMinutes(millis) % 60
int sec = MILLISECONDS.toSeconds(millis) % 60
int mls = millis % 1000
sprintf( '%02d:%02d:%02d (%03d)', [hrs, min, sec, mls])
}
For Kotlin
val hours = String.format("%02d", TimeUnit.MILLISECONDS.toHours(milSecs))
val minutes = String.format("%02d",
TimeUnit.MILLISECONDS.toMinutes(milSecs) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milSecs)))
val seconds = String.format("%02d",
TimeUnit.MILLISECONDS.toSeconds(milSecs) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milSecs)))
where, milSecs is milliseconds
Well, you could try something like this, :
public String getElapsedTimeHoursMinutesSecondsString() {
long elapsedTime = getElapsedTime();
String format = String.format("%%0%dd", 2);
elapsedTime = elapsedTime / 1000;
String seconds = String.format(format, elapsedTime % 60);
String minutes = String.format(format, (elapsedTime % 3600) / 60);
String hours = String.format(format, elapsedTime / 3600);
String time = hours + ":" + minutes + ":" + seconds;
return time;
}
to convert milliseconds to a time value
In kotlin
private fun stringForTime(timeMs: Int): String {
val totalSeconds = timeMs / 1000
val seconds = totalSeconds % 60
val minutes = totalSeconds / 60 % 60
val hours = totalSeconds / 3600
return if (hours > 0) {
"%d:%02d:%02d".format(hours, minutes, seconds)
} else {
"%02d:%02d".format(minutes, seconds)
}
}
In Java
private String stringForTime(int timeMs) {
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = totalSeconds / 60 % 60;
int hours = totalSeconds / 3600;
return hours > 0 ? String.format(Locale.getDefault(),
"%d:%02d:%02d",
hours,
minutes,
seconds) :
String.format(Locale.getDefault(),
"%02d:%02d",
minutes,
seconds);
}
In Kotlin:
fun FUNCTION_NAME(milliSeconds: Long): String {
val s: Long = milliSeconds / 1000 % 60
val m: Long = milliSeconds / (1000*60) % 60
val h: Long = milliSeconds / (1000*60*60) % 24
return String.format("%02d:%02d:%02d", h, m, s)
}