java时间往后一天_如何在Java中将日期增加一天?

24个解决方案

617 votes

像这样的东西应该做的伎俩:

String dt = "2008-01-01"; // Start date

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Calendar c = Calendar.getInstance();

c.setTime(sdf.parse(dt));

c.add(Calendar.DATE, 1); // number of days to add

dt = sdf.format(c.getTime()); // dt is now the new date

Dave answered 2018-12-30T16:49:31Z

177 votes

与C#相比,Java似乎远远落后于八球。 此实用程序方法使用Calendar.add方法(可能是唯一简单的方法)显示了在Java SE 6中的方法。

public class DateUtil

{

public static Date addDays(Date date, int days)

{

Calendar cal = Calendar.getInstance();

cal.setTime(date);

cal.add(Calendar.DATE, days); //minus number would decrement the days

return cal.getTime();

}

}

要根据提出的问题添加一天,请按以下方式调用:

String sourceDate = "2012-02-29";

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

Date myDate = format.parse(sourceDate);

myDate = DateUtil.addDays(myDate, 1);

Lisa answered 2018-12-30T16:50:00Z

60 votes

我更喜欢使用Apache的DateUtils。 查看这个[http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html。]它非常方便,特别是当你必须使用它时 您项目中的多个位置,并且不希望为此编写单线方法。

API说:

addDays(Date date,int amount):在返回新对象的日期中添加若干天。

请注意,它返回一个新的Date对象,并且不会对前一个对象进行更改。

Risav Karna answered 2018-12-30T16:50:43Z

58 votes

java.time

在Java 8及更高版本中,java.time包使其非常自动化。(教程)

假设String输入输出:

import java.time.LocalDate;

public class DateIncrementer {

static public String addOneDay(String date) {

return LocalDate.parse(date).plusDays(1).toString();

}

}

Daniel C. Sobral answered 2018-12-30T16:51:18Z

55 votes

SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );

Calendar cal = Calendar.getInstance();

cal.setTime( dateFormat.parse( inputString ) );

cal.add( Calendar.DATE, 1 );

Alex B answered 2018-12-30T16:51:34Z

43 votes

构造一个Calendar对象并使用方法add(Calendar.DATE,1);

krosenvold answered 2018-12-30T16:51:56Z

39 votes

看看Joda-Time([http://joda-time.sourceforge.net/)。]

DateTimeFormatter parser = ISODateTimeFormat.date();

DateTime date = parser.parseDateTime(dateString);

String nextDay = parser.print(date.plusDays(1));

Willi aus Rohr answered 2018-12-30T16:52:19Z

37 votes

请注意,这条线增加了24小时:

d1.getTime() + 1 * 24 * 60 * 60 * 1000

但这条线增加了一天

cal.add( Calendar.DATE, 1 );

在夏令时变化(25或23小时)的日子里,您会得到不同的结果!

Florian R. answered 2018-12-30T16:52:54Z

36 votes

Java 8添加了一个用于处理日期和时间的新API。

使用Java 8,您可以使用以下代码行:

// parse date from yyyy-mm-dd pattern

LocalDate januaryFirst = LocalDate.parse("2014-01-01");

// add one day

LocalDate januarySecond = januaryFirst.plusDays(1);

micha answered 2018-12-30T16:53:24Z

25 votes

你可以使用Simple java.util lib

Calendar cal = Calendar.getInstance();

cal.setTime(yourDate);

cal.add(Calendar.DATE, 1);

yourDate = cal.getTime();

Pawan Pareek answered 2018-12-30T16:53:47Z

22 votes

Date today = new Date();

SimpleDateFormat formattedDate = new SimpleDateFormat("yyyyMMdd");

Calendar c = Calendar.getInstance();

c.add(Calendar.DATE, 1); // number of days to add

String tomorrow = (String)(formattedDate.format(c.getTime()));

System.out.println("Tomorrows date is " + tomorrow);

这将给明天的日期。 c.add(...)参数可以从1更改为另一个数字以获得适当的增量。

Akhilesh T. answered 2018-12-30T16:54:09Z

16 votes

如果您使用的是Java 8,那么请执行此操作。

LocalDate sourceDate = LocalDate.of(2017, Month.MAY, 27); // Source Date

LocalDate destDate = sourceDate.plusDays(1); // Adding a day to source date.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // Setting date format

String destDate = destDate.format(formatter)); // End date

如果你想使用SimpleDateFormat,那就这样做吧。

String sourceDate = "2017-05-27"; // Start date

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Calendar calendar = Calendar.getInstance();

calendar.setTime(sdf.parse(sourceDate)); // parsed date and setting to calendar

calendar.add(Calendar.DATE, 1); // number of days to add

String destDate = sdf.format(calendar.getTime()); // End date

Avijit Karmakar answered 2018-12-30T16:54:38Z

15 votes

long timeadj = 24*60*60*1000;

Date newDate = new Date (oldDate.getTime ()+timeadj);

这将从oldDate开始占用epoch以来的毫秒数,并添加1天的毫秒数,然后使用Date()公共构造函数使用新值创建日期。 此方法允许您添加1天或任意数量的小时/分钟,而不仅仅是整天。

dvaey answered 2018-12-30T16:55:02Z

11 votes

由于Java 1.5 TimeUnit.DAYS.toMillis(1)对我来说看起来更干净。

SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );

