Get n days before now
When I tried to google and get a way to get a Date() n days before now.
Lots of the answer are trying to do with Calendar.set() or GregorianCalendar.roll().
When n > 30, enven > 366, the solution becomes complex.
The easier way for me is calculating with milliseconds since January 1, 1970, 00:00:00 GMT.
result:
Thu Aug 23 22:38:18 CST 2012:INFO:2012-08-23 22:38:18
Thu Aug 23 22:38:18 CST 2012:INFO:Day before 32 days:
Thu Aug 23 22:38:18 CST 2012:INFO:2012-07-22 22:38:18
Lots of the answer are trying to do with Calendar.set() or GregorianCalendar.roll().
When n > 30, enven > 366, the solution becomes complex.
The easier way for me is calculating with milliseconds since January 1, 1970, 00:00:00 GMT.
A groovy example:
What you have to care for this way is the value of milliseconds is large.
For example, the value of milliseconds of 50days is more than 32-bit unsiged.
But the good news is that the value of milliseconds of 100years is less than 43-bit unsigned.
import java.util.Date
def now = new Date()
def nowMsFrom1970 = now.getTime()
def days = 32
def MsInOneDay = 24*3600*1000
def days_before = now.clone()
days_before.setTime(nowMsFrom1970 - (long)days * MsInOneDay)
log.info(now.format("YYYY-MM-dd HH:mm:ss"))
log.info("Day before " + days.toString() + " days:")
log.info(days_before.format("YYYY-MM-dd HH:mm:ss"))
result:
Thu Aug 23 22:38:18 CST 2012:INFO:2012-08-23 22:38:18
Thu Aug 23 22:38:18 CST 2012:INFO:Day before 32 days:
Thu Aug 23 22:38:18 CST 2012:INFO:2012-07-22 22:38:18
相關文章
- 9 must-knows before implementing SAP Simple Finance.NaN
- [Shell] get after|before year,month,day,week,hour,minute,second
- 2008 5 5: I want to get know you better
- New Year and Days
- 023 Given an integer n. get the number of 0, 2, 4 from all the values from [0, n]
- flask08_daysFlask
- docker_02daysDocker
- docker_03daysDocker3D
- docker_04daysDocker
- InnoDB:Failingassertion:page_get_n_recs(page)>1AI
- Kotlin31DaysKotlin
- 解決問題: go get certificate signed by unknown authority 換個代理或者Go
- jQuery before()jQuery
- 遭遇 bug InnoDB: Failing assertion: page_get_n_recs(page) > 1AI
- db2 get database configuration 報錯: CSQL6031NDB2DatabaseSQL
- mysql partition table use to_days bugMySql
- Days on my past 童年英文版AST
- Visual C++ generate uuid via UuidCreate and CoCreateGuid,get time now,write string to fileC++GUI
- [CSS] 偽元素和偽類,::before 和 :before 區別CSS
- 「SAP技術」SAP ME2N報表能按'order acknowledgement'查詢PO
- 0days最好的破解網站網站
- JavaScript insertBefore()JavaScript
- javascript beforeprint事件JavaScript事件
- jQuery insertBefore()jQuery
- Before的翻譯
- VM - 6Days_Lab-v1.0.1 的破解
- [LeetCode] 568. Maximum Vacation DaysLeetCode
- JUnit4 中@AfterClass @BeforeClass @after @before的區別對比
- CSS 巧用 :before和:afterCSS
- CSS巧用:before和:afterCSS
- jQuery.now()jQuery
- jQuery knowledgejQuery
- Dynamics CRM實體系列之1:N、N:1以及N:N關係
- css偽元素(before與after)CSS
- 偽元素 before 和 after 初探
- javascript onbeforeunload事件用法JavaScript事件
- 理解偽元素 :before 和 :after
- lombok get/set 與 JavaBean get/setLombokJavaBean