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.
Use the Date() constructor to convert milliseconds to a date, e.g. const date = new Date(timestamp) . The Date() constructor takes an integer value that represents the number of milliseconds since January 1, 1970, 00:00:00 UTC and returns a Date object.
var date = '1475235770601'; var d = new Date(date); var ds = d. toLocaleString(); alert(ds); console. log(ds);
date +"%T. %6N" returns the current time with nanoseconds rounded to the first 6 digits, which is microseconds. date +"%T. %3N" returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With