Date day = dateFormat.parse(string);

// add the day

Date dayAfter = new Date(day.getTime() + TimeUnit.DAYS.toMillis(1));

Jens answered 2018-12-30T16:55:24Z

7 votes

Apache Commons已经有了这个DateUtils.addDays(Date date,int amount)[http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DateUtils.html#addDays% 您使用的是28java.util.Date,%20int%29],或者您可以使用JodaTime使其更清洁。

ROCKY answered 2018-12-30T16:55:47Z

7 votes

只需在String中传递日期和下一天的数量

private String getNextDate(String givenDate,int noOfDays) {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

Calendar cal = Calendar.getInstance();

String nextDaysDate = null;

try {

cal.setTime(dateFormat.parse(givenDate));

cal.add(Calendar.DATE, noOfDays);

nextDaysDate = dateFormat.format(cal.getTime());

} catch (ParseException ex) {

Logger.getLogger(GR_TravelRepublic.class.getName()).log(Level.SEVERE, null, ex);

}finally{

dateFormat = null;

cal = null;

}

return nextDaysDate;

}

LMK answered 2018-12-30T16:56:09Z

7 votes

如果要添加单个时间单位并且您希望其他字段也增加,则可以安全地使用add方法。 见下面的例子:

SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");

Calendar cal = Calendar.getInstance();

cal.set(1970,Calendar.DECEMBER,31);

System.out.println(simpleDateFormat1.format(cal.getTime()));

cal.add(Calendar.DATE, 1);

System.out.println(simpleDateFormat1.format(cal.getTime()));

cal.add(Calendar.DATE, -1);

System.out.println(simpleDateFormat1.format(cal.getTime()));

将打印:

1970-12-31

1971-01-01

1970-12-31

terrmith answered 2018-12-30T16:56:38Z

7 votes

在java 8中,您可以使用LocalDate

LocalDate parsedDate = LocalDate.parse("2015-10-30"); //Parse date from String

LocalDate addedDate = parsedDate.plusDays(1); //Add one to the day field

您可以按如下方式转换为LocalDate对象。

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

您可以将LocalDate格式化为字符串,如下所示。

String str = addedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

Ramesh-X answered 2018-12-30T16:57:13Z

5 votes

使用Calendar API将String转换为Date对象,然后使用Calendar API添加一天。 如果您需要特定的代码示例,请告诉我,我可以更新我的答案。

Ross answered 2018-12-30T16:57:36Z

5 votes

在Java 8中,简单的方法是:

Date.from(Instant.now().plusSeconds(SECONDS_PER_DAY))

dpk answered 2018-12-30T16:57:58Z

4 votes

这很简单,试着用一个简单的词来解释。得到今天的日期如下

Calendar calendar = Calendar.getInstance();

System.out.println(calendar.getTime());// print today's date

calendar.add(Calendar.DATE, 1);

现在通过calendar.add方法提前一天设置此日期,该方法采用(常量,值)。 这里的常数可以是DATE,hours,min,sec等,value是常量的值。 就像有一天一样,前面的常量是Calendar.DATE,它的值是1,因为我们想要提前一天的价值。

System.out.println(calendar.getTime());// print modified date which is明天的日期

谢谢

Kushwaha answered 2018-12-30T16:58:41Z

2 votes

如果您使用的是Java 8,java.time.LocalDate和java.time.format.DateTimeFormatter可以使这项工作变得非常简单。

public String nextDate(String date){

LocalDate parsedDate = LocalDate.parse(date);

LocalDate addedDate = parsedDate.plusDays(1);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd");

return addedDate.format(formatter);

}

realhu answered 2018-12-30T16:59:03Z

2 votes

您可以使用“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);

Shaheed answered 2018-12-30T16:59:25Z

-1 votes

Date newDate = new Date();

newDate.setDate(newDate.getDate()+1);

System.out.println(newDate);

Filip Trajcevski answered 2018-12-30T16:59:41Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值