久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java - 從日期中減去天數

Java - Subtract Days from date(Java - 從日期中減去天數)
本文介紹了Java - 從日期中減去天數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試從最初以字符串形式出現的日期中減去 5 天.

I'm trying to subtract 5 days from a date which comes in as a string initially.

我查看了有關此主題的其他一些帖子,但我從代碼中得到的結果總是不正確的.主要問題是,例如,減去天數時,年份值似乎沒有改變 - 2012-01-01 minus 5 days give me 'Jan 27 2012' 使用此代碼 -

I have had a look at some of the other posts on this subject but the result i get from the code is always incorrect. The main problem is that the year value does not seem to change when the days are subtracted for example - 2012-01-01 subtract 5 days gives me 'Jan 27 2012' using this code -

cal.add(Calendar.DATE, -5);

請幫忙.

推薦答案

你知道嗎,在 Java 中,月份 1 實際上是二月?

Did you know that, in Java, month 1 is actually February?

Date februaryTheFirst = new Date(2012,1,1); // equals 2012-02-01

這可以解釋您所看到的.如果你想實例化 2012-01-01 ,你應該這樣做:

This might explain what you are seeing. If you want to instantiate 2012-01-01 instead, you should do:

Date firstDayOf2012 = new Date(2012,0,1); // this is 2012-01-01

在處理 Calendar 時會發生完全相同的事情:

Exactly the same thing happens when dealing with Calendar:

Calendar.getInstance().set(2012,0,1); // 2012-01-01

請務必查看 Date(int, int, int) 的文檔和 Calendar.set(int, int, int).此外,您可以檢查解析字符串的方式.如果您使用 SimpleDateFormat.parse(...),事情會變得更容易.

Be sure to check the documentation for Date(int, int, int) and Calendar.set(int, int, int). Also, you could check the way you are parsing the string. If you use SimpleDateFormat.parse(...), things can be easier.

很奇怪,不是嗎?想想看……就像一個有趣的事實,IntelliJ 的文檔用 @MagicConstant 注釋了第二個參數月,以提醒程序員發生了一些非常奇怪的事情.

Strange, isn't it? Go figure... Just as a fun fact, IntelliJ's documentation annotates this second parameter, month, with @MagicConstant, to remember the programmer that there's something very strange going on.

這篇關于Java - 從日期中減去天數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: www.99riav| 中文字幕一区二区在线播放 | 国产精品自拍小视频 | www精品| 久久综合一区 | 黄av在线 | 欧美精品999| 国产欧美综合一区二区三区 | av影院在线 | 欧美一级做性受免费大片免费 | 日韩a级片 | 韩国精品一区 | 亚洲色在线视频 | 欧美日韩亚洲一区 | 欧美理论在线观看 | 亚洲无av在线中文字幕 | 日韩欧美大片 | 九九热在线精品视频 | 免费一区二区 | 538精品视频 | 国产一区二区在线播放 | 一区二区三区四区精品 | 国产精品91在线 | 99视频在线观看免费 | 亚洲高清毛片一区二区 | 一级做a爱片性色毛片 | 欧美一级在线 | 精品精品| 国产一级特黄aaa大片 | 中文字幕亚洲精品 | 久久亚洲免费视频 | 九九九精品视频 | 五月婷婷色 | 午夜视频网站 | 国产精品一二三区 | 99久久精品国产一区二区三区 | 欧美男人天堂 | 日韩视频一区二区三区 | 在线观看av网站 | 精品久久视频 | 久久中文字幕视频 |