0

One part of my assignment requires me to use gregorian calendar in the student class.

Every student has a unique studentNumber (int), name (String), **

dateRegistered (GregorianCalendar)

** , id (String) and courseEnrolled (CourseOffering).

import java.util.Calendar;
import java.util.GregorianCalendar;

public abstract class Student
{
    private int studentNumber;
    private String name;
    private String id;
    private CourseOffering courseEnrolled;
    private GregorianCalendar startDate = new GregorianCalendar();

    public Student( int studentNumber, String name, String id, CourseOffering courseEnrolled){
        this.studentNumber = studentNumber;
        this.name = name;
        this.id = id;
        this.courseEnrolled = courseEnrolled;
    }

    public int getStudentNumber(){
        return studentNumber;
    }

    public String toString(){
        return String.format("%08d", studentNumber) + "          " + name + "     " + id;
    }

}

I wasn't taught on how to use the calendar in school yet, but this came up for the assignment.

"The constructor that does all the necessary initializations given all necessary values including the day (int), month (int) and year (int) of the registration date "

How do i put the day month year in the constructor ? Help guys :(

Kerry
  • 1
  • 3
  • 1
    We're not going to do your homework, but if you google `java 8 gregoriancalendar api`, you'll get [this page](https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html) which basically tells you everything you need to know. If you don't know how a `class` works, google `java 8 myclass api`. – Millie Smith Mar 28 '15 at 20:37
  • well i wasnt asking for anyone to do my homework for me ? i was just listing out the requirements of this small part of my assignment so that people reading this could have a better understanding of what i am trying to ask. and there thats what i needed, some information of or guideline to 'learn more about' the gregorian calendar. – Kerry Mar 29 '15 at 07:30
  • Sorry, you're right, I jumped to conclusions. Go here, and find the constructor you need: https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html#GregorianCalendar-- – Millie Smith Mar 29 '15 at 15:11
  • I've read about that, but dont really understand how it actually works. do i create another constructor for it like this ? public Student( int studentNumber, String name, String id, CourseOffering courseEnrolled){ this.studentNumber = studentNumber; this.name = name; this.id = id; this.courseEnrolled = courseEnrolled; } public Student(int year,int month,int dayOfMonth){ startDate = new GregorianCalendar(year, month, dayOfMonth); } – Kerry Mar 31 '15 at 06:21
  • It should be startDate = new GregorianCalendar(year, month-1, dayOfMonth) – ravthiru Aug 12 '16 at 01:32

1 Answers1

0

tl;dr

LocalDate.of( 2018 , 1 , 23 )  // January 23, 2018.

Use java.time classes instead. Unlike the legacy classes, these modern classes use sane numbering:

  • 2018 is year 2018.
  • 1-12 for January-December.
  • 1-7 for Monday-Sunday.

The java.time classes use static factory methods rather than new constructor calls.

ZonedDateTime

The GregorianCalendar class is now obsolete (good riddance). Replaced by ZonedDateTime.

ZonedDateTime  zdt = 
    ZonedDateTime.of(
        LocalDate.of( 2018 , Month.JANUARY , 23 ) ,
        LocalTime.of( 18 , 30 ) ,
        ZoneId.of( "Africa/Tunis" )
    )
;

zdt.toString(): 2018-01-23T18:30+01:00[Africa/Tunis]

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

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" ) ;  
LocalDate today = LocalDate.now( z ) ;

If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM.

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.

LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ;  // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.

Or, better, use the Month enum objects pre-defined, one for each month of the year. Tip: Use these Month objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety.

LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;

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.

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