从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中做类似的事情?
答案 0 :(得分:2)
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
}
以毫秒为单位返回差异。然后,如果您想将其转换为" Human Readable"格式,您可以使用新的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/
答案 1 :(得分:1)
不要用Java(或PHP)来做这件事。这是在MySQL中使用date/time functions。
完成的SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(my_timestamp_col) AS seconds_ago ...
第一种方法将当前时间返回为unix timestamp(即,自纪元以来的秒数)。第二种方法将my_timestamp_col
时间作为unix时间戳返回。减去这两个,你有几秒钟前my_timestamp_col
。
编辑:正如亚历山大指出的那样,当你考虑时区时,这会变得更加复杂。一种方法是使用CONVERT_TZ
函数将my_timestamp_col
转换为UTC,然后在其上调用UNIX_TIMESTAMP
:
UNIX_TIMESTAMP(CONVERT_TZ(timestamp, '-5:00', '+0:00'))
另一个选择是在Java端执行此操作:将字段设置为Date
,应该为您进行时区转换;然后调用其getTime()
方法从纪元开始获取毫秒,并从System.currentTimeMillis()
中减去该值。这可以告诉你事件发生了多少毫秒。