如何从java当前日期减去n天? [英] How to subtract n days from current date in java?
本文介绍了如何从java当前日期减去n天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要如何从Java当前的日期中减去n天?
解决方案
您不必使用日历。您可以使用时间戳记:
日期d = initDate(); //将日期初始化到任何日期
日期dateBefore =新日期(d.getTime() - n * 24 * 3600 * 1000 l); //减去n天
更新
不要忘记
请考虑以下警告:
添加 1000 * 60 * 60 * 24
毫秒到一个java日期会一直在很大的一段时间内添加零天或两天的原始日期在闰秒,夏令时的情况下等等。如果您需要100%确定只有一天会被添加,此解决方案不是使用的。
I want to subtract n days from the current date in Java.
How do I do that?
解决方案
You don't have to use Calendar. You can just play with timestamps :
Date d = initDate();//intialize your date to any date
Date dateBefore = new Date(d.getTime() - n * 24 * 3600 * 1000 l ); //Subtract n days
UPDATE DO NOT FORGET TO ADD "l" for long by the end of 1000.
Please consider the below WARNING:
Adding 1000*60*60*24
milliseconds to a java date will once in a great while add zero days or two days to the original date in the circumstances of leap seconds, daylight savings time and the like. If you need to be 100% certain only one day is added, this solution is not the one to use.
这篇关于如何从java当前日期减去n天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文