java 中 2 个日期对象之间的差异(以分钟为单位)
difference between 2 date objects in minutes in java
我想在几分钟内得到 java 中两个日期对象之间的差异,以了解用户在我的应用程序中登录了多少分钟。
String query = "Select * from cabin_info where ip_address = ?";
ps = con.prepareStatement(query);
ps.setString(1, IPAddress);
rs = ps.executeQuery();
if (rs.next()) {
cabin_id = rs.getInt("cabin_id");
start_time = rs.getString("start_time");
username = rs.getString("username");
}
Date st_time = AppConstants.time_format.parse(start_time);
Date date = AppConstants.time_format.parse(AppConstants.time_format.format(new Date()));
long diff = date.getTime() - st_time.getTime();
long diffMinutes = diff / (60 * 1000) % 60;
System.out.println("Total time = " + diffMinutes);
日期格式为 HH-mm-ss
但我得到的只是时间分钟之间的差异,而不是时间
由于您要查找以分钟为单位的时间间隔,因此只需将毫秒转换为分钟即可。
minutes = milliseconds/(60*1000)
因此,您的代码:
long diffMinutes = diff / (60 * 1000) % 60;
变为:
long diffMinutes = diff / (60 * 1000);
如果您想计算小时和分钟,在末尾添加一个模数 60 是有意义的。
long result = ((date.getTime()/60000) - (st_time.getTime()/60000));
System.out.println("Total time = " + result);
将毫秒转换为分钟并减去它
长分钟 = TimeUnit.MINUTES.convert(date.getTime() - st_time.getTime(), TimeUnit.MILLISECONDS)
我想在几分钟内得到 java 中两个日期对象之间的差异,以了解用户在我的应用程序中登录了多少分钟。
String query = "Select * from cabin_info where ip_address = ?";
ps = con.prepareStatement(query);
ps.setString(1, IPAddress);
rs = ps.executeQuery();
if (rs.next()) {
cabin_id = rs.getInt("cabin_id");
start_time = rs.getString("start_time");
username = rs.getString("username");
}
Date st_time = AppConstants.time_format.parse(start_time);
Date date = AppConstants.time_format.parse(AppConstants.time_format.format(new Date()));
long diff = date.getTime() - st_time.getTime();
long diffMinutes = diff / (60 * 1000) % 60;
System.out.println("Total time = " + diffMinutes);
日期格式为 HH-mm-ss
但我得到的只是时间分钟之间的差异,而不是时间
由于您要查找以分钟为单位的时间间隔,因此只需将毫秒转换为分钟即可。
minutes = milliseconds/(60*1000)
因此,您的代码:
long diffMinutes = diff / (60 * 1000) % 60;
变为:
long diffMinutes = diff / (60 * 1000);
如果您想计算小时和分钟,在末尾添加一个模数 60 是有意义的。
long result = ((date.getTime()/60000) - (st_time.getTime()/60000));
System.out.println("Total time = " + result);
将毫秒转换为分钟并减去它
长分钟 = TimeUnit.MINUTES.convert(date.getTime() - st_time.getTime(), TimeUnit.MILLISECONDS)