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
SimpleDateForma开发者_JAVA技巧t format = new SimpleDateFormat("MMM D'\''YY");
but i get an exception:
Caused by: java.lang.IllegalArgumentException: Unterminated quote
Any help would be highly appreciated.
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.
try:
import java.util.*;
import java.text.*;
class D {
public static void main( String ... args ) {
System.out.println(
new SimpleDateFormat("MMM dd''yy")
.format( new Date( 1308700800000L ))
);
}
}
prints:
Jun 21'11
Andrzej is right, but Caps D and Y won't work for you. Read the doc, but that should work:
SimpleDateFormat format = new SimpleDateFormat("MMM d''yy 'at' HH:mm:ss z")
精彩评论