添加日期到日期 [英] adding days to a date

查看:122
本文介绍了添加日期到日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序需要从1/1/09开始,当我开始新的一天,我的程序将显示第二天。
这是我到目前为止:

  GregorianCalendar startDate = new GregorianCalendar(2009,Calendar.JANUARY,1) ; 
SimpleDateFormat sdf = new SimpleDateFormat(d / M / yyyy);
public void setStart()
{
startDate.setLenient(false);
System.out.println(sdf.format(startDate.getTime()));
}

public void today()
{
newDay = startDate.add(5,1);
System.out.println(newDay);
//我想添加一天到开始的一天,当我开始另一个新的一天,我想添加另一天。
}

我发现错误void but expected int,in'newDay = startDate

解决方案

日历 对象有一个 add 方法,允许添加或减少指定字段的值。



例如,


  
c.add(Calendar.DAY_OF_MONTH,1);

用于指定字段的常量可以在 日历 class。



仅供将来参考, Java API规范包含有关如何使用Java API一部分的许多有用的信息。






更新:


我收到错误,但
预期的int,在'newDay =
startDate.add(5,1);'我该怎么
做?


添加方法不返回任何内容,因此,尝试分配调用 Calendar.add 的结果无效。



编译器错误表示一个人试图将一个 void 分配给一个变量h的类型 int 。这是无效的,因为不能将无分配给 int 变量。



只是一个猜测,但是也许这可能是想要实现的:

  //获取设置为指定日期的日历。 
日历calendar = new GregorianCalendar(2009,Calendar.JANUARY,1);

//获取日历的当前日期表示。
Date startDate = calendar.getTime();

//将日历的日期增加1天。
calendar.add(Calendar.DAY_OF_MONTH,1);

//获取日历的当前日期表示。
Date endDate = calendar.getTime();

System.out.println(startDate);
System.out.println(endDate);

输出:

 code> Thu Jan 01 00:00:00 PST 2009 
Fri Jan 02 00:00:00 PST 2009

需要考虑的是日历实际上是



A 日历不是日期的表示。它是一个日历的表示,它正在指向哪里。为了获取当前日历指向的位置,应该获得一个 日期 日历使用 getTime 方法。


I have a program that needs to start on 1/1/09 and when I start a new day, my program will show the next day. This is what I have so far:

GregorianCalendar startDate = new GregorianCalendar(2009, Calendar.JANUARY, 1);
SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy"); 
public void setStart()
{
    startDate.setLenient(false);
    System.out.println(sdf.format(startDate.getTime()));
}

public void today()
{
    newDay = startDate.add(5, 1);
    System.out.println(newDay);
//I want to add a day to the start day and when I start another new day, I want to add another day to that.
}

I am getting the error found void but expected int, in 'newDay = startDate.add(5, 1);' What should I do?

解决方案

The Calendar object has an add method which allows one to add or subtract values of a specified field.

For example,

Calendar c = new GregorianCalendar(2009, Calendar.JANUARY, 1);
c.add(Calendar.DAY_OF_MONTH, 1);

The constants for specifying the field can be found in the "Field Summary" of the Calendar class.

Just for future reference, The Java API Specification contains a lot of helpful information about how to use the classes which are part of the Java API.


Update:

I am getting the error found void but expected int, in 'newDay = startDate.add(5, 1);' What should I do?

The add method does not return anything, therefore, trying to assign the result of calling Calendar.add is not valid.

The compiler error indicates that one is trying to assign a void to a variable with the type of int. This is not valid, as one cannot assign "nothing" to an int variable.

Just a guess, but perhaps this may be what is trying to be achieved:

// Get a calendar which is set to a specified date.
Calendar calendar = new GregorianCalendar(2009, Calendar.JANUARY, 1);

// Get the current date representation of the calendar.
Date startDate = calendar.getTime();

// Increment the calendar's date by 1 day.
calendar.add(Calendar.DAY_OF_MONTH, 1);

// Get the current date representation of the calendar.
Date endDate = calendar.getTime();

System.out.println(startDate);
System.out.println(endDate);

Output:

Thu Jan 01 00:00:00 PST 2009
Fri Jan 02 00:00:00 PST 2009

What needs to be considered is what Calendar actually is.

A Calendar is not a representation of a date. It is a representation of a calendar, and where it is currently pointing at. In order to get a representation of where the calendar is pointing at at the moment, one should obtain a Date from the Calendar using the getTime method.

这篇关于添加日期到日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