久久久久久久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:獲取當前星期幾的值)
主站蜘蛛池模板: 亚洲第一福利视频 | 国产精品a一区二区三区网址 | 亚洲精品区| av免费在线播放 | 大学生a级毛片免费视频 | 中文字幕欧美一区 | 中文字幕av中文字幕 | 中文天堂在线观看 | 视频一区二区在线观看 | 国产精品免费大片 | 国产精品18久久久久久久 | 粉嫩粉嫩芽的虎白女18在线视频 | 日本不卡一区二区三区在线观看 | 国产日产精品一区二区三区四区 | 国产国产精品久久久久 | 久久久久久久99 | 国产清纯白嫩初高生在线播放视频 | 日韩欧美一区二区三区免费观看 | 99精品久久久久 | 黄网站涩免费蜜桃网站 | 国产资源一区二区三区 | 一区二区成人 | 久久大香 | 久久久国产精品网站 | 亚洲一区二区久久 | 中文字幕国产视频 | 九色视频网站 | 69亚洲精品 | 亚洲午夜精品一区二区三区 | 亚洲一区二区三区免费观看 | 午夜精品久久久久久 | 精品一区久久 | 国产精品呻吟久久av凹凸 | a视频在线播放 | 欧美日韩综合精品 | 久久丝袜视频 | 国产精品久久久久久久模特 | 欧美一区二区三区 | 99国产精品99久久久久久粉嫩 | 夜夜草导航 | av在线免费观看网址 |