[英]Android / Java convert String date to long type
I need to convert a string in the format "dd/mm/yyyy", to a long type. 我需要将格式为“dd / mm / yyyy”的字符串转换为long类型。 In order to pass the value to the calendarProvider in android.
为了将值传递给android中的calendarProvider。
Currently I have: 目前我有:
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();
I need: 我需要:
long startDate = *Some sort of conversion* EditText.getText();
I've tried using SimpleDateFormat but i'm having problems getting the correct type back. 我已经尝试过使用SimpleDateFormat但是我在回到正确的类型时遇到了问题。 Any help would be much appreciated.
任何帮助将非常感激。
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". 您可以使用以下代码从格式为“dd / mm / yyyy”的字符串日期获取长值(自1970年1月1日,格林威治标准时间00:00:00起的毫秒)。
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();
}
Use like this your code 像这样使用你的代码
String[] dateArray = dateString.split("-");
int year = Integer.parseInt(dateArray[0]);
int month = Integer.parseInt(dateArray[1]);
int date = Integer.parseInt(dateArray[2]);
GregorianCalendar gc = new GregorianCalendar(year,month,date);
long timeStamp = gc.getTimeInMillies();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.