站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题只剩10分了,请问我如何把总秒数换算成时分秒的形式呢?比如把4210秒换成1时10分10秒.

只剩10分了,请问我如何把总秒数换算成时分秒的形式呢?比如把4210秒换成1时10分10秒.

摘要: 只剩10分了,请问我如何把总秒数换算成时分秒的形式呢?比如把4210秒换成1时10分10秒.


如題,急!!


public class FormatTime {

public static void main(String[] args) {
int time=4210;
int hour,minute,second;
hour=time/3600; //int型整除
time=time-3600*hour;//除去整小时数后剩下的时间
minute=time/60;
time=time-60*minute;
second=time;
System.out.println(time+"秒转化后为:"+hour+"时"+minute+"分"+second+"秒");

}
}


Sorry,刚才弄错了,最后输出时time中的值是已经改变过的,应该这么写:

public class FormatTime {

public static void main(String[] args) {
int time=4210;
int hour,minute,second;
hour=time/3600; //int型整除
time=time-3600*hour;//除去整小时数后剩下的时间
minute=time/60;
time=time-60*minute;
second=time;
System.out.println("4210秒转化后为:"+hour+"时"+minute+"分"+second+"秒");

}
}



Decode ( Length (Trunc ( 你的秒数 / 3600 ) ), 1, "0" + Trunc ( 你的秒数 / 3600 ), Trunc ( 你的秒数 / 3600 ) ) + ':' + Decode ( Length ( Trunc ( 你的秒数 / 60 % 60 ) ), 1, "0" + Trunc ( 你的秒数 / 60 % 60 ) , Trunc ( 你的秒数 / 60 % 60 ) ) + ':' + Decode ( Length ( 你的秒数 % 60 % 60 ) , 1 , "0" + 你的秒数 % 60 % 60 , 你的秒数 % 60 % 60 )

不知道你用什么语言,我这个是Brio中脚本的语言,你看能不能用


谢谢各位!~
TO:asmetoyou(),我也是像你这样计算的,不过有时候不准,有时候准,有点郁闷.
//将秒数转换成x时x分x秒显示
private String returnFullTimes(int theSecond) {
int h = 0, m = 0, s = 0;
h = (int)Math.floor(theSecond/3600);
if((theSecond/3600) > 0) {
m = (int)Math.floor((theSecond-h*3600)/60);
if(((theSecond-h*3600)/60) > 0) {
s = theSecond-h*3600-m*60;
}else {
s = theSecond;
}
}else {
m = (int)Math.floor(theSecond/60);
if((theSecond/60) > 0) {
s = theSecond-m*60;
}else {
s = theSecond;
}
}
return h + "时" + m + "分" + s + "秒";
}




是否可能把总秒数转换成日期型的数据呢,因为对日期型来说达到楼主的目的是很容易做到的


TO:blue_summer(),好像不行,因为这个值并不是具体的日期,不能按日期类型来计算,其实我也想这样呀.


long timeInMillis = 123456789;
int hours = (int)Math.floor(timeInMillis/(60*60));
int minutes = (int)Math.floor((timeInMillis %(60*60))/60);
int seconds = (int)(timeInMillis %(60*60))%60;

System.out.println(hours + ":" + minutes + ":" + seconds);


TO:emin_lee() ,这样肯定不对了:)
我之前的方法应该是对的,只是不知道为何有时候会不准,大多数时候是准的,遇到不准时跟客户难以交待,郁闷.
出去溜达,等下回来结帖了,分不多,谢谢大家的回应.


Calendar calendar = new GregorianCalendar();

试一下这个类吧!


呵呵~
试一试看准不准:P


解决了,我上面写的算法,错了个小小的地方,没注意到.完整的代码如下:
private String returnFullTimes(int theSecond) {
int h = 0, m = 0, s = 0;

h = (int)Math.floor(theSecond/3600);
if(h > 0) {
m = (int)Math.floor((theSecond-h*3600)/60);
if(m > 0) {
s = theSecond-h*3600-m*60;
}else {
s = theSecond-h*3600; //原来这里写错了,写成了s = theSecond;
}
}else {
m = (int)Math.floor(theSecond/60);
if(m > 0) {
s = theSecond-m*60;
}else {
s = theSecond;
}
}
return h + "时" + m + "分" + s + "秒";
}

import java.util.Date;

public class Convert {
public static void main(String[] args) {
Date date = new Date(4210 * 1000);
System.out.println(date.getHours() - 8);
System.out.println(date.getMinutes());
System.out.println(date.getSeconds());
}
}


↑返回目录
前一篇: Struts资源文件问题
后一篇: jsp开发了一个网站系统,在运行日志里好多“无法调用 servlet /index.jsp 的 service() 方法”错误?