我正在开发一个Android应用程序,因为我希望从Date-Time获得Millisecond。
我使用下面的代码进行解析,但是我无法得到正确的结果。
public static long getTimeInMillisecond(String time) {
// eg. time = "27 Sep 2016 12:24PM";
final SimpleDateFormat sdf = new SimpleDateFormat(
"dd MMM yyyy hh:mma");
sdf.setTimeZone(TimeZone.getDefault());
try {
Date mDate = sdf.parse(time);
long timeInMilliseconds = mDate.getTime();
return timeInMilliseconds;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
答案 0 :(得分:0)
public static long getDateStringToLong(String str_date) {
DateFormat formatter;
Date date = null;
formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US);
try {
date = (Date) formatter.parse(str_date);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime();
}
根据您的要求更改日期格式 希望这会有所帮助..
答案 1 :(得分:0)
您好,您可以尝试使用此代码..
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String currentDateandTime = sdf.format(new Date());
Log.e("Log","Time----"+currentDateandTime);
答案 2 :(得分:0)
终于得到了解决方案:
使用 SimpleDateFormat 添加 Locale.English 并正常工作。
final SimpleDateFormat sdf = new SimpleDateFormat(
"dd MMM yyyy hh:mma", Locale.ENGLISH);