Search code examples
androidstringdatetimemilliseconds

String date to milliseconds


I have a date String (format: dd-mmm-yyyy) and I would like to convert it into milliseconds, but it has to have the time zone of the device.

I've already tried this, but unfortunetly the "f.parse("17-July-2018");" is not working right.

SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); //Sample "17-July-2018"

            try {
                Date d = f.parse("17-July-2018");
                bonusTime = d.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }

Solution

  • The parsing format for full month names is MMMM

    SimpleDateFormat f = new SimpleDateFormat("dd-MMMM-yyyy", Locale.ENGLISH);
    

    should work. MMM is for short names like "Jan" or "Feb".