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

java時間轉換成秒

發布時間:2023-09-30 03:52:02

『壹』 java怎麼把時間長轉換成時分秒格式

小時:h=time/3600(整除)
分鍾:m=(time-h*3600)/褲物60 (整除)
秒圓孝:s=(time-h*3600) mod 60 (取余)橘純稿

『貳』 Java中如何把日期轉換為秒數

Date類有一個getTime()可以換回秒數,例如:

publicclassDateToSecond
{
publicstaticvoidmain(String[]args)
{
Datedate=newDate(System.currentTimeMillis());
System.out.println(date.getTime());
}
}

『叄』 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);

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

//得到long類型當前時間

longl=System.currentTimeMillis();

//new日期對

Datedate=newDate(l);

//轉換提日期輸出格式

SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-

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

(4)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編寫一個程序,鍵入一個以秒為單位的時間長度,然後換算成小時、分和秒的組合表達方式並輸出結果。


publicclassTest{
privatestaticfinalintSECOND_PER_HOUR=60*60;
privatestaticfinalintSECOND_PER_MINUTE=60;

publicstaticvoidmain(String[]args){
longsecond=newScanner(System.in).nextLong();
longhour=second/SECOND_PER_HOUR;
second-=SECOND_PER_HOUR*hour;
intminute=(int)(second/SECOND_PER_MINUTE);
second-=SECOND_PER_MINUTE*minute;
System.out.println(String.format("%d小時%d分鍾%d秒",hour,minute,second));
}

}

『陸』 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+"秒");

}

}

(6)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 毫秒轉換時間

時間除以1000轉換成秒,對60取余就是秒數,除以60後再對60取余是分,除以60後再對24取余是小時

閱讀全文

與java時間轉換成秒相關的資料

熱點內容
程序員怎麼跟男朋友說我愛你 瀏覽:309
單片機頻率變化 瀏覽:428
哪個app可以看賭神 瀏覽:466
rstudiopython 瀏覽:127
團隊如何開發伺服器 瀏覽:440
php選擇資料庫的函數 瀏覽:772
dhcp伺服器新增地址 瀏覽:930
程序員跑三個月外賣 瀏覽:941
linux配置tomcat的jdk路徑 瀏覽:363
液體壓縮公式 瀏覽:777
php開發後台管理系統 瀏覽:360
python二分查找遞歸 瀏覽:447
微信如何發視頻不壓縮 瀏覽:902
河北2021美術高考綜合分演算法 瀏覽:606
如何為電腦文件夾加密 瀏覽:835
電腦自啟動應用命令 瀏覽:690
php判斷一個文件是否存在 瀏覽:829
php導出xml文件 瀏覽:904
7個文件夾解壓 瀏覽:383
python實現機器碼 瀏覽:356