从mysql获取时间戳,它返回类似于"2014-03-19 12:00:43"的东西,在java中,我想从时间戳和当前时间中获得差异。
新的java和非常不熟悉,这是我在php虽然....任何类似的东西都可以达到相同的效果
date_def
新的java和非常不熟悉,这是我在php虽然....任何类似的东西都可以达到相同的效果
date_default_timezone_set('America/New_York');
$server = timesince($row['timestamp']);
function timesince($time){
$seconds = strtotime($time) - time();
$days = floor($seconds/ -86400);
$seconds %= 86400;
新的java和非常不熟悉,这是我在php虽然....任何类似的东西都可以达到java新手和非常不熟悉,这是我在php虽然....任何类似的东西都可以达到相同的效果
date_default_timezone_set('America/New_York');
$server = timesince($row['timestamp']);
新的java和非常不熟悉,这是我在php虽然....任何类似的东西都可以达到相同的效果
新的java和非常不熟悉,这是我在php虽然....任何类似的东西都可以达到相同的效果
date_default_timezone_set('America/New_York');
$server = timesince($row['timestamp']);
function timesince($time){
$seconds = strtotime($time) - time();
$days = floor($seconds/ -86400);
$seconds %= 86400;time();
$days = floor($seconds/ -86400);
$seconds %= 86400;
function timesince($time){
$seconds = strtotime($time) - time();
$days = floor($seconds/ -86400);
$seconds %= 86400;
$seconds = strtotime($time) - time();
$days = floor($seconds/ -86400);
$seconds %= 86400;ault_timezone_set('America/New_York');
新的java和非常不熟悉,这是我在php虽然....任何类似的东西都可以达到相同的效果
date_default_timezone_set('America/New_York');
$server = timesince($row['timestamp']);
function timesince($time){
$seconds = strtotime($time) - time();
$days = floor($seconds/ -86400);
$seconds %= 86400;
$minutes = floor($seconds / -60);
$seconds %= 60;
$seconds *= -1;
if ($days > 360){
return "Not voted here yet.";
}
if($days > 1){
$days = $days." days, ";
} elseif ($days == 1){
$days = $days." day, ";
} else {
$days = "";
}
if($hours > 1){
$hours = $hours." hours, ";
}elseif ($hours == 1){
$hours = $hours." hour, ";
} else {
$hours = "";
}
if($minutes > 1){
$minutes = $minutes." minutes and ";
}elseif ($minutes == 1){
$minutes = $minutes." minute and ";
}else {
$minutes = "";
}
if($seconds > 1){
$seconds = $seconds." seconds";
}elseif ($seconds == 1){
$seconds = $seconds." second";
}
return $days.$hours.$minutes.$seconds." ago";
}
我在java中没有看到任何类似于strtotime的东西,也不想使用任何额外的库。
同样在php脚本中我在顶部有
date_default_timezone_set('America/New_York');
所以它得到正确的时差与时区的考虑。
从哪里开始在java中做类似的事情呢?
Javascript的原生Date对象能够非常容易地复制php的strtodate函数。要调用它,只需使用:
var d = new Date(dateString);
(参见http://www.w3schools.com/jsref/jsref_obj_date.asp了解更多信息)因此,您可以使用如下函数:
function timesince( dateString ) {
var originalTime = new Date(); // The time you provided in milliseconds converted to javascript object
var currentTime = new Date(); // The current time as a javascript object
return millisecondDifference = parseFloat(currentTime.getTime()) - parseFloat(originalTime.getTime());
// returns the difference in milliseconds between the current time and the original time
// parseFloat is used to ensure that the values returned are read by javascript as floats (numbers) and thus can have math operations performed on them
}
以毫秒为单位返回差值。然后,如果您想将其转换为"人类可读"格式,您可以使用一个新的Date对象,如下所示:
var diffRaw = timesince( '2014-03-19 12:00:43' );
var diff = new Date(diffRaw);
var seconds = diff.getSeconds() // Seconds from 0 - 59
var minutes = diff.getMinutes() // Minutes from 0 - 59
var hours = diff.getHours() // Hours from 0 - 23
var day = diff.getDay() // Days from 0 - 6
var date = diff.getDate() // Date from 1 - 31
var month = diff.getMonth() // Months from 0 - 11
var year = diff.getFullYear() // XXXX year starting from 1970
要完全复制您所编写的函数,您可以使用以下函数:
function timesince( dateString ) {
var originalTime = new Date(dateString);
var currentTime = new Date();
var diffRaw = millisecondDifference = parseFloat(currentTime.getTime()) - parseFloat(originalTime.getTime());
var diff = new Date(diffRaw);
var seconds = diff.getSeconds();
var minutes = diff.getMinutes();
var hours = diff.getHours();
var day = diff.getDay();
var date = diff.getDate() - 1;
var month = diff.getMonth();
var year = diff.getFullYear();
if( year !== 1970 ) {
return "Not voted here yet.";
}
var returnString = "";
if(month > 1 || month == 0) {
returnString += month + " months, ";
} else {
returnString += month + " month, ";
}
if(date > 1 || date == 0) {
returnString += date + " days, ";
} else {
returnString += date + " day, ";
}
if(hours > 1 || hours == 0) {
returnString += hours + " hours, ";
} else {
returnString += hours + " hour, ";
}
if(minutes > 1 || minutes == 0) {
returnString += minutes + " minutes, ";
} else {
returnString += minutes + " minute, ";
}
if(seconds > 1 || seconds == 0) {
returnString += seconds + " seconds, ";
} else {
returnString += seconds + " second, ";
}
return returnString;
}
你可以在这里找到一个工作示例:http://jsfiddle.net/MQPPw/
在Java(或PHP)中不要这样做。这是在MySQL中完成的,使用它的日期/时间函数。
SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(my_timestamp_col) AS seconds_ago ...
第一个方法返回当前时间作为unix时间戳(即从epoch开始的秒数)。第二个方法返回my_timestamp_col
时间作为unix时间戳。这两个相减,你就得到了my_timestamp_col
在多少秒前。
EDIT:正如Alexander指出的那样,当您考虑时区因素时,情况会变得更加复杂。一种选择是使用CONVERT_TZ
函数将my_timestamp_col
转换为UTC,然后调用UNIX_TIMESTAMP
:
UNIX_TIMESTAMP(CONVERT_TZ(timestamp, '-5:00', '+0:00'))
另一个选择是在Java端这样做,毕竟:获取字段作为Date
,它应该为您做时区转换;然后调用它的getTime()
方法来获得自epoch以来的毫秒数,并从System.currentTimeMillis()
中减去它。它给出了事件发生的毫秒数