我想像这样显示数组列表:
[2013-11-01,2013-11-8,2013-11-15,2013-11-22,2013-11-29]我编写了以下代码,并将静态值传递给该方法:
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class DateExample {
Calendar cal = Calendar.getInstance();
public static void main(String[] args) {
DateExample date=new DateExample();
date.getAllDaysInaMonth("2013",11,1);
}
public List<java.sql.Date> getAllDaysInaMonth(String year,int month,int day){
System.out.println(year+""+month+""+day);
cal.set(Calendar.DAY_OF_MONTH, 1);
int utilyear=cal.get(cal.YEAR);
String syear= Integer.toString(utilyear);
int utilmonth=cal.get(cal.MONTH);
int utilday=cal.get(cal.DAY_OF_MONTH);
List<java.sql.Date> arraylist=new ArrayList<java.sql.Date>();
while (month==cal.get(Calendar.MONTH)) {
System.out.println("while"+cal.getTime());
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
String dateofutil=format.format(cal.getTime());
System.out.println("dateofutil"+dateofutil);
try {
java.sql.Date sqldate=new java.sql.Date(format.parse(dateofutil).getTime());
arraylist.add(sqldate);
} catch (ParseException e) {
e.printStackTrace();
}
cal.add(Calendar.DAY_OF_MONTH,1);
}
System.out.println("arraylist values"+arraylist);
return arraylist;
}
}在这里,我将静态年、月、日期作为参数传递给方法,当我经过1天后,打印出类似yyyy-MM-dd格式的日期,然后在7天后打印日期,但上面的代码不能正常工作,请给我正确的代码
发布于 2013-11-20 18:46:04
这里有两件事需要改变。
第一,
while (month==cal.get(Calendar.MONTH)) {需要更改为
while (month==cal.get(Calendar.MONTH)+1) {因为根据docs
在公历和儒略历中,一年的第一个月是一月,也就是0;
第二件事是使您的ArrayList类型为String而不是Date,因为Date没有格式。您只能获得它的格式化字符串表示。
List<String> arraylist = new ArrayList<String>();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // This can go out of the `while` loop though.
String dateofutil = format.format(cal.getTime());
arraylist.add(dateofutil);发布于 2013-11-20 18:47:26
您需要进行以下更改:
从…
while (month==cal.get(Calendar.MONTH)) {至
while ((month-1)==cal.get(Calendar.MONTH)) {在Calendar.class ==> 11月份,it is 10 rather 11上可以看到更多信息。这就是为什么之前你没有得到预期的结果。更改它,然后继续。
public final static int NOVEMBER = 10;
/**
* Value of the {@link #MONTH} field indicating the
* twelfth month of the year.
*/
public final static int DECEMBER = 11;
/**
* Value of the {@link #MONTH} field indicating the
* thirteenth month of the year. Although <code>GregorianCalendar</code>
* does not use this value, lunar calendars do.
*/
public final static int UNDECIMBER = 12;发布于 2014-01-07 07:45:44
answer by R.J和answer by MouseLearnJava均正确。
使用Joda-Time库可以更轻松地完成这类日期-时间工作。
首先,Joda-Time不同于java.util.Calendar的从零开始的愚蠢。因此,一周的天数是1-7,月份是1-12。
下面是一些使用Joda-Time 2.3和Java 7的示例代码。
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
// Data passed into method.
int year = 2014;
int month = 2;
int day = 2;
java.util.List<DateTime> dateTimes = new ArrayList<DateTime>();
DateTime start = new DateTime( year, month, day, 0, 0, 0 );
DateTime dateTime = start;
while( dateTime.monthOfYear().equals( start.monthOfYear() ) ) {
dateTimes.add( dateTime ); // Collect each date-time object.
dateTime = dateTime.plusDays( 7 );
}
System.out.println( "The list: " + dateTimes );
for( DateTime item : dateTimes ){
System.out.println( ISODateTimeFormat.date().print( item ) );
}运行…时
The list: [2014-02-02T00:00:00.000-08:00, 2014-02-09T00:00:00.000-08:00, 2014-02-16T00:00:00.000-08:00, 2014-02-23T00:00:00.000-08:00]
2014-02-02
2014-02-09
2014-02-16
2014-02-23https://stackoverflow.com/questions/20093594
复制相似问题