I'm not trying to format the date in YYYY-MM-DD or dd/MM/YYYY. I'm asking about the literal format of LocalDate.
I just started learning Java and I am using this IDE called BlueJ. and I want to create a test method.
The screenshot will show what I am trying to do
Now since from the constructor we know that it requires a int, LocalDate and a double. I've searched online and found that
java.time.LocalDate: A LocalDate instance holds a date without a time zone, in ISO-86011 calendar system. LocalDate has the default format ‘YYYY-MM-DD’ as in ‘2016-12-12’.
So I would put a normal number in 10001 for the testID and double would be something like 50.5 I also know that for it to register a string (if it was needed) I would need to enclose it within "string"
But I've tried all sorts of way to put in the date and I would be left with an error
2018-05-30,30-05-2018,30/05/2018 would give me
Error: incompatible types: Int cannot be converted to java.time.LocalDate
"30/05/2018" on the other hand would give me
Error: Incompatible types: java.lang.String cannot be converted to java.time.LocalDate
If I try 30.05.2018 it would say
Error: ';' expected
If I try '2018-05-30' it would say
Error: unclosed character literal
I ran out of ways to try it. So if you could tell me how I should put it in there, that would be great.
I just really need to know how BlueJ wants me to input it. Cause the resources for BlueJ is so sparse online.
Code:
import java.time.LocalDate;
import java.util.ArrayList;
/**
* Write a description of class TestPaper here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TestPaper
{
// instance variables - replace the example below with your own
private int testID;
private LocalDate testDate;
private double testMarks;
private ArrayList<MCQ> MCQDetails;
/**
* Constructor for objects of class TestPaper
*/
public TestPaper(int testID, LocalDate testDate, double testMarks)
{
this.testID = testID;
this.testDate = testDate;
this.testMarks = testMarks;
MCQDetails = new ArrayList<MCQ>() ;
}
/**
* Accessor Method getTestID to get the testID
*
* @return int value of the choice ID
*/
public int getTestID(){
return testID;
}
/**
* Mutator Method to set the testID
*
* @param int format of the testID to set
*/
public void setTestID(int testID){
this.testID = testID;
}
/**
* Accessor Method getTestMarks to get the Test Marks
*
* @return double value of the test marks
*/
public double getTestMarks(){
return testMarks;
}
/**
* Mutator Method to set the testMarks
*
* @param String format of the choice Description to be set
*/
public void setTestMarks(double testMarks){
this.testMarks = testMarks;
}
/**
* Accessor Method getTestDate to get the testDate
*
* @return LocalDate value of the testDate
*/
public LocalDate getTestDate(){
return testDate;
}
/**
* Mutator Method to set the testDate
*
* @param LocalDate format of the testDate to set
*/
public void setTestDate(LocalDate testDate){
this.testDate = testDate;
}
/**
* Method addMCQ will allow users to add a MCQ Object to the list of MCQ
*
* @param addMCQ a MCQ Object
* @return boolean will return true if it is successfully added or false if not
*/
public boolean addMCQ(MCQ MCQName)
{
return MCQDetails.add(MCQName);
}
/**
* Method removeMCQ to remove an MCQ object from the Arraylist
*
* @param MCQName A parameter of type MCQ
*/
public void removeMCQ(MCQ MCQName)
{
MCQDetails.remove(MCQName);
}
/**
* Method listMCQ to return a list of MCQ arraylist
*
* @return The return value of MCQDetails (MCQ Arraylist)
*/
public ArrayList<MCQ> listMCQ()
{
return MCQDetails;
}
public MCQ findMCQ(int MCQID)
{
for(MCQ m : MCQDetails)
{
if(m.getQuestionID() == MCQID)
{
return m;
}
}
return null;
}
Try converting the LocalDate in the call, such as:
There are other static methods within LocalDate you can use. See here for more examples.
From your comment above, don't forget your import:
Include package
As discussed in the comments, the solution is to add the code that creates the
LocaDate
, but bluej needs the fully qualified class name with the package prefix “java.time.”:Not sure why it doesn't work with just
LocalDate.of(...)
(even with the class correclty imported), but at least this works.Just another detail: a date has no format. Classes like
LocalDate
just holds values (in this case, it has year, month and day values), but a date itself has no format at all. The same date can be represented in many different formats:May 30th 2018
,2018-05-30
,30/05/18
are different formats, but all represent the same date. A date object just holds the values, and you can choose whatever format you want to represent it.When you print a
LocalDate
, it implicity callstoString()
, which by default choosesyyyy-MM-dd
format, which is a ISO 8601 format, but as I said, that's just one of the many possible ways to format a date (although the value always stays the same). Telling that "a date has a format" is wrong and misleading.