1

I am new to Java and wanted to know how can I convert date to timestamp like

http://www.timestampconvert.com/?go1=true&m=08&d=06&y=2007&hours=05&min=30&sec=000&Submit=++++++Convert+to+timestamp+++++&offset=-5.5 if I pass a date to it and vice versa..

I searched here on StackOverflow but none of the questions have solved my problem

I need to use this timestamp in my JSON as a parameter on the highcharts API to show points

http://www.highcharts.com/samples/data/jsonp.php?filename=msft-c.json&callback=

aaradhana
  • 13
  • 1
  • 5
  • possible duplicate of [Java: Date from unix timestamp](http://stackoverflow.com/questions/3371326/java-date-from-unix-timestamp) and [this](http://stackoverflow.com/q/12031333/642706) and [this](http://stackoverflow.com/q/535004/642706) and [this](http://stackoverflow.com/q/6687433/642706) and [this](http://stackoverflow.com/q/6687433/642706) and many more. – Basil Bourque Aug 03 '14 at 15:58
  • @BasilBourque thnx for you comment but i have already checked them – aaradhana Aug 03 '14 at 16:20

2 Answers2

1

To convert a date to a timestamp:

String date = "2014-08-03 15:20:10"; //Replace with your value
Timestamp timestamp = Timestamp.valueOf(date);
// Convert timestamp to long for use
long timeParameter = timestamp.getTime();

To convert a timestamp to a date:

long timeParameter = 1186358400; //Replace with your value
Timestamp timestamp = new Timestamp(timeParameter);
Date date = new Date(timestamp.getTime());
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String textDate = df.format(date); // This gives a string like "2014-08-03 15:20:10"

Hope this helps!

Eoin
  • 833
  • 2
  • 13
  • 24
  • @Thnks for your answer buddy :) – aaradhana Aug 03 '14 at 15:06
  • No problem! I'm new on Stack Overflow too. Glad I could help! :) – Eoin Aug 03 '14 at 15:06
  • Timestamp timestamp = new Timestamp(timeParameter); here m getting like {SysUpTime = 137 days 7:26:24} {Timestamp = Sun Aug 03 20:37:33 IST 2014} how to fetch date from this like 2007-08-06 5:30:00 – aaradhana Aug 03 '14 at 15:08
-1
final Date toTimestamp = new Date();
final long timestamp = toTimestamp.getTime();

final Date fromTimestamp = new Date(timestamp);

?

acc15
  • 1,205
  • 1
  • 11
  • 22