Search code examples
javadatetimedatetime-formatsimpledateformatmilliseconds

Custom Date and Time String from milliseconds


I have time in milliseconds for ex. 1308700800000; I need to convert it to something like Jun 9'11 at 02:15 PM.

I tried using

SimpleDateFormat format = new SimpleDateFormat("MMM D'\''YY");

but i get an exception:

Caused by: java.lang.IllegalArgumentException: Unterminated quote

Any help would be highly appreciated.


Solution

  • It's clear from the exception message that the problem is going to lie with your format string, in particular around the single quote part.

    Looking at the documentation, we can see that:

    Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote.

    Thus I believe your format (for that date part, as per your existing example) can be as simple as

    new SimpleDateFormat("MMM d''yy")
    

    There should be no need to get backslashes involved.