How to convert TimeStamp to appropriate Date forma

2019-02-18 22:30发布

问题:

This question already has an answer here:

  • Java: How to get current date in ISO 8601 SECOND format 2 answers

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?

回答1:

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


回答2:

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.