1

I'm trying to understand the logic of formatting a date and adding it as a parameter for my objects however finding it difficult. I am trying to use SimpleDateFormat..

myClassroom.addEnrolment(new Classroom("502","424"));
myClassroom.addEnrolment(new Classroom("503","425"));
myClassroom.addEnrolment(new Classroom("504","426"));   

but trying to achieve it accepting

myClassroom.addEnrolment(new Classroom("502","424", "2020-05-12"));
myClassroom.addEnrolment(new Classroom("503","425", "2020-04-22"));
myClassroom.addEnrolment(new Classroom("504","426", "2020-05-05"));

I have imported both..

import java.text.SimpleDateFormat;
import java.util.Date;

and have added as parameters/initialized in the Classroom class. Where do I go from here? Appreciate any help I can't seem to find this info

EDIT the attributes of Classroom:

import java.util.Date;

public class Classroom{      
     private String studID;
     private String subjectID;
     private Date dateEnroled;

     public Classroom(String studID, String subjectID, Date dateEnroled)
     {  
        this.studID = studID; 
        this.courseID = subjectID;
        this.dateEnroled = dateEnroled;
     }
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Wihoo
  • 39
  • 4
  • 1
    see [here](https://stackoverflow.com/a/37506250) for how to parse a date in that format into a Date object. – fpezzini May 07 '20 at 11:42
  • What are you trying to achieve? – akuzminykh May 07 '20 at 11:46
  • 1
    Post some more details e.g. the attributes list of `Classroom`, what you want to do etc. Also, do not use outdated `SimpleDateFormat`; use `DateTimeFormatter` instead. – Arvind Kumar Avinash May 07 '20 at 11:50
  • @ArvindKumarAvinash I've added the attributes – Wihoo May 07 '20 at 11:56
  • @akuzminykh Trying to achieve it accepting date as part of the enrolment. The tutorials I find only show how to do it for one date – Wihoo May 07 '20 at 11:57
  • 2
    Do not use outdated `Date` and `SimpleDateFormat`. Use `LocalDate` and `DateTimeFormatter` instead. Check [this](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html) for more details. – Arvind Kumar Avinash May 07 '20 at 11:57

2 Answers2

1

As I have mentioned in comments, do not use outdated Date and SimpleDateFormat. Use LocalDate and DateTimeFormatter instead. Check this for more details.

Do it as follows:

import java.time.LocalDate;

class Classroom {
    private String studID;
    private String subjectID;
    private LocalDate dateEnroled;

    public Classroom(String studID, String subjectID, LocalDate dateEnroled) {
        this.studID = studID;
        this.subjectID = subjectID;
        this.dateEnroled = dateEnroled;
    }
    // Getters, setters, hashCode, equals and toString
}

public class Main {
    public static void main(String[] args) {
        myClassroom.addEnrolment(new Classroom("502","424", LocalDate.parse("2020-05-12"));
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

You need a constructor that accepts a string, something like this one:

public Classroom(String studID, String subjectID, String dateEnroled)
{  
    this.studID = studID; 
    this.courseID = subjectID;
    this.dateEnroled = new java.text.SimpleDateFormat("yyyy-MM-dd")
                       .parse(dateEnroled);
}
Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
  • 3
    Do not encourage to use outdated `Date` and `SimpleDateFormat`. You should have given some example using `LocalDat`e and `DateTimeFormatter`. – Arvind Kumar Avinash May 07 '20 at 12:00
  • @ArvindKumarAvinash Actually the original question was about `Date` and `SimpleDateFormat`, so this is what I use in my answer. I am not here to evangelize people to the third or fourth attempt for a proper date/time library in java ;-) – Erich Kitzmueller May 07 '20 at 12:07