关于Date数据计算

1)获取系统当前时间:
 
 SimpleDateFormat dateFormat=new SimpleDateFormat("yyyMMddHHmm");//设置时间格式
 String currentTime =dateFormat.format(new Date());

该currentTime结果为:201805112126

2)获取固定时间后的时间(我考虑这个用法是因为项目需要,我需要把从当前时间开始,到多久时间之后的规格化的时间存起来)

Calendar current = Calendar.getInstance();//相当于获取日历
            current.add(Calendar.MINUTE, 5);//获取分钟(同样的可以获取其它属性年月日天……),然后加上5分钟,如果是-5就是减去5分钟
            Date taskTime = current.getTime();//获取Date格式的时间
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyMMddHHmm");//设置时间格式
            String currentTime = dateFormat.format(taskTime);//格式化输出

如果原始时间为:201805112126,则currentTime结果将为:201805112131

3)对时间进行比较

Date date1=dateFormat.parse(201805112139);//现在时间
Date date2=dateFormat.parse(201805112143);//下次响铃时间
如果使用  date1 .before(date2) 则返回结果为true,因为date1小于date2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值