convert date to timestamp UTC

限于喜欢 提交于 2019-12-11 10:49:15

问题


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=


回答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!




回答2:


final Date toTimestamp = new Date();
final long timestamp = toTimestamp.getTime();

final Date fromTimestamp = new Date(timestamp);

?



来源:https://stackoverflow.com/questions/25105816/convert-date-to-timestamp-utc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!