Convert seconds to hour minutes?

I am trying to convert the value of seconds (in a BigDecimal variable) to a string in editText, like "1 hour 22 minutes 33 seconds" or something like that.

I tried this:

String sequenceCaptureTime = ""; BigDecimal roundThreeCalc = new BigDecimal("0"); BigDecimal hours = new BigDecimal("0"); BigDecimal myremainder = new BigDecimal("0"); BigDecimal minutes = new BigDecimal("0"); BigDecimal seconds = new BigDecimal("0"); BigDecimal var3600 = new BigDecimal("3600"); BigDecimal var60 = new BigDecimal("60"); 

(I have roundThreeCalc, which is the value in seconds, so I'm trying to convert it here.)

 hours = (roundThreeCalc.divide(var3600)); myremainder = (roundThreeCalc.remainder(var3600)); minutes = (myremainder.divide(var60)); seconds = (myremainder.remainder(var60)); sequenceCaptureTime = hours.toString() + minutes.toString() + seconds.toString(); 

Then I set editText to sequnceCaptureTime String. But that did not work. This forces the application to close each time. I am completely beside myself from this depth, any help is greatly appreciated. Happy coding!

+60
java android string android-edittext bigdecimal
May 25 '11 at 2:32
source share
16 answers

You should have more luck with

 hours = roundThreeCalc.divide(var3600, BigDecimal.ROUND_FLOOR); myremainder = roundThreeCalc.remainder(var3600); minutes = myremainder.divide(var60, BigDecimal.ROUND_FLOOR); seconds = myremainder.remainder(var60); 

This will reduce the decimal values ​​after each division.

Edit: if this did not work, try this. (I just wrote and tested it)

 public static int[] splitToComponentTimes(BigDecimal biggy) { long longVal = biggy.longValue(); int hours = (int) longVal / 3600; int remainder = (int) longVal - hours * 3600; int mins = remainder / 60; remainder = remainder - mins * 60; int secs = remainder; int[] ints = {hours , mins , secs}; return ints; } 
+48
May 25 '11 at 2:39
source share

Do I need to use BigDecimal? If you do not need this, I would use int or for a long time for several seconds, and this would simplify things a bit:

 hours = totalSecs / 3600; minutes = (totalSecs % 3600) / 60; seconds = totalSecs % 60; timeString = String.format("%02d:%02d:%02d", hours, minutes, seconds); 

You may want to pave each one to make sure that they are two-digit values ​​(or something else) in the string.

+142
May 25 '11 at 2:44
source share

DateUtils.formatElapsedTime(long) , formats the elapsed time in the form of " MM:SS " or " H:MM:SS ". It returns the string you are looking for. You can find the documentation here.

+40
Oct 13 '15 at 14:15
source share

Here is the working code:

 private String getDurationString(int seconds) { int hours = seconds / 3600; int minutes = (seconds % 3600) / 60; seconds = seconds % 60; return twoDigitString(hours) + " : " + twoDigitString(minutes) + " : " + twoDigitString(seconds); } private String twoDigitString(int number) { if (number == 0) { return "00"; } if (number / 10 == 0) { return "0" + number; } return String.valueOf(number); } 
+23
Dec 22 '12 at 12:28
source share

Something really useful in Java 8

 import java.time.LocalTime; private String ConvertSecondToHHMMSSString(int nSecondTime) { return LocalTime.MIN.plusSeconds(nSecondTime).toString(); } 
+15
Jun 24 '14 at 11:17
source share
 private String ConvertSecondToHHMMString(int secondtTime) { TimeZone tz = TimeZone.getTimeZone("UTC"); SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); df.setTimeZone(tz); String time = df.format(new Date(secondtTime*1000L)); return time; } 
+11
May 27 '14 at 11:21
source share

I prefer java built into the TimeUnit library

 long seconds = TimeUnit.MINUTES.toSeconds(8); 
+7
Mar 13 '17 at 10:30
source share

This is my simple solution:

 String secToTime(int sec) { int seconds = sec % 60; int minutes = sec / 60; if (minutes >= 60) { int hours = minutes / 60; minutes %= 60; if( hours >= 24) { int days = hours / 24; return String.format("%d days %02d:%02d:%02d", days,hours%24, minutes, seconds); } return String.format("%02d:%02d:%02d", hours, minutes, seconds); } return String.format("00:%02d:%02d", minutes, seconds); } 

Test Results :

 Result: 00:00:36 - 36 Result: 01:00:07 - 3607 Result: 6313 days 12:39:05 - 545488745 
+3
Jan 10 '17 at 7:08
source share

Here is my function to solve the problem:

 public static String getConvertedTime(double time){ double h,m,s,mil; mil = time % 1000; s = time/1000; m = s/60; h = m/60; s = s % 60; m = m % 60; h = h % 24; return ((int)h < 10 ? "0"+String.valueOf((int)h) : String.valueOf((int)h))+":"+((int)m < 10 ? "0"+String.valueOf((int)m) : String.valueOf((int)m)) +":"+((int)s < 10 ? "0"+String.valueOf((int)s) : String.valueOf((int)s)) +":"+((int)mil > 100 ? String.valueOf((int)mil) : (int)mil > 9 ? "0"+String.valueOf((int)mil) : "00"+String.valueOf((int)mil)); } 
