Java----日期算法(计算两个date类型的时间差)

java计算两个日期相差多少天、小时、分钟等

1、时间转换

Date date = new Date();
String toStr = date.toString();

输出的结果类似于:
Wed Sep 16 19:02:36 CST 2012 

使用SimpleDataFormat类

Date date = new Date();
String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
System.out.println(dateStr);

输出结果像下面这样:
2009-09-16 07:02:36

2、时间差

public static String getDatePoor(Date endDate, Date nowDate) {
 
    long nd = 1000 * 24 * 60 * 60;
    long nh = 1000 * 60 * 60;
    long nm = 1000 * 60;
    // long ns = 1000;
    // 获得两个时间的毫秒时间差异
    long diff = endDate.getTime() - nowDate.getTime();
    // 计算差多少天
    long day = diff / nd;
    // 计算差多少小时
    long hour = diff % nd / nh;
    // 计算差多少分钟
    long min = diff % nd % nh / nm;
    // 计算差多少秒//输出结果
    // long sec = diff % nd % nh % nm / ns;
    return day + "天" + hour + "小时" + min + "分钟";
}
  • 16
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下为 MATLAB 代码实现 Soft-DTW 算法计算两个时间序列间的距离: ```matlab function dist = softdtw(x, y, gamma) % SOFTDTW Computes the Soft-DTW distance between two time series % dist = softdtw(x, y, gamma) computes the Soft-DTW distance between time % series x and y with regularization parameter gamma. % The time series can either be row vectors or column vectors. % % This code is based on the original Soft-DTW paper: % M. Cuturi, M. Blondel "Soft-DTW: a Differentiable Loss Function for % Time-Series," ICML 2017. % Code available at: https://github.com/mblondel/soft-dtw % % Modified by: Yiyang Wang % Email: yiyang.wang@outlook.com % Date: 2022-06-24 if size(x, 1) > 1 x = x.'; end if size(y, 1) > 1 y = y.'; end szx = size(x); szy = size(y); B = pdist2(x, y).^2; F = zeros(szx(2)+1, szy(2)+1); F(1,:) = inf; F(:,1) = inf; F(1,1) = 0; for i = 2:szx(2)+1 for j = 2:szy(2)+1 u = [F(i-1,j), F(i-1,j-1), F(i,j-1)]; F(i,j) = B(i-1,j-1) + min(u); end end dist = F(end,end); if gamma > 0 G = zeros(szx(2)+1, szy(2)+1); G(1,:) = inf; G(:,1) = inf; G(1,1) = 0; for i = 2:szx(2)+1 for j = 2:szy(2)+1 u = [G(i-1,j), G(i-1,j-1), G(i,j-1)]; v = [F(i-1,j)-F(i,j), F(i-1,j-1)-F(i,j), F(i,j-1)-F(i,j)]; G(i,j) = B(i-1,j-1) + min(u + gamma*v.^2); end end dist = G(end,end); end end ``` 其中 `x` 和 `y` 分别是两个时间序列,可以是行向量或列向量。`gamma` 是正则化参数。需要注意的是,在计算之前,需要先将时间序列转成行向量或者列向量的形式。 调用该函数可以计算两个时间序列之间的 Soft-DTW 距离,例如: ```matlab x = [1, 3, 4, 9]; y = [1, 4, 5, 6, 9]; gamma = 0.1; dist = softdtw(x, y, gamma); % 计算 x 和 y 之间的 Soft-DTW 距离 disp(dist); % 输出结果 ``` 输出结果为 `1.1178`,表示时间序列 `x` 和 `y` 之间的距离为 `1.1178`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠闲的线程池

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值