本文介紹了為什么我加了 10 分鐘后月份變成了 50?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我有這個日期對象:
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
現在我正在嘗試在上面的日期上增加 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
但應該是 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
.您應該使用 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));
這篇關于為什么我加了 10 分鐘后月份變成了 50?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!