Ⅰ java 我獲得單位為毫秒的當前時間,如何轉化成年月日小時分格式
import java.util.*;
import java.text.SimpleDateFormat;
public class test
{
public static void main (String args[])
{
Date d = new Date();
long longtime = d.getTime();
System.out.println(longtime);
//你獲得的是上面的long型數據吧
String time = d.toLocaleString();
//你可以簡單的得到本地化時間,本來就是String類型的就不用轉換了
System.out.println(time);
//也可以自己用SimpleDateFormat這個函數把它變成自己想要的格式,注意需要import java.text.SimpleDateFormat;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf.format(longtime));
}
}
Ⅱ 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如何獲取當前時間 年月日 時分秒
//得到long類型當前時間
longl=System.currentTimeMillis();
//new日期對
Datedate=newDate(l);
//轉換提日期輸出格式
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-
ddHH:mm:ss");System.out.println(dateFormat.format(date));
(3)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中如何獲取毫秒和微秒數
一、獲取毫秒數的代碼:
微秒使用System.nanoTime()方法:如果Java程序需要高精度的計時,如1毫秒或者更小,使用System.nanoTime()方法,可以滿足需求。
(4)java系統當前時間毫秒擴展閱讀:
獲取微秒函數System.nanoTime() 的隱患:
System.currentTimeMillis() 起始時間是基於 1970.1.1 0:00:00 這個確定的時間的,而System.nanoTime()是基於cpu核心的時鍾周期來計時,它的開始時間是不確定的。
但是在多核處理器上,由於每個核心的開始時間不確定,那麼
「long start = System.nanoTime();String ip = Utilities.getIpByUrl(url);long cost = System.nanoTime() - start;」
這段代碼有可能會運行在兩個不同的cpu核心上,從而導致得到的結果完全不符邏輯。
Ⅳ java 怎麼獲取指定時間的毫秒值如(2012-5-5)
DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
try {
java.util.Date dateTime = format.parse("2012-05-05");
long time=dateTime.getTime();
System.out.println("Time:"+time);
} catch (ParseException e) {
e.printStackTrace();
}
時間如果是Date類型直接getTime()就可以...如果是String類型就用上面的代碼轉成Date然後取毫秒值.
Ⅵ 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語言中使用 那System.current.TimeMillis過得當前毫秒數,是從199
從1970年到現在的毫秒數
Integer 在JAVA內用32位表示,因此32位能表示的最大值是2147483647。另外1年365天的總秒數是 31536000,2147483647/31536000 = 68.1,也就是說32位能表示的最長時間是68年,從1970年開始的話,加上68.1,實際最終到2038年01月19日03時14分07秒,便會到 達最大時間,過了這個時間點,所有32位操作系統時間便會變為10000000 00000000 00000000 00000000,算下來也就是1901年12月13日20時45分52秒,這樣便會出現時間回歸的現象,很多軟體便會運行異常了。
到 這里,我想問題的答案已經顯現出來了,那就是:因為用32位來表示時間的最大間隔是68年,而最早出現的UNIX操作系統考慮到計算機產生的年代和應用的 時限綜合取了1970年1月1日作為UNIX TIME的紀元時間(開始時間),至於時間回歸的現象相信隨著64為操作系統的產生逐漸得到解決,因為用64位操作系統可以表示到 292,277,026,596年12月4日15時30分08秒,相信我們的N代子孫,哪怕地球毀滅那天都不用愁不夠用了,因為這個時間已經是千億年以後 了。
Ⅷ java中如何將Timestamp轉換為毫秒數
我寫了一個把當前時間轉換為毫秒數的例子,你參考一下,我這運行沒問題:
package test;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author Administrator
*當前時間轉換為毫秒數
*/
public class DeclareTimer {
public static void main(String[] args) throws ParseException {
//獲取當前時間
Timestamp t = new Timestamp(new Date().getTime());
System.out.println("當前時間:"+t);
//定義時間格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String str = dateFormat.format(t);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
//此處轉換為毫秒數
long millionSeconds = sdf.parse(str).getTime();// 毫秒
System.out.println("毫秒數:"+millionSeconds);
}
}
Ⅸ Java 如何根據現在的時間毫秒來計算時間的公式(不想用原有的類)
特意寫了一個 你看看是不是這個意思
publicstaticvoidmain(String[]args){
inttimezone=8;
longtimeMillis=newDate().getTime();//1970
longtotalSeconds=timeMillis/1000;
totalSeconds+=60*60*timezone;
intsecond=(int)(totalSeconds%60);//秒
longtotalMinutes=totalSeconds/60;
intminute=(int)(totalMinutes%60);//分
longtotalHours=totalMinutes/60;
inthour=(int)(totalHours%24);//時
inttotalDays=(int)(totalHours/24);
int_year=1970;
intyear=_year+totalDays/366;
intmonth=1;
intday=1;
intdiffDays;
booleanleapYear;
while(true){
intdiff=(year-_year)*365;
diff+=(year-1)/4-(_year-1)/4;
diff-=((year-1)/100-(_year-1)/100);
diff+=(year-1)/400-(_year-1)/400;
diffDays=totalDays-diff;
leapYear=(year%4==0)&&(year%100!=0)||(year%400==0);
if(!leapYear&&diffDays<365||leapYear&&diffDays<366){
break;
}else{
year++;
}
}
int[]monthDays;
if(diffDays>=59&&leapYear){
monthDays=newint[]{-1,0,31,60,91,121,152,182,213,244,274,305,335};
}else{
monthDays=newint[]{-1,0,31,59,90,120,151,181,212,243,273,304,334};
}
for(inti=monthDays.length-1;i>=1;i--){
if(diffDays>=monthDays[i]){
month=i;
day=diffDays-monthDays[i]+1;
break;
}
}
System.out.println(year);
System.out.println(month);
System.out.println(day);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
}
Ⅹ 怎麼在java里獲取帶有毫秒的時間
1.
long java.util.Date.getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
represented by this Date object.
如上JDK文檔說,在Date對象上用getTime()獲得自1970年1月1日以來的毫秒數。
2.
System.currentTimeMillis(); 這個方法獲取當前時間的毫秒數。
3.
以下實例代碼把通過毫秒數相減算的目前距2014-10-01 00:00:00的天數。
publicclassTest{
publicstaticvoidmain(String[]args)throwsParseException{
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Stringstart="2014-10-0100:00:00";
//得到毫秒數
longtimeStart=sdf.parse(start).getTime();
longjustNow=System.currentTimeMillis();
//兩個日期想減得到天數
longdayCount=(justNow-timeStart)/(24*3600*1000);
System.out.println(dayCount);
}
}
輸出
25