120

我有这个日期对象:

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 分钟。

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

它正确地增加了分钟,但它也改变了月份,不知道为什么!

4

13 回答 13

202

您的问题是您正在使用mm. 你应该使用MM. MM是月,mm是分钟。尝试yyyy-MM-dd HH:mm

其他方法:

它可以像这样简单(其他选择是使用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));
于 2012-01-28T08:45:51.950 回答
63

您可以使用 org.apache.commons.lang3.time 包中的 DateUtils 类

int addMinuteTime = 5;
Date targetTime = new Date(); //now
targetTime = DateUtils.addMinutes(targetTime, addMinuteTime); //add minute
于 2014-12-17T11:22:04.143 回答
33

实现@Pangea答案的便捷方法:

/*
*  Convenience method to add a specified number of minutes to a Date object
*  From: http://stackoverflow.com/questions/9043981/how-to-add-minutes-to-my-date
*  @param  minutes  The number of minutes to add
*  @param  beforeTime  The time that will have minutes added to it
*  @return  A date object with the specified number of minutes added to it 
*/
private static Date addMinutesToDate(int minutes, Date beforeTime){
    final long ONE_MINUTE_IN_MILLIS = 60000;//millisecs

    long curTimeInMs = beforeTime.getTime();
    Date afterAddingMins = new Date(curTimeInMs + (minutes * ONE_MINUTE_IN_MILLIS));
    return afterAddingMins;
}
于 2014-06-20T07:36:18.890 回答
28

为了避免任何依赖,您可以使用 java.util.Calendar 如下:

    Calendar now = Calendar.getInstance();
    now.add(Calendar.MINUTE, 10);
    Date teenMinutesFromNow = now.getTime();

在 Java 8 中,我们有新的 API:

    LocalDateTime dateTime = LocalDateTime.now().plus(Duration.of(10, ChronoUnit.MINUTES));
    Date tmfn = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
于 2016-09-20T10:20:28.783 回答
19

这是错误地指定:

SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:mm");

您使用的是分钟而不是月份 (MM)

于 2012-01-28T08:49:46.497 回答
6
于 2016-12-03T21:28:27.703 回答
4

使用这种格式,

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");

mm 表示分钟,MM 表示 mounth

于 2012-01-28T08:52:59.733 回答
4

您的 SimpleDateFormat 的模式有错误。它应该是

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
于 2012-01-28T08:51:35.830 回答
2

一旦你解析了日期,我使用这个实用函数来添加小时、分钟或秒:

public class DateTimeUtils {
    private static final long ONE_HOUR_IN_MS = 3600000;
    private static final long ONE_MIN_IN_MS = 60000;
    private static final long ONE_SEC_IN_MS = 1000;

    public static Date sumTimeToDate(Date date, int hours, int mins, int secs) {
        long hoursToAddInMs = hours * ONE_HOUR_IN_MS;
        long minsToAddInMs = mins * ONE_MIN_IN_MS;
        long secsToAddInMs = secs * ONE_SEC_IN_MS;
        return new Date(date.getTime() + hoursToAddInMs + minsToAddInMs + secsToAddInMs);
    }
}

添加较长时间段时要小心,1 天并不总是 24 小时(夏令时类型的调整、闰秒等),Calendar因此建议这样做。

于 2016-12-03T18:46:53.837 回答
2

可以在没有常量的情况下完成(比如 3600000 ms 是 1h)

public static Date addMinutesToDate(Date date, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minutes);
        return calendar.getTime();
    }

public static Date addHoursToDate(Date date, int hours) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.HOUR_OF_DAY, hours);
    return calendar.getTime();
}

用法示例:

System.out.println(new Date());
System.out.println(addMinutesToDate(new Date(), 5));

Tue May 26 16:16:14 CEST 2020
Tue May 26 16:21:14 CEST 2020
于 2020-05-26T14:20:42.827 回答
1

为我工作 DateUtils

//import
import org.apache.commons.lang.time.DateUtils

...

        //Added and removed minutes to increase current range dates
        Date horaInicialCorteEspecial = DateUtils.addMinutes(new Date(corteEspecial.horaInicial.getTime()),-1)
        Date horaFinalCorteEspecial = DateUtils.addMinutes(new Date(corteEspecial.horaFinal.getTime()),1)
于 2015-06-13T00:05:01.433 回答
1

对于 android 开发人员,这是一个使用 @jeznag 答案扩展的 kotlin 实现

 fun Date.addMinutesToDate(minutes: Int): Date {
    val minuteMillis: Long = 60000 //millisecs
    val curTimeInMs: Long = this.time
    val result = Date(curTimeInMs + minutes * minuteMillis)
    this.time = result.time
    return this
}

检查功能的单元测试按预期工作

@Test
fun `test minutes are added to date`() {
    //given
    val date = SimpleDateFormat("dd-MM-yyyy hh:mm").parse("29-04-2021 23:00")
    //when
    date?.addMinutesToDate(45)
    //then
    val calendar = Calendar.getInstance()
    calendar.time = date
    assertEquals(29, calendar[Calendar.DAY_OF_MONTH])
    assertEquals(23, calendar[Calendar.HOUR_OF_DAY])
    assertEquals(45, calendar[Calendar.MINUTE])
}
于 2021-04-18T07:12:02.033 回答
-3

Just for anybody who is interested. I was working on an iOS project that required similar functionality so I ended porting the answer by @jeznag to swift

private func addMinutesToDate(minutes: Int, beforeDate: NSDate) -> NSDate {
    var SIXTY_SECONDS = 60

    var m = (Double) (minutes * SIXTY_SECONDS)
    var c =  beforeDate.timeIntervalSince1970  + m
    var newDate = NSDate(timeIntervalSince1970: c)

    return newDate
}
于 2015-07-06T22:52:54.747 回答