How to add minutes to my Date



  • 5 Answer(s)

    Try this solution, we should use MM. MM is for month and mm is for minutes.

    So the order is yyyy-mm-dd-HH-mm.

    Another vision:

    We can use joda.time. But this is convenient and simple

    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));
    

     

    Answered on December 20, 2018.
    Add Comment

     In org.apache.commons.lang3 time we can use DateUtils class .. It is so simple,

    int addMinuteTime = 5;
    Date targetTime = new Date(); //now
    targetTime = DateUtils.addMinutes(targetTime, addMinuteTime); //add minute
    
    Answered on December 20, 2018.
    Add Comment

    Use this code,

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

    We can use minutes instead of month. Months are denoted as (MM) and minutes are (mm).

    Answered on December 20, 2018.
    Add Comment

    This is incorrectly specified:

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

    You’re using minutes instead of month (MM)

    Answered on January 16, 2019.
    Add Comment

    In order to avoid any dependency you can use java.util.Calendar as follow:

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

    In Java 8 we have new API:

        LocalDateTime dateTime = LocalDateTime.now().plus(Duration.of(10, ChronoUnit.MINUTES));
        Date tmfn = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
    Answered on January 16, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.