将日期转换为时间戳UTC

时间:2014-08-03 14:46:01

标签: java timestamp java.util.date

我是Java的新手,想知道如何将日期转换为时间戳,如

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 如果我把日期传给它,反之亦然..

我在这里搜索StackOverflow,但没有一个问题解决了我的问题

我需要在我的JSON中使用此时间戳作为highcharts API上的参数来显示点

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

2 个答案:

答案 0 :(得分:0)

将日期转换为时间戳:

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();

将时间戳转换为日期:

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"

希望这有帮助!

答案 1 :(得分:-1)

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

final Date fromTimestamp = new Date(timestamp);