当前时间减数据库时间

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateDifferentExample {
    public static void main(String[] args) {
        String dateStart = "2013-02-19 09:29:58";
        String dateStop = "2013-02-20 11:31:48";

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date d1 = null;
        Date d2 = null;

        try {
            d1 = format.parse(dateStart);
            d2 = format.parse(dateStop);

            //毫秒ms
            long diff = d2.getTime() - d1.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);

            System.out.print("两个时间相差:");
            System.out.print(diffDays + " 天, ");
            System.out.print(diffHours + " 小时, ");
            System.out.print(diffMinutes + " 分钟, ");
            System.out.print(diffSeconds + " 秒.");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

获取当前时间

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(date);

例子

 

 

package cn.service.impl;

import cn.dao.IDentDao;
import cn.pojo.Dog;
import cn.service.DogMessage;
import cn.service.DogZhanShiService;
import com.alibaba.fastjson.JSON;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DogZhanShiServiceImpl implements DogZhanShiService {

    @Resource(name = "IDentDao")
    private IDentDao dd;

    public IDentDao getDd() {
        return dd;
    }

    public void setDd(IDentDao dd) {
        this.dd = dd;
    }

    public String getAll() {
        List<DogMessage> ss = new ArrayList<DogMessage>();
        for (Dog dog:dd.getss()) {
            DogMessage d = new DogMessage();
            d.setDogPrice(dog.getDogPrice());
            d.setDogKind(dd.getDogType(dog.getDogKind()).getDtype());
            String a = d.setDogUploading(dog.getDogUploading());
            d.setDogTupian(dog.getDogTupian());
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(date);
//            System.out.println(format);
//            System.out.println(a);
            Date d1 = null;
            Date d2 = null;
            try {
                d1 = sdf.parse(a);
                d2 = sdf.parse(format);
                long diff = d2.getTime() - d1.getTime();
                long diffSeconds = diff / 1000 % 60;
                long diffMinutes = diff / (60 * 1000) % 60;
                long diffHours = diff / (60 * 60 * 1000) % 24*60;
                long diffDays = diff / (24 * 60 * 60 * 1000)*24*60;
                long diffSum = diffMinutes + diffHours + diffDays + diffSeconds;
                d.setDogUploading(String.valueOf(diffSum));
//                System.out.print("两个时间相差:");
//                System.out.print(diffDays + " 天, ");
//                System.out.print(diffHours + " 小时, ");
//                System.out.print(diffMinutes + diffHours + diffDays + diffSeconds + " 分钟前");
//                System.out.print(diffSeconds + " 秒.");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            ss.add(d);
        }
        String a = JSON.toJSONString(ss);
        return a;
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值