[英]Getting days in a current month
我想像這樣顯示arraylist:
[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;
}
}
這里傳遞靜態年,月,日為方法作為參數通過該值打印日期,如yyyy-MM-dd
格式,當我通過1天,然后7天后打印但是上面的代碼不能正常工作給我正確的代碼
這里有兩件事需要改變。
第一,
while (month==cal.get(Calendar.MONTH)) {
需要改為
while (month==cal.get(Calendar.MONTH)+1) {
因為根據文件
格列高利歷和朱利安歷的一年中的第一個月是1月,即0;
第二件事是使你的ArrayList
類型為String
而不是Date
,因為Date
沒有格式。 您只能獲得格式化的String表示形式。
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);
您需要進行以下更改:
從
while (month==cal.get(Calendar.MONTH)) {
至
while ((month-1)==cal.get(Calendar.MONTH)) {
在Calendar.class ==> NOVEMBER中查看更多信息, 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;
RJ的答案和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-23
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.