How to add one day to a date? [duplicate]

2019-01-01 01:49发布

This question already has an answer here:

I want to add one day to a particular date. How can I do that?

Date dt = new Date();

Now I want to add one day to this date.

标签: java datetime
18条回答
泪湿衣
2楼-- · 2019-01-01 02:16

you want after days find date this code try..

public Date getToDateAfterDays(Integer day) {
        Date nowdate = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(nowdate);
        cal.add(Calendar.DATE, day);
        return cal.getTime();
    }
查看更多
几人难应
3楼-- · 2019-01-01 02:17

Java 8 LocalDate API

LocalDate.now().plusDays(1L);

查看更多
泪湿衣
4楼-- · 2019-01-01 02:23

I found a simple method to add arbitrary time to a Date object

Date d = new Date(new Date().getTime() + 86400000)

Where:

86 400 000ms = 1 Day  : 24*60*60*1000
 3 600 000ms = 1 Hour :    60*60*1000
查看更多
长期被迫恋爱
5楼-- · 2019-01-01 02:26

As mentioned in the Top answer, since java 8 it is possible to do:

Date dt = new Date();
LocalDateTime.from(dt.toInstant()).plusDays(1);

but this can sometimes lead to an DateTimeException like this:

java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: 2014-11-29T03:20:10.800Z of type java.time.Instant

It is possible to avoid this Exception by simply passing the time zone:

LocalDateTime.from(dt.toInstant().atZone(ZoneId.of("UTC"))).plusDays(1);
查看更多
美炸的是我
6楼-- · 2019-01-01 02:26

This will increase any date by exactly one

String untildate="2011-10-08";//can take any date in current format    
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );   
Calendar cal = Calendar.getInstance();    
cal.setTime( dateFormat.parse(untildate));    
cal.add( Calendar.DATE, 1 );    
String convertedDate=dateFormat.format(cal.getTime());    
System.out.println("Date increase by one.."+convertedDate);
查看更多
人气声优
7楼-- · 2019-01-01 02:26

use DateTime object obj.Add to add what ever you want day hour and etc. Hope this works:)

查看更多
登录 后发表回答