从字符串中解析日期以获取毫秒
Parse date from string to get milliseconds
我试图从包含日期的字符串中获取毫秒数,但我似乎得到了错误的值。这是我试图解析的字符串:2015-03-01 00:00:00
,
我这样做是为了解析它:
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date inputDate = dateFormat.parse(data.get(position).getValidTo());
Log.d("--", inputDate.getDay() + " | " + inputDate.getMonth());
使用 "MM"
而不是 "mm"
来获取月份。 ("mm"代表分钟)
而inputDate.getTime()
将给出以毫秒为单位的时间。
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date inputDate = dateFormat.parse("2014-10-12 12:00:00");
System.out.println(inputDate.getTime());
如果你添加依赖项,我会使用 JodaTime 来做任何与日期相关的事情。
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
并使用这个
String input = "2015-03-01 00:00:00";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dt = DateTime.parse(input, formatter);
System.out.println(dt.getDayOfWeek());
System.out.println(dt.getMonthOfYear());
我试图从包含日期的字符串中获取毫秒数,但我似乎得到了错误的值。这是我试图解析的字符串:2015-03-01 00:00:00
,
我这样做是为了解析它:
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date inputDate = dateFormat.parse(data.get(position).getValidTo());
Log.d("--", inputDate.getDay() + " | " + inputDate.getMonth());
使用 "MM"
而不是 "mm"
来获取月份。 ("mm"代表分钟)
而inputDate.getTime()
将给出以毫秒为单位的时间。
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date inputDate = dateFormat.parse("2014-10-12 12:00:00");
System.out.println(inputDate.getTime());
如果你添加依赖项,我会使用 JodaTime 来做任何与日期相关的事情。
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
并使用这个
String input = "2015-03-01 00:00:00";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dt = DateTime.parse(input, formatter);
System.out.println(dt.getDayOfWeek());
System.out.println(dt.getMonthOfYear());