简体   繁体   中英

How to generate Calendars in java?

I need to build a backend Java class that generates Calenders. The Calendar has 12 months to complete a year. In that year, the first month is July and the last month is June. In a sense, the months in the generated calender will have a different years. For instance, the first month will be July-2016 and the last month will be June-2017 and so on. Below is what I have so far.

public class Calendars {

    public Date startDate;
    public Date endDate;
    public String periodName;//This is the name of the year(e.g July-2016 to June-2017 may be called period01)
    public short physicalYear;//This is the normal year

    public Calendars() {
    }

    public Calendars(Date startDate, Date endDate, String periodName, short physicalYear) {
        this.startDate = startDate;
        this.endDate = endDate;
        this.periodName = periodName;
        this.physicalYear = physicalYear;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public String getPeriodName() {
        return periodName;
    }

    public void setPeriodName(String periodName) {
        this.periodName = periodName;
    }

    public short getPhysicalYear() {
        return physicalYear;
    }

    public void setPhysicalYear(short physicalYear) {
        this.physicalYear = physicalYear;
    }

    @Override
    public String toString() {
        return "Calendars{" +
                "startDate=" + startDate +
                ", endDate=" + endDate +
                ", periodName='" + periodName + '\'' +
                ", physicalYear=" + physicalYear +
                '}';
    }
}

tl;dr

YearMonth.of( 2017 , Month.JULY )
         .plusYears( 1 )

Avoid legacy date-time classes

Avoid the old legacy date-time classes such as Date & Calendar . They are troublesome, confusing, poorly designed, and flawed. Now supplanted by the java.time classes.

YearMonth

You seem to want to track a span of time as whole months. For that use the YearMonth class.

Note that unlike the legacy classes, in java.time the months have sane numbering, 1-12 for January-December.

YearMonth start = YearMonth.of( 2017 , 7 );
YearMonth stop = start.plusYears( 1 );

Or specify the month using the handy Month enum.

YearMonth start = YearMonth.of( 2017 , Month.JULY );

If you want the current YearMonth , specify a time zone. Remember that for any given moment, the date varies around the globe by zone, and therefore the month may vary.

Specify a proper time zone name in the format of continent/region , such as America/Montreal , Africa/Casablanca , or Pacific/Auckland . Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
YearMonth ym = YearMonth.now( z );

LocalDate

To work with individual dates, use the LocalDate class.

LocalDate firstOfMonth = ym.atDay( 1 );
LocalDate dayAfter = firstOfMonth.plusDays( 1 );
LocalDate endOfMonth = ym.atEndOfMonth();

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification is JSR 310 .

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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