導航:首頁 > 編程語言 > java時分秒轉換秒

java時分秒轉換秒

發布時間:2022-06-02 14:21:00

java如何獲取當前時間 年月日 時分秒

//得到long類型當前時間

longl=System.currentTimeMillis();

//new日期對

Datedate=newDate(l);

//轉換提日期輸出格式

SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-

ddHH:mm:ss");System.out.println(dateFormat.format(date));

(1)java時分秒轉換秒擴展閱讀

package com.ob;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class DateTest {

public static void main(String[] args) throws ParseException {

Calendar now = Calendar.getInstance();

System.out.println("年: " + now.get(Calendar.YEAR));

System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");

System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));

System.out.println("時: " + now.get(Calendar.HOUR_OF_DAY));

System.out.println("分: " + now.get(Calendar.MINUTE));

System.out.println("秒: " + now.get(Calendar.SECOND));

System.out.println("當前時間毫秒數:" + now.getTimeInMillis());

System.out.println(now.getTime());

Date d = new Date();

System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdf.format(d);

System.out.println("格式化後的日期:" + dateNowStr);

String str = "2012-1-13 17:26:33";

//要跟上面sdf定義的格式一樣
Date today = sdf.parse(str);

System.out.println("字元串轉成日期:" + today);
}
}

Ⅱ JAVA怎麼把時間長(秒)轉換成時分秒格式

小時:h=time/3600(整除)
分鍾:m=(time-h*3600)/60 (整除)
秒:s=(time-h*3600) mod 60 (取余)

Ⅲ 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+"秒");

}

}

(3)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 如何把輸入的時分秒轉換再輸出為秒數

import java.util.Scanner;
public class Convert{
public static void main(String[] args){
System.out.println("請輸入時分秒,格式為hh:mi:ss");
Scanner input=new Scanner(System.in);
String s=input.next();
int index1=s.indexOf(":");
int index2=s.indexOf(":",index1+1);
int hh=Integer.parseInt(s.substring(0,index1));
int mi=Integer.parseInt(s.substring(index1+1,index2));
int ss=Integer.parseInt(s.substring(index2+1));
System.out.println(hh*60*60+mi*60+ss);
}
}

Ⅳ 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如何把時間格式轉為毫秒

獲取毫秒數,即long類型的數值,僅能返回自 1970 年 1 月 1 日 00:00:00 GMT 以來的毫秒數。

一樓、二樓的回答就是正確的,不過在使用中還需要根據自身使用環境,直接使用或者進一步按需優化後再使用。

最常使用的就是,把String類型的日期先轉換為Date類型,最後直接調用.getTime()即可,這也是比較方便的了。

還有就是以上提到的Timestamp類中的valueOf(String s) 方法,這里一定要注意,給定的字元串日期型數據必須符合置頂指定格式:yyyy-mm-dd hh:mm:ss[.fffffffff],否則會拋出異常。

PS>

Ⅶ java時間轉換

java中毫秒轉日期:
//毫秒轉換為日期
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long now = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
System.out.println(now + " = " + formatter.format(calendar.getTime()));
// 日期轉換為毫秒 兩個日期想減得到天數
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String start="2011-09-20 12:30:45";
String end ="2011-10-20 6:30:00";
//得到毫秒數
long timeStart=sdf.parse(start).getTime();
long timeEnd =sdf.parse(end).getTime();
//兩個日期想減得到天數
long dayCount= (timeEnd-timeStart)/(24*3600*1000);
System.out.println(dayCount);
}

Ⅷ 用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+"秒";
}
}

閱讀全文

與java時分秒轉換秒相關的資料

熱點內容
音頻採集單片機 瀏覽:590
加密管的優點 瀏覽:280
dock基礎命令 瀏覽:345
java編程愛好者 瀏覽:723
做外包程序員怎麼樣 瀏覽:865
程序員技術門檻 瀏覽:473
路由花生殼搭建web伺服器地址 瀏覽:541
小米傳送文件用什麼app 瀏覽:102
哪個領域演算法好 瀏覽:380
用命令行編譯java 瀏覽:677
筆趣閣app哪個是正版手機app 瀏覽:427
程序員這個工作好嗎 瀏覽:898
agps定位伺服器地址 瀏覽:659
用水做的解壓玩具怎麼做 瀏覽:418
安卓411能下載什麼 瀏覽:304
小海龜logo命令 瀏覽:493
java製作界面 瀏覽:895
台達plc編程電纜製作 瀏覽:249
30多歲當程序員 瀏覽:442
怎樣把表格轉換成pdf 瀏覽:514