+2
Nov 15 '12 at 17:06
source share

I use this:

  public String SEG2HOR( long lnValue) { //OK String lcStr = "00:00:00"; String lcSign = (lnValue>=0 ? " " : "-"); lnValue = lnValue * (lnValue>=0 ? 1 : -1); if (lnValue>0) { long lnHor = (lnValue/3600); long lnHor1 = (lnValue % 3600); long lnMin = (lnHor1/60); long lnSec = (lnHor1 % 60); lcStr = lcSign + ( lnHor < 10 ? "0": "") + String.valueOf(lnHor) +":"+ ( lnMin < 10 ? "0": "") + String.valueOf(lnMin) +":"+ ( lnSec < 10 ? "0": "") + String.valueOf(lnSec) ; } return lcStr; } 
+1
May 23 '13 at 22:23
source share

I know this is pretty old, but in java 8:

LocalTime.MIN.plusSeconds (120) .format (DateTimeFormatter.ISO_LOCAL_TIME)

+1
Feb 10 '17 at 16:19
source share

I like to keep things simple, so:

  int tot_seconds = 5000; int hours = tot_seconds / 3600; int minutes = (tot_seconds % 3600) / 60; int seconds = tot_seconds % 60; String timeString = String.format("%02d Hour %02d Minutes %02d Seconds ", hours, minutes, seconds); System.out.println(timeString); 

The result will be: 01 hour 23 minutes 20 seconds

+1
Nov 02 '17 at 4:49 on
source share

If you want the units h , min and sec last, you can use this:

 String convertSeconds(int seconds) int h = seconds/ 3600; int m = (seconds % 3600) / 60; int s = seconds % 60; String sh = (h > 0 ? String.valueOf(h) + " " + "h" : ""); String sm = (m < 10 && m > 0 && h > 0 ? "0" : "") + (m > 0 ? (h > 0 && s == 0 ? String.valueOf(m) : String.valueOf(m) + " " + "min") : ""); String ss = (s == 0 && (h > 0 || m > 0) ? "" : (s < 10 && (h > 0 || m > 0) ? "0" : "") + String.valueOf(s) + " " + "sec"); return sh + (h > 0 ? " " : "") + sm + (m > 0 ? " " : "") + ss; } int seconds = 3661; String duration = convertSeconds(seconds); 

These are many conditional statements. The method will return these lines:

 0 -> 0 sec 5 -> 5 sec 60 -> 1 min 65 -> 1 min 05 sec 3600 -> 1 h 3601 -> 1 h 01 sec 3660 -> 1 h 01 3661 -> 1 h 01 min 01 sec 108000 -> 30 h 
0
Dec 23 '16 at 10:25
source share

I use this in python to convert a float representing seconds to hours, minutes, seconds and microseconds. It is elegant enough and convenient to convert to datetime type via strptime for conversion. It can also be easily extended to longer intervals (weeks, months, etc.), if necessary.

  def sectohmsus(seconds): x = seconds hmsus = [] for i in [3600, 60, 1]: # seconds in a hour, minute, and second hmsus.append(int(x / i)) x %= i hmsus.append(int(round(x * 1000000))) # microseconds return hmsus # hours, minutes, seconds, microsecond 
0
Jul 04 '17 at 16:07
source share

I tried a better way and less code, but maybe it’s a little hard to understand how I wrote my code, but if you are good at math, it’s so simple

 import java.util.Scanner; 

watch class {

 public static void main(String[] args) { Scanner input = new Scanner(System.in); double s; System.out.println("how many second you have "); s =input.nextInt(); double h=s/3600; int h2=(int)h; double h_h2=h-h2; double m=h_h2*60; int m1=(int)m; double m_m1=m-m1; double m_m1s=m_m1*60; System.out.println(h2+" hours:"+m1+" Minutes:"+Math.round(m_m1s)+" seconds"); } 

}

more that's for sure!

0
Nov 10 '17 at 22:20
source share

A nice and easy way to do this with GregorianCalendar

Import them into the project:

 import java.util.GregorianCalendar; import java.util.Date; import java.text.SimpleDateFormat; import java.util.Scanner; 

And then:

  Scanner s = new Scanner(System.in); System.out.println("Seconds: "); int secs = s.nextInt(); GregorianCalendar cal = new GregorianCalendar(0,0,0,0,0,secs); Date dNow = cal.getTime(); SimpleDateFormat ft = new SimpleDateFormat("HH 'hours' mm 'minutes' ss 'seconds'"); System.out.println("Your time: " + ft.format(dNow)); 
0
Nov 27 '17 at 20:10
source share



All Articles