本文介紹了Android/Java 將 String 日期轉換為 long 類型的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我需要將格式為dd/mm/yyyy"的字符串轉換為長類型.為了將值傳遞給 android.calendarProvider.
I need to convert a string in the format "dd/mm/yyyy", to a long type. In order to pass the value to the calendarProvider in android.
目前我有:
Calendar calendar = Calendar.getInstance();
long startEndDate = 0;
Calendar currentDateInfo = Calendar.getInstance();
currentDateInfo.set(calendar.get(Calendar.YEAR), calendar.SEPTEMBER, calendar.get(Calendar.DAY_OF_MONTH));
startEndDate = currentDateInfo.getTimeInMillis();
我需要:
long startDate = *Some sort of conversion* EditText.getText();
我已嘗試使用 SimpleDateFormat,但在獲取正確類型時遇到問題.任何幫助將非常感激.
I've tried using SimpleDateFormat but i'm having problems getting the correct type back. Any help would be much appreciated.
推薦答案
您可以使用以下代碼從字符串日期獲取長值(自 1970 年 1 月 1 日 00:00:00 GMT 以來的毫秒數),格式為"dd/mm/yyyy".
You can use the following code to get a long value (milliseconds since January 1, 1970, 00:00:00 GMT) from a String date with the format "dd/mm/yyyy".
try {
String dateString = "30/09/2014";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = sdf.parse(dateString);
long startDate = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
這篇關于Android/Java 將 String 日期轉換為 long 類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!