1

I want to convert the following datetime do milliseconds :

17/07/2015 13:30

For this I have used the following code:

SimpleDateFormat f = new SimpleDateFormat("dd/mm/yyyy hh:mm");
Date d = null;
try {
    d = f.parse(date);
} catch (ParseException e) {
}
long milliseconds = d.getTime();

But when this date I add to the calendar in Android it seems that the date is 17 of January not 17 of July.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Xhulio
  • 581
  • 7
  • 26
  • 2
    "mm" means minutes - so `dd/mm/yyyy hh:mm` should almost certainly be `dd/MM/yyyy HH:mm`. Note the change from `hh` to `HH` too. See http://codeblog.jonskeet.uk/2015/05/05/common-mistakes-in-datetime-formatting-and-parsing/ – Jon Skeet Jul 17 '15 at 07:28

3 Answers3

4

It should be like this :-

SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy HH:mm"); with MM for months not mm for minutes & HH for 24-Hours instead of hh for am/pm Hours

AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23
1

Change the mm for your month to MM.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
0

I found that you have typo in date format dd/mm/yyyy hh:mm it should be dd/MM/yyyy HH:mm now please have look into my code and 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())
MilapTank
  • 9,988
  • 7
  • 38
  • 53