How can I increment a date by one day in Java?

前端 未结 28 1769

I\'m working with a date in this format: yyyy-mm-dd.

How can I increment this date by one day?

相关标签:
28条回答
  • 2020-11-21 07:23

    In Java 8 simple way to do is:

    Date.from(Instant.now().plusSeconds(SECONDS_PER_DAY))
    
    0 讨论(0)
  • 2020-11-21 07:23

    In java 8 you can use java.time.LocalDate

    LocalDate parsedDate = LocalDate.parse("2015-10-30"); //Parse date from String
    LocalDate addedDate = parsedDate.plusDays(1);   //Add one to the day field
    

    You can convert in into java.util.Date object as follows.

    Date date = Date.from(addedDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    

    You can formate LocalDate into a String as follows.

    String str = addedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    
    0 讨论(0)
  • 2020-11-21 07:24

    You can use this package from "org.apache.commons.lang3.time":

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     Date myNewDate = DateUtils.addDays(myDate, 4);
     Date yesterday = DateUtils.addDays(myDate, -1);
     String formatedDate = sdf.format(myNewDate);  
    
    0 讨论(0)
  • 2020-11-21 07:26
    SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
    Calendar cal = Calendar.getInstance();
    cal.setTime( dateFormat.parse( inputString ) );
    cal.add( Calendar.DATE, 1 );
    
    0 讨论(0)
提交回复
热议问题