Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert TimeStamp to appropriate Date format? [duplicate]

I have the TimeStamp and I need to convert it to Data type object that should match this pattern - "2016-11-16T18:42:33.049Z". How can I do that?

like image 677
neckobik Avatar asked Dec 14 '16 13:12

neckobik


2 Answers

Convert like this.

String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000));

you can also use the data object in the manner u want.

like image 199
Vishesh Avatar answered Nov 01 '22 05:11

Vishesh


Date d = new Date((long)timestamp*1000);

will create a Date instance. Displaying it later is another thing.

I think it's what you want:

DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
System.out.println(f.format(date));

Test:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) throws Exception {

        Date d = new Date((long)1481723817*1000);
        DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
        System.out.println(f.format(d));
    }
}

>>2016-12-14T14:56:57.056Z
like image 36
xenteros Avatar answered Nov 01 '22 05:11

xenteros