将日期转换为毫秒给我错误的日期 java
Convert date to milliseconds gives me wrong date java
我想将以下日期时间转换为毫秒:
17/07/2015 13:30
为此我使用了以下代码:
SimpleDateFormat f = new SimpleDateFormat("dd/mm/yyyy hh:mm");
Date d = null;
try {
d = f.parse(date);
} catch (ParseException e) {
}
long milliseconds = d.getTime();
但是当我将这个日期添加到 Android 中的日历时,日期似乎是 1 月 17 日而不是 7 月 17 日。
应该是这样的:-
SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy HH:mm");
MM
几个月而不是 mm
分钟 & HH
24 小时而不是 hh
am/pm 小时
将您所在月份的 mm
更改为 MM
。
我发现您的日期格式有错字 dd/mm/yyyy hh:mm
现在应该是 dd/MM/yyyy HH:mm
请查看我的代码 dateformate android
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.ENGLISH);
String input = "17/07/2015 13:30";
Date datestr = formatter.parse(input);
**long date = datestr.getTime();** // what you want
// revers of your Date
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(datestr.getTime());
String output = formatter.format(calendar.getTime())
我想将以下日期时间转换为毫秒:
17/07/2015 13:30
为此我使用了以下代码:
SimpleDateFormat f = new SimpleDateFormat("dd/mm/yyyy hh:mm");
Date d = null;
try {
d = f.parse(date);
} catch (ParseException e) {
}
long milliseconds = d.getTime();
但是当我将这个日期添加到 Android 中的日历时,日期似乎是 1 月 17 日而不是 7 月 17 日。
应该是这样的:-
SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy HH:mm");
MM
几个月而不是 mm
分钟 & HH
24 小时而不是 hh
am/pm 小时
将您所在月份的 mm
更改为 MM
。
我发现您的日期格式有错字 dd/mm/yyyy hh:mm
现在应该是 dd/MM/yyyy HH:mm
请查看我的代码 dateformate android
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.ENGLISH);
String input = "17/07/2015 13:30";
Date datestr = formatter.parse(input);
**long date = datestr.getTime();** // what you want
// revers of your Date
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(datestr.getTime());
String output = formatter.format(calendar.getTime())