① java將時分秒格式的時間轉化成秒數
public class TimeToSecond {
public static void main(String[] args) {
String time ="01:22:12";
String[] my =time.split(":");
int hour =Integer.parseInt(my[0]);
int min =Integer.parseInt(my[1]);
int sec =Integer.parseInt(my[2]);
int zong =hour*3600+min*60+sec;
System.out.println("慶拍納散共"+zong+"秒");
}
}
(1)java時間轉換成秒擴展閱讀
java將毫秒值轉換為譽茄羨日期時間
public static void main(String[] args) {
long milliSecond = 1551798059000L;
Date date = new Date();
date.setTime(milliSecond);
System.out.println(new SimpleDateFormat().format(date));
}
② 用Java編寫一個程序,以小時,分,秒讀取時間長度,然後全部換算成秒並輸出結果.
classYugi{
privatestaticvoidprint(inth,intm,ints){
intss=h*60*60+m*60+s;
System.out.format("%d小時襲%d分%d秒等於%d秒%n",h,m,s,ss);
}
privatestaticvoidprint(ints){
inth=s/60/60;
intm=s/60%60;
intss=s%60;
System.out.format("%d秒等於%d小時%d分%d秒%n",s,h,m,ss);
}
publicstaticvoidmain(String[]args){
print(1,28,42);
print(9999);
}
}
③ Java 將時間轉換成秒
public int returnSeconde(String instr){
String[] a=instr.splite("\\.");
String[] b=a[0].splite(":");
return Integer.valueOf(b[0])*60*60 + Integer.valueOf(b[1])*60 + Integer.valueOf(b[2]);
}
基本上就是這樣了,沒有調試過,也沒做輸入校驗,不過基本思路內沒什麼容錯。
④ java 毫秒轉換時間
時間除以1000轉換成秒,對60取余就是秒數,除以60後再對60取余是分,除以60後再對24取余是小時
⑤ java時間轉換
Java 毫秒轉換為(天:時:分:秒)方法
public static String format(long ms) {
//將毫秒數換算成x天x時x分x秒x毫秒
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = ms / dd;
long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
String strDay = day < 10 ? "0" + day : "" + day;
String strHour = hour < 10 ? "0" + hour : "" + hour;
String strMinute = minute < 10 ? "0" + minute : "" + minute;
String strSecond = second < 10 ? "0" + second : "" + second;
String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;
strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
return strDay + " " + strHour + ":" + strMinute + ":" + strSecond + " " + strMilliSecond;
}
通過此方法可以將毫秒轉換成 天:時:分:秒
也可以對此方法進行稍微的修改就可以轉換成其他格式。
⑥ JAVA怎麼把時間長轉換成時分秒格式
小時:h=time/3600(整除)
分鍾:m=(time-h*3600)/60 (整除)
秒:s=(time-h*3600) mod 60 (取余)
⑦ Java 如何把輸入的時分秒轉換再輸出為秒數
思路就是你把輸入的時分秒轉換為Date對象
Date對象就可以獲取到毫秒數
然後你把毫秒數轉換為秒數就可以了
⑧ java中如何取系統時間精確到秒
1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式 System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間
2 Calendar c = Calendar.getInstance();//可以對每個時間域單獨修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
3 Date nowTime = new Date(System.currentTimeMillis());
SimpleDateFormat
sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String
retStrFormatNowDate = sdFormatter.format(nowTime);