rab*_*itt 91 java string android bigdecimal android-edittext
我一直在尝试将秒值(在BigDecimal变量中)转换为editText中的字符串,如"1小时22分33秒"或类似的东西.
我试过这个:
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");
Run Code Online (Sandbox Code Playgroud)
(我有一个roundThreeCalc,这是秒的值,所以我尝试在这里转换它.)
hours = (roundThreeCalc.divide(var3600));
myremainder = (roundThreeCalc.remainder(var3600));
minutes = (myremainder.divide(var60));
seconds = (myremainder.remainder(var60));
sequenceCaptureTime = hours.toString() + minutes.toString() + seconds.toString();
Run Code Online (Sandbox Code Playgroud)
然后我将editText设置为sequnceCaptureTime String.但那没用.它每次强制关闭应用程序.我完全超出了我的深度,非常感谢任何帮助.快乐的编码!
Geo*_*its 211
是否有必要使用BigDecimal?如果你没有,我会使用int或long for seconds,它会简化一些事情:
hours = totalSecs / 3600;
minutes = (totalSecs % 3600) / 60;
seconds = totalSecs % 60;
timeString = String.format("%02d:%02d:%02d", hours, minutes, seconds);
Run Code Online (Sandbox Code Playgroud)
您可能想要填充每个以确保它们是字符串中的两位数值(或其他).
Hap*_*ard 61
你应该有更多的运气
hours = roundThreeCalc.divide(var3600, BigDecimal.ROUND_FLOOR);
myremainder = roundThreeCalc.remainder(var3600);
minutes = myremainder.divide(var60, BigDecimal.ROUND_FLOOR);
seconds = myremainder.remainder(var60);
Run Code Online (Sandbox Code Playgroud)
这将在每次除法后删除小数值.
编辑:如果这不起作用,试试这个.(我刚刚编写并测试过它)
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;
}
Run Code Online (Sandbox Code Playgroud)
Rah*_*him 27
这是工作代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
Epi*_*ist 24
import java.time.LocalTime;
private String ConvertSecondToHHMMSSString(int nSecondTime) {
return LocalTime.MIN.plusSeconds(nSecondTime).toString();
}
Run Code Online (Sandbox Code Playgroud)
Ajj*_*jji 19
我更喜欢java内置的TimeUnit库
long seconds = TimeUnit.MINUTES.toSeconds(8);
Run Code Online (Sandbox Code Playgroud)
SKU*_*ULL 12
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;
}
Run Code Online (Sandbox Code Playgroud)
Nic*_*las 10
如果你想要单位h
,min
并且sec
在一段时间内你可以使用这个:
public static 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);
Run Code Online (Sandbox Code Playgroud)
这是很多条件运算符。该方法将返回这些字符串:
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
Run Code Online (Sandbox Code Playgroud)
这是我的简单解决方案:
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);
}
Run Code Online (Sandbox Code Playgroud)
Result: 00:00:36 - 36
Result: 01:00:07 - 3607
Result: 6313 days 12:39:05 - 545488745
Run Code Online (Sandbox Code Playgroud)
因此,我喜欢保持简单:
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);
Run Code Online (Sandbox Code Playgroud)
结果将是:01小时23分钟20秒
BigDecimal secondsValue = BigDecimal.valueOf(4953);
if (secondsValue.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) {
System.out.println("Seconds value " + secondsValue + " is out of range");
} else {
Duration dur = Duration.ofSeconds(secondsValue.longValueExact());
long hours = dur.toHours();
int minutes = dur.toMinutesPart();
int seconds = dur.toSecondsPart();
System.out.format("%d hours %d minutes %d seconds%n", hours, minutes, seconds);
}
Run Code Online (Sandbox Code Playgroud)
该片段的输出是:
1小时22分33秒
如果此代码中存在非零的秒分数,则BigDecimal
该代码将无法正常工作,但您可以对其进行修改。该代码适用于 Java 9 及更高版本。在 Java 8 中,从小时、分钟和秒的转换Duration
有点冗长,请参阅底部的链接了解具体方法。我让您选择单词的正确单数或复数形式(hour或hours等)。
Duration
。这段代码工作正常:
txtTimer.setText(String.format("%02d:%02d:%02d",(SecondsCounter/3600), ((SecondsCounter % 3600)/60), (SecondsCounter % 60)));
Run Code Online (Sandbox Code Playgroud)