Ⅰ 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如何獲取當前小時的毫秒數呢是當前小時,比如2014-03-04 下午16:00:00的毫秒數
大寫的s表示毫秒數
你的這個可以這么寫
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd aHH:mm:ss:SSS");
System.out.println(dateFormatGmt.format(new Date()));
輸出2014-03-04 下午18:13:05:627
這個627就是對應那個SSS,也就是當前毫秒數
Ⅲ 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怎麼獲取當前系統時間 毫秒數
首先獲取當前時間:
java.util.Date nowdate = new java.util.Date();
2/2
然後如果你想時間的格式和你想用的時間格式一致 那麼就要格式化時間了SimpleDateFormat 的包在java.text包下SimpleDateFormat
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //年月日 時分秒
String t = sdf.parse(nowdate);
Ⅳ java中如何獲得當前時間並輸出:時,分,秒,
import java.util.*;
import java.text.*;
public class TimeTest {
///詳細見java.util.SimpleDateFormat;
///java.util.Calendar;
//裡面有get(xxFIELD);方法很方便就獲取了時,分,秒,毫秒等數值
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");//時:分:秒.毫秒
GregorianCalendar gc = new GregorianCalendar();
System.out.println(sdf.format(gc.getTime()));
////////////////增加2分鍾
gc.add(GregorianCalendar.MINUTE, 2);
System.out.println(sdf.format(gc.getTime()));
}
}
Ⅵ java怎麼獲得得系統時間就是那種一串數字的時間(當前秒數),不要格式輸出的
System.out.println(System.currentTimeMillis());
Ⅶ java 中怎麼比較兩個時間相差的秒數
方法:
java 計算兩個時間相差可以用getTime()來獲得兩個時間的毫秒數,可以這樣計算得出兩個時間的秒數的相差如下:
public int calLastedTime(Date startDate) {long a = new Date().getTime();long b = startDate.getTime();int c = (int)((a - b) / 1000);return c;}
(7)java當前時間秒數擴展閱讀:
1、通過Date類來獲取當前時間
Date day=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(day));
2、通過System類中的currentTimeMillis方法來獲取當前時間
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(df.format(System.currentTimeMillis()));
3、通過Calendar類來獲取當前時間
Calendar c = Calendar.getInstance();//可以對每個時間域單獨修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
4、 通過Date類來獲取當前時間
Date date = new Date();
String year = String.format("%tY", date);
String month = String.format("%tB", date);
String day = String.format("%te", date);
System.out.println("今天是:"+year+"-"+month+"-"+day);
Ⅷ java如何獲取當前時間 年月日 時分秒
//得到long類型當前時間
longl=System.currentTimeMillis();
//new日期對
Datedate=newDate(l);
//轉換提日期輸出格式
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-
ddHH:mm:ss");System.out.println(dateFormat.format(date));
(8)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計算當前時間對象與形參傳入時間對象之間間隔秒數的方法
當前時間 Date current=new Date(); current.getTime() 減去 傳入時間對象.getTime() ,得到的是間隔毫秒數,再除以1000,就是間隔秒數。
Ⅹ java輸入時間算秒數
看我滴~~~~~~~~
完全按照樓主所給思路來的:
importjava.util.Scanner;
/**
*Input:
*
*Enteradateinttheformatyyyy/mm/dd;
*
*1999/12/25
*
*Enteratimeintheformathh:mm:ss;
*
*11:03:25
*
*?1)Windows2)Mac
*
*1
*
*Output:
*
*Year:1999Month:12Day:25
*
*Hour:11Minute:3second:25
*
*Totalseconds:39805.0
*
*TheWindowsExceldate/timeis36519.460706018515
*
*EndofProcessing
*
*@authorxiaobo
*
*思路:
*
*1.計算到目前為止的閏年數
*
*leap=1+min(year-?,0)+【(year-?)/4】-【(year-?)/100】+【(year-?+300)/400】
*
*其中【】表示向下取整,?表示1900或1904
*
*2.計算該年到這個月為止的天數
*
*d=【-1.63+(month-1)*30.6】
*
*3.計算總的天數
*
*result=day+(year-?)*365+leap+d
*
*/
publicclassCalculateSeconds{
publicstaticvoidmain(String[]args){
Scannerin=newScanner(System.in);
System.out.println("Enteradateinttheformatyyyy/mm/dd");
Stringymd=in.nextLine();
System.out.println("Enteratimeintheformathh:mm:ss");
Stringhms=in.nextLine();
System.out
.println("?1)Windows2)Mac");
Stringwm=in.nextLine();
String[]arrStr1=ymd.split("/");
String[]arrStr2=hms.split(":");
intyear=Integer.parseInt(arrStr1[0]);
if(wm.equals("1")){
year-=1900;
}elseif(wm.equals("0")){
year-=1904;
}else{
System.err.println("error");
System.exit(-1);
}
intleap=(int)(1+Math.min(year,0)+Math.floor(year/4)
+Math.floor(year/100)+Math.floor((year+300)/400));
intd=(int)Math.floor(-1.63+(Integer.parseInt(arrStr1[1])-1)
*30.6);
intresult=Integer.parseInt(arrStr1[2])+year*365+leap+d;
System.out.println("Year:"+Integer.parseInt(arrStr1[0])+"Month:"
+Integer.parseInt(arrStr1[1])+"Day:"
+Integer.parseInt(arrStr1[2]));
System.out.println("Hour:"+Integer.parseInt(arrStr2[0])+"Minute:"
+Integer.parseInt(arrStr2[1])+"second:"
+Integer.parseInt(arrStr2[2]));
System.out.println("Totalseconds:"
+(Integer.parseInt(arrStr2[0])*3600
+Integer.parseInt(arrStr2[1])*60+Integer
.parseInt(arrStr2[2])));
if(wm.equals("1")){
System.out.print("TheWindowsExceldate/timeis");
}else{
System.out.print("TheMacExceldate/timeis");
}
System.out.println(result+(Integer.parseInt(arrStr2[0])*3600
+Integer.parseInt(arrStr2[1])*60+Integer
.parseInt(arrStr2[2]))/(24*3600.0));
System.out.println("EndofProcessing");
}
}
結果如圖: