java把秒轉換成時間
㈠ java 把秒轉作 時:分:秒
public class Test{
public static void main(String[] args){
long a=100;
long hour=a/3600; !小時
long minute=a%3600/60; !分鍾
long second=a%60; !秒
System.out.println(hour+":"+minute+":"+second);
}
}
㈡ java語言寫出:輸入一個秒數。轉換為小時:分:秒的格式輸出。
Java程序:
importjava.util.Scanner;
publicclasstest{
publicstaticvoidmain(String[]args){
Scannerscan=newScanner(System.in);
intnum;
inthour=0;
intminute=0;
intsecond=0;
System.out.print("請輸入秒數:");
num=scan.nextInt();
second=num%60;
num-=second;
if(num>0){
num/=60;
minute=num%60;
num-=minute;
if(num>0){
hour=num/60;
}
}
System.out.printf("%d:%d:%d ",hour,minute,second);
}
}
運行測試:
請輸入秒數:9876
2:44:36
㈢ 用java怎樣把一個以秒為單位的數轉換成時分秒,比如說3670s表示為:1時1分10秒 詳細的答案給全分!
我剛寫的你看看把
public class h
{
public static void main(String args[]){
System.out.print( cal(3670));
}
public static String cal(int second){
int h = 0;
int d = 0;
int s = 0;
int temp = second%3600;
if(second>3600){
h= second/3600;
if(temp!=0){
if(temp>60){
d = temp/60;
if(temp%60!=0){
s = temp%60;
}
}else{
s = temp;
}
}
}else{
d = second/60;
if(second%60!=0){
s = second%60;
}
}
return h+"時"+d+"分"+s+"秒";
}
}