For my current assessment (see prev blog entry), I have to store a start date and end date for (some) of my employees. Eddie's suggestion is to store the startDate and endDate as type Date, and create an instance of Calendar for any calculations.
As far as I can work from the limited examples in the lecture, this is the easiest way to have a date (I'll use the previous example of Student, Unit, University, as I'm pretty sure I shouldn't blog about what's going in my assessable work!). There's nothing in the study guide or the textbooks about dates, stupid lack of reference material.
To create a Date or Calendar object, you need to import the classes - they're both in java.util.*
import java.util.Date;
import java.util.GregorianCalendar;
public class Student
{
private String id;
private String name;
private int age;
private Date enrolledDate;
public Student()
{
id = "";
name = "";
age = 0;
GregorianCalendar calendar1 = new GregorianCalendar();
calendar1.set(2011, 1, 28, 9, 0, 0);
enrolledDate = calendar1.getTime();
}
public Student (String id, String name, int age, int enrolledYear, int enrolledMonth, int enrolledDay)
{
this.id = id;
this.name = name;
this.age = age;
GregorianCalendar calendar1 = new GregorianCalendar();
calendar1.set(enrolledYear, enrolledMonth, enrolledDay, 9, 0, 0);
enrolledDate = calendar1.getTime();
}
...
}I DON'T LIKE THIS! It just feels pointless. Why do I have to have both a Date and a Calendar.
Could I do this with just a calendar? Is there any reason to use the Date?
Assessments suck.
Oh, and the Calendar object sucks too - for the Day of Month and Year, you enter them correctly, but for Month, you start counting at 0 (such a java thing), so January is 0. So to create the date 01/01/11, it's
calendar.set(2011,0,1).grrrr.
UPDATE: I think I found a better way to do the date thing
public Student()
{
id = "";
name = "";
age = 0;
this.enrolledDate = new GregorianCalendar(2011, 0, 3).getTime();
}It looks much simpler...
No comments:
Post a Comment