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

Java - 從日期中減去天數(shù)

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

問題描述

限時送ChatGPT賬號..

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

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

我查看了有關(guān)此主題的其他一些帖子,但我從代碼中得到的結(jié)果總是不正確的.主要問題是,例如,減去天數(shù)時,年份值似乎沒有改變 - 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 ,你應(yīng)該這樣做:

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 時會發(fā)生完全相同的事情:

Exactly the same thing happens when dealing with Calendar:

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

請務(wù)必查看 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 注釋了第二個參數(shù)月,以提醒程序員發(fā)生了一些非常奇怪的事情.

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.

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

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

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
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 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 九九热在线免费视频 | 91社影院在线观看 | 狠狠久久| 五月婷婷激情网 | 久久亚洲国产精品 | 黄色片网此 | 亚洲不卡在线视频 | 日韩一区二区三区在线 | 中文字幕一区二区三区四区 | 亚洲成人av一区二区 | 久久精品国产亚洲夜色av网站 | 91视频在线看 | 91久久久久久 | 中文字幕在线免费视频 | 一区二区三区视频在线观看 | 欧洲成人免费视频 | 欧洲成人 | 99riav国产一区二区三区 | 五月婷亚洲 | 福利视频一区二区三区 | 欧美综合国产精品久久丁香 | 亚洲精品一区在线观看 | 日本国产精品视频 | 女人毛片a毛片久久人人 | 一区二区av | 国产日屁 | 欧美综合一区二区三区 | 91视频.com| 国产精品视频免费看 | 欧美精品日韩 | 天天操操操操操 | 久久国产一区二区 | 亚洲精品一区二区在线观看 | 国产在线麻豆精品入口 | 狠狠干影院 | 成人黄视频在线观看 | 精品1区 | 狠狠干在线 | 日韩免费视频一区二区 | 免费观看黄网站 | 亚洲福利一区二区 |