我一直在尝试将秒值(在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");
(我有一个roundThreeCalc,这是以秒为单位的值,所以我尝试在这里转换它。)
hours = (roundThreeCalc.divide(var3600));
myremainder = (roundThreeCalc.remainder(var3600));
minutes = (myremainder.divide(var60));
seconds = (myremainder.remainder(var60));
sequenceCaptureTime = hours.toString() + minutes.toString() + seconds.toString();
然后我将editText设置为sequnceCaptureTime String。 但那没用。它每次强制关闭应用程序。我完全超出了我的深度,非常感谢任何帮助。快乐的编码!
答案 0 :(得分:198)
是否有必要使用BigDecimal?如果你不需要,我会使用一个int或long来做几秒钟,它会简化一些事情:
hours = totalSecs / 3600;
minutes = (totalSecs % 3600) / 60;
seconds = totalSecs % 60;
timeString = String.format("%02d:%02d:%02d", hours, minutes, seconds);
你可能想要填充每一个,以确保它们是字符串中的两位数值(或其他)。
答案 1 :(得分:62)
DateUtils.formatElapsedTime(long)
,格式化表单中的已用时间" MM:SS
"或" H:MM:SS
" 。它返回您要查找的String。您可以找到文档here
答案 2 :(得分:60)
你应该有更多运气
hours = roundThreeCalc.divide(var3600, BigDecimal.ROUND_FLOOR);
myremainder = roundThreeCalc.remainder(var3600);
minutes = myremainder.divide(var60, BigDecimal.ROUND_FLOOR);
seconds = myremainder.remainder(var60);
这将在每次除法后删除小数值。
编辑:如果这不起作用,请尝试此操作。 (我刚刚编写并测试过它)
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;
}
答案 3 :(得分:25)
以下是工作代码:
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);
}
答案 4 :(得分:24)
import java.time.LocalTime;
private String ConvertSecondToHHMMSSString(int nSecondTime) {
return LocalTime.MIN.plusSeconds(nSecondTime).toString();
}
答案 5 :(得分:14)
我更喜欢java内置的 TimeUnit 库
long seconds = TimeUnit.MINUTES.toSeconds(8);
答案 6 :(得分:11)
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;
}
答案 7 :(得分:8)
这是我的简单解决方案:
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);
}
Result: 00:00:36 - 36
Result: 01:00:07 - 3607
Result: 6313 days 12:39:05 - 545488745
答案 8 :(得分:3)
本规范工作正常:
txtTimer.setText(String.format("%02d:%02d:%02d",(SecondsCounter/3600), ((SecondsCounter % 3600)/60), (SecondsCounter % 60)));
答案 9 :(得分:3)
因此我喜欢简单易懂:
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);
结果将是:01小时23分20秒
答案 10 :(得分:3)
使用GregorianCalendar
执行此操作的一种简单方法将这些导入项目:
import java.util.GregorianCalendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Scanner;
然后:
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));
答案 11 :(得分:2)
这是我解决问题的功能:
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));
}
答案 12 :(得分:1)
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);
}
此代码段的输出为:
1小时22分33秒
如果BigDecimal
中的秒数不为零,那么此代码将无法正常运行,但是您可以对其进行修改。该代码可在Java 9及更高版本中使用。在Java 8中,从Duration
到小时分钟和秒的转换比较罗word,有关如何操作,请参见底部的链接。我要请您选择单词的正确单数或复数形式( hour 或 hours 等)。
Duration
到几天,几小时,几分和几秒的转换。答案 13 :(得分:1)
使用Java 8,您可以轻松地从很长的几秒钟开始以String格式实现时间,
..NbThemeModule.forRoot(
{
name: 'corporate', // change here it can be default, corporate, cosmic or dark
},
[ DEFAULT_THEME, COSMIC_THEME, CORPORATE_THEME, DARK_THEME ],
).provider
这里,给定值是最大允许转换(最多24小时),结果将是
LocalTime.ofSecondOfDay(86399L)
优点:1)无需手动转换并为单个数字附加0
缺点:最多只能使用24小时
答案 14 :(得分:1)
仅需几分钟和几秒钟即可使用
String.format("%02d:%02d", (seconds / 3600 * 60 + ((seconds % 3600) / 60)), (seconds % 60))
答案 15 :(得分:1)
我知道这很老了但是在java 8中:
LocalTime.MIN.plusSeconds(120).format(DateTimeFormatter.ISO_LOCAL_TIME)
答案 16 :(得分:1)
如果您希望单位h
,min
和sec
持续一段时间,可以使用此单位:
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);
这是很多条件运算符。该方法将返回这些字符串:
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
答案 17 :(得分:1)
我用这个:
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;
}
答案 18 :(得分:0)
我尝试过最好的方法和更少的代码,但可能有点难以理解我是如何编写代码的,但如果你擅长数学就很容易
import java.util.Scanner;
课时{/ p>
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");
}
}
更多它是准确的!
答案 19 :(得分:0)
我在python中使用它将表示秒的浮点数转换为小时,分钟,秒和微秒。它相当优雅,通过strptime转换为日期时间类型很方便。如果需要,它也可以很容易地扩展到更长的间隔(数周,数月等)。
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
答案 20 :(得分:0)
使用Java 8,您可以轻松地从很长的几秒钟开始以LocalTime格式获取时间,
LocalTime.ofSecondOfDay(86399L)
这里,给定值是最大允许转换(最多24小时),结果将是
23:59:59
优点:1)无需手动转换并为单个数字附加0
缺点:最多只能使用24小时