-1

I am trying to get dd.MM.yyyy and hh:mm from 1436536800 but only the time is correct, the date is completely wrong. I don't really understand how this is supposed to work

int dt = time.getInt("start")*1000;
Date date = new Date(dt);
startDate = dateFormat.format(date);
Aurora
  • 23
  • 9

4 Answers4

0

Assuming the time is correct, it's likely the fact that you're multiplying by 1,000. When creating the date the way you are, it takes in milliseconds. Is it possible that your input is already in milliseconds? (Your current method will be ~2 minutes off if so)

cameronlund4
  • 537
  • 1
  • 5
  • 27
0
Date date=new Date(1436536800);
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String dateText = df2.format(date);
Raghu K Nair
  • 3,854
  • 1
  • 28
  • 45
0

If time.getInt("start") is a valid unix timestamp, you must add "000" to the number. Example: 1436536800 * 1000 = 1436536800000. Then you can use the timestamp to get a Date:

final Date date = new Date(Long.parseLong("1436536800000"));
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy hh:mm");
System.out.println(sdf.format(date));

Console exit: 10.07.2015 09:00

Merch0
  • 594
  • 5
  • 17
  • This is exactly what I am doing and I get Sat 24.01.1970 at 09.28 – Aurora May 25 '16 at 22:38
  • In unix this is a valid timestamp: 1436536800. In java you must add "000". So the correct timestamp in java is: 1436536800000. Print in cosole the ts before call new Date(). – Merch0 May 25 '16 at 22:58
  • Isn't this exactly what I am doing in this line? int dt = time.getInt("start")*1000; – Aurora May 26 '16 at 09:07
  • okay it should have been getLong, not getInt...that was the problem. – Aurora May 26 '16 at 11:24
0

Date you are getting is a JSON string value. follow steps below to format it correctly.

First download Moment.js file and add it in your project.

var date1 = "1436536800"; // your long value contain in this variable.
var date2 = moment(date1).format(MMMM Do YYYY);//It will give you formatted date value.

see more formats below enter image description here

Harshil Shah
  • 203
  • 1
  • 16