2

I got some values

"createTime": 1527217399000,
"updateTime": 1527218049000,

"createTime": 1527217399000,
"updateTime": 1527217954000,

But I can not parse them to date format, I'm not sure if there is something wrong with the data format,is there any method to parse them?

SimpleDateFormat format =  new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Long tempTime = 1527217399000L;
Date createDate=format.parse(tempTime.toString());
    System.out.println(createDate.toString());

Exception in thread "main" java.text.ParseException: Unparseable date: 
"1527217399000"
at java.text.DateFormat.parse(DateFormat.java:366)                                                      
Vikki
  • 1,897
  • 1
  • 17
  • 24
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 28 '18 at 07:07
  • guys, I fixed it by transferring the Timestamp to Date first – Vikki May 28 '18 at 07:08
  • You shouldn’t post the solution in the question, rather than as an answer, please. You may even accept your own answer (after a while). We can find our way around here easier when the question is where the question belongs and the answers where the answers belong. – Ole V.V. May 28 '18 at 07:10

3 Answers3

4

If you want to create a Date instance from a long value, use:

Date createDate=new Date(tempTime);

As the Javadoc mentions:

java.util.Date.Date(long date)

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) is meant to parse a String of the specified format into a Date (i.e. if you wanted to parse a String such as "2018-05-28 06:12:00" into a Date instance).

Community
  • 1
  • 1
Eran
  • 387,369
  • 54
  • 702
  • 768
2

Since it's 2018, you really should be making use of the java.time API introduced in Java 8

long createTime = 1527217399000L;
LocalDateTime ldt = Instant.ofEpochMilli(createTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
String format = ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH));
System.out.println(format);

Which prints 2018-05-25 13:03:19 (on PC)

And if you don't have Java 8, there's the ThreeTen backport which makes the API available to earlier versions of Java

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

The old java.util.Date API is notoriously poor and if you want to make clear decisions about what type of date / time you're using - whether it's in a particular timezone, etc. - you should use the java.time API.

Assuming (you'd need to confirm) that these millisecond timestamps are in UTC timezone, you can use code like this to format it as a date/time in your local system timezone.

Long tempTime = 1527217399000L;
Instant instant = Instant.ofEpochMilli(tempTime);
ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault());
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79