售樓處咨詢電話(工作時間:)
樓盤地址: | 售樓處地址: | ||
---|---|---|---|
總占地面積: | ㎡ | 總建筑面積: | ㎡ |
所在區域: | 建筑類型: | ||
開發商: | 裝修情況: | ||
物業公司: | 物業管理費: | ||
綠化率: | % | 容積率: | % |
景觀設計: | 暫無 | 車位數: | 暫無 |
車位費: | 待定 | 項目網站: | 暫無 |
售樓電話: | 預售許可證: |
我寫了一個把當前時間轉換為毫秒數的例子,你參考一下,我這運行沒問題:
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);
}
}
public class TestTime { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日"); try { Date d = sdf.parse("2013年1月6日"); sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sdf.format(d)); System.out.println(d.getTime()); } catch (ParseException e) { e.printStackTrace(); } }}。
獲取毫秒數,即long類型的數值,僅能返回自 1970 年 1 月 1 日 00:00:00 GMT 以來的毫秒數。
一樓、二樓的回答就是正確的,不過在使用中還需要根據自身使用環境,直接使用或者進一步按需優化后再使用。
最常使用的就是,把String類型的日期先轉換為Date類型,最后直接調用.getTime()即可,這也是比較方便的了。
還有就是以上提到的Timestamp類中的valueOf(String s) 方法,這里一定要注意,給定的字符串日期型數據必須符合置頂指定格式:yyyy-mm-dd hh:mm:ss[.fffffffff],否則會拋出異常。
PS>
首先獲取當前時間:
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);
一、獲取毫秒數的代碼:
(1)System.currentTimeMillis() 這種方式速度最快。
(2)Calendar.getInstance().getTimeInMillis() 這種方式速度最慢。
二、獲取微秒數的代碼:
微秒使用System.nanoTime()方法:如果Java程序需要高精度的計時,如1毫秒或者更小,使用System.nanoTime()方法,可以滿足需求。
擴展資料:
獲取微秒函數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核心上,從而導致得到的結果完全不符邏輯。
這個的話:
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Cat {
public static void main(String[] args) throws ParseException {
String str = "201104141302";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
long millionSeconds = sdf.parse(str).getTime();//毫秒
System.out.println(millionSeconds);
}
}
輸出結果就是:1302757320000
大寫的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,也就是當前毫秒數
import java.util.Calendar;
public class Time {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar c = Calendar.getInstance();
long a = c.getTimeInMillis();
System.out.println(a);
}
}
我寫了一個把當前時間轉換為毫秒數的例子,你參考一下,我這運行沒問題:
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);
}
}
public class TestTime { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日"); try { Date d = sdf.parse("2013年1月6日"); sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sdf.format(d)); System.out.println(d.getTime()); } catch (ParseException e) { e.printStackTrace(); } }}。
獲取毫秒數,即long類型的數值,僅能返回自 1970 年 1 月 1 日 00:00:00 GMT 以來的毫秒數。
一樓、二樓的回答就是正確的,不過在使用中還需要根據自身使用環境,直接使用或者進一步按需優化后再使用。
最常使用的就是,把String類型的日期先轉換為Date類型,最后直接調用.getTime()即可,這也是比較方便的了。
還有就是以上提到的Timestamp類中的valueOf(String s) 方法,這里一定要注意,給定的字符串日期型數據必須符合置頂指定格式:yyyy-mm-dd hh:mm:ss[.fffffffff],否則會拋出異常。
PS>
首先獲取當前時間:
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);
一、獲取毫秒數的代碼:
(1)System.currentTimeMillis() 這種方式速度最快。
(2)Calendar.getInstance().getTimeInMillis() 這種方式速度最慢。
二、獲取微秒數的代碼:
微秒使用System.nanoTime()方法:如果Java程序需要高精度的計時,如1毫秒或者更小,使用System.nanoTime()方法,可以滿足需求。
擴展資料:
獲取微秒函數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核心上,從而導致得到的結果完全不符邏輯。
這個的話:
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Cat {
public static void main(String[] args) throws ParseException {
String str = "201104141302";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
long millionSeconds = sdf.parse(str).getTime();//毫秒
System.out.println(millionSeconds);
}
}
輸出結果就是:1302757320000
大寫的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,也就是當前毫秒數
import java.util.Calendar;
public class Time {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar c = Calendar.getInstance();
long a = c.getTimeInMillis();
System.out.println(a);
}
}
我寫了一個把當前時間轉換為毫秒數的例子,你參考一下,我這運行沒問題:
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);
}
}
public class TestTime { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日"); try { Date d = sdf.parse("2013年1月6日"); sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sdf.format(d)); System.out.println(d.getTime()); } catch (ParseException e) { e.printStackTrace(); } }}。
獲取毫秒數,即long類型的數值,僅能返回自 1970 年 1 月 1 日 00:00:00 GMT 以來的毫秒數。
一樓、二樓的回答就是正確的,不過在使用中還需要根據自身使用環境,直接使用或者進一步按需優化后再使用。
最常使用的就是,把String類型的日期先轉換為Date類型,最后直接調用.getTime()即可,這也是比較方便的了。
還有就是以上提到的Timestamp類中的valueOf(String s) 方法,這里一定要注意,給定的字符串日期型數據必須符合置頂指定格式:yyyy-mm-dd hh:mm:ss[.fffffffff],否則會拋出異常。
PS>
首先獲取當前時間:
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);
一、獲取毫秒數的代碼:
(1)System.currentTimeMillis() 這種方式速度最快。
(2)Calendar.getInstance().getTimeInMillis() 這種方式速度最慢。
二、獲取微秒數的代碼:
微秒使用System.nanoTime()方法:如果Java程序需要高精度的計時,如1毫秒或者更小,使用System.nanoTime()方法,可以滿足需求。
擴展資料:
獲取微秒函數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核心上,從而導致得到的結果完全不符邏輯。
這個的話:
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Cat {
public static void main(String[] args) throws ParseException {
String str = "201104141302";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
long millionSeconds = sdf.parse(str).getTime();//毫秒
System.out.println(millionSeconds);
}
}
輸出結果就是:1302757320000
大寫的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,也就是當前毫秒數
import java.util.Calendar;
public class Time {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar c = Calendar.getInstance();
long a = c.getTimeInMillis();
System.out.println(a);
}
}