繁体   English   中英

获得当月的日子

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM