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?
Asked
Active
Viewed 6.0k times
9

neckobik
- 307
- 1
- 6
- 13
-
1What is "the `TimeStamp`"? Is it a `java.sql.Timestamp`, or something else? – Mark Rotteveel Dec 14 '16 at 16:26
2 Answers
17
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

xenteros
- 15,586
- 12
- 56
- 91
-
I need date to perfectly match the pattern cause I'm using it for query to dynamoDB... Do you know how to achieve that? – neckobik Dec 14 '16 at 13:54
-
You should keep timestamp in DynamoDB. Date is Date and don't worry abut the display format. – xenteros Dec 14 '16 at 13:55
-
-
actually I need it to make condition for querying secondary index in aws dynamoDB. that is why i need exact same representation as i mentioned before. Case query will compare everything as Strings (Date attribute in Dynamo db is a String value). But ty for your answer any ways. I will test it now. – neckobik Dec 14 '16 at 14:00
-
I think you intended your format to be `"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"` ending in milliseconds: https://docs.oracle.com/javase/9/docs/api/java/text/SimpleDateFormat.html – Kyr Apr 03 '18 at 17:11