将日期转换为毫秒给我错误的日期java

时间:2015-07-17 07:26:45

标签: java android datetime converter simpledateformat

我想将以下日期时间转换为毫秒:

  

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日。

3 个答案:

答案 0 :(得分:4)

应该是这样的: -

SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy HH:mm");MM合并了几个月mm分钟& HH为24小时而不是hh上午/下午

答案 1 :(得分:1)

将您所在月份的getInput(&input, fileName)更改为mm

答案 2 :(得分:0)

我发现您的日期格式为dd/mm/yyyy hh:mm,它应该是dd/MM/yyyy HH:mm现在请查看我的代码,dateformate for 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())