本文介紹了在Android中將字符串日期轉換為時間戳?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想轉換我的日期(字符串格式),例如2011 年 9 月 13 日,進入時間戳.我使用了下面的代碼,但我得到了 2011-09-13 00:00:00.0因此.但我想要像 1312828200000 格式的時間戳.
I want to convert my date (which is in String format), e.g. 13-09-2011, into Timestamp. I used below code but I got the 2011-09-13 00:00:00.0 as a result. But I want Timestamp like,1312828200000 format.
我不明白如何轉換它.
我的代碼:
String str_date="13-09-2011";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MM-yyyy");
date = (Date)formatter.parse(str_date);
java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
System.out.println("Today is " +timeStampDate);
推薦答案
如果你使用 Date
對象的 getTime()
你會得到毫秒的時間.無需使用 Timestamp
即可獲得結果.
If you use getTime()
of Date
object you will get time in millisecond.
No need to use Timestamp
to get your result.
String str_date="13-09-2011";
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = (Date)formatter.parse(str_date);
System.out.println("Today is " +date.getTime());
上面的代碼會打印出你需要的類似 1312828200000
的東西,這是長值.
The above code will print something like 1312828200000
you need and this is long value.
這篇關于在Android中將字符串日期轉換為時間戳?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!