本文介紹了為什么我加了 10 分鐘后月份變成了 50?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時(shí)送ChatGPT賬號(hào)..
我有這個(gè)日期對象:
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:mm");
Date d1 = df.parse(interviewList.get(37).getTime());
d1 的值為 Fri Jan 07 17:40:00 PKT 2011
現(xiàn)在我正在嘗試在上面的日期上增加 10 分鐘.
Now I am trying to add 10 minutes to the date above.
Calendar cal = Calendar.getInstance();
cal.setTime(d1);
cal.add(Calendar.MINUTE, 10);
String newTime = df.format(cal.getTime());
newTime
的值更改為 2011-50-07 17:50
但應(yīng)該是 07-01-2011 17:50
.
Value of newTime
changes to 2011-50-07 17:50
but it should be 07-01-2011 17:50
.
它正確地添加了分鐘,但它也改變了月份,不知道為什么!
It adds minutes correctly but it also changes month, don't know why!
推薦答案
你的問題是你正在使用 mm
.您應(yīng)該使用 MM
.MM
是月份,mm
是分鐘.試試 yyyy-MM-dd HH:mm
The issue for you is that you are using mm
. You should use MM
. MM
is for month and mm
is for minutes. Try with yyyy-MM-dd HH:mm
其他方法:
可以這么簡單(其他選擇是使用 joda-time)
It can be as simple as this (other option is to use joda-time)
static final long ONE_MINUTE_IN_MILLIS=60000;//millisecs
Calendar date = Calendar.getInstance();
long t= date.getTimeInMillis();
Date afterAddingTenMins=new Date(t + (10 * ONE_MINUTE_IN_MILLIS));
這篇關(guān)于為什么我加了 10 分鐘后月份變成了 50?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!