0

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 +
                '}';
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
eldix_
  • 127
  • 4
  • 20
  • 3
    And your question is? – shmosel Mar 31 '17 at 08:01
  • I want to create a method that can do what's on the paragraph above. I don't have an idea of how to go about it. – eldix_ Mar 31 '17 at 08:49
  • The requirements are very unclear. In a sense, your `Calendars` class is already fulfilling what you are say you want, so no doubt there are some additional requirements that you have not told us? – Ole V.V. Mar 31 '17 at 10:35
  • What Java version are you using? If you can use Java 8, drop the use of `Date` completely. Look into `YearMonth` or `LocalDate`. – Ole V.V. Mar 31 '17 at 10:38

1 Answers1

1

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154