Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 756
Faheem Kalsekar Avatar asked Jun 24 '11 09:06

Faheem Kalsekar


People also ask

How do you convert milliseconds to date and time?

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.

How do you convert milliseconds to dates in typescript?

var date = '1475235770601'; var d = new Date(date); var ds = d. toLocaleString(); alert(ds); console. log(ds);

How do I print the date in milliseconds?

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.


2 Answers

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.

like image 64
Andrzej Doyle Avatar answered Oct 19 '22 16:10

Andrzej Doyle


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
like image 35
OscarRyz Avatar answered Oct 19 '22 14:10

OscarRyz