How to enter a LocalDate value into BlueJ "Create Object" dialog box - java

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
https://www.javabrahman.com/java-8/java-8-working-with-localdate-localtime-localdatetime-tutorial-with-examples/
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;
}

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.”:
java.time.LocalDate.of(2018, 5, 30)
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 calls toString(), which by default chooses yyyy-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.

Try converting the LocalDate in the call, such as:
TestPaper (2018-05-30, LocalDate.parse("2018/05/30"), 30/05/2018);
There are other static methods within LocalDate you can use. See here for more examples.
From your comment above, don't forget your import:
import java.time.LocalDate;

Related

What is the format to add java Localdate parameters when creating objects in BlueJ [duplicate]

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
https://www.javabrahman.com/java-8/java-8-working-with-localdate-localtime-localdatetime-tutorial-with-examples/
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;
}
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.”:
java.time.LocalDate.of(2018, 5, 30)
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 calls toString(), which by default chooses yyyy-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.
Try converting the LocalDate in the call, such as:
TestPaper (2018-05-30, LocalDate.parse("2018/05/30"), 30/05/2018);
There are other static methods within LocalDate you can use. See here for more examples.
From your comment above, don't forget your import:
import java.time.LocalDate;

How to write Javadoc for a getInstance method?

Say I have something like this:
public class MyClass {
private static MyClass sInstance;
/**
*
* #return The {#link MyClass} application instance.
*/
public static MyClass getInstance() {
return sInstance;
}
}
IntelliJ gives me this warning:
'#link' pointing to containing class is unnecessary
What's the proper/conventional way to write this piece of Javadoc?
How would you write it?
In the JDK, they use {#code}. That does not make a clickable link, but you are already looking at the page that would be linked anyway.
For example (from String.java):
/**
* Initializes a newly created {#code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {#code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* #param original
* A {#code String}
*/
You only get the warning because the link won't go anywhere. Just change it to {#code MyClass} to keep the formatting but without the link.
Here are some example getInstance() methods from the JDK.
java.text.Collator:
/**
* Gets the Collator for the current default locale.
* The default locale is determined by java.util.Locale.getDefault.
* #return the Collator for the default locale.(for example, en_US)
* #see java.util.Locale#getDefault
*/
public static synchronized Collator getInstance() {
java.text.NumberFormat:
/**
* Returns a general-purpose number format for the current default
* {#link java.util.Locale.Category#FORMAT FORMAT} locale.
* This is the same as calling
* {#link #getNumberInstance() getNumberInstance()}.
*
* #return the {#code NumberFormat} instance for general-purpose number
* formatting
*/
public final static NumberFormat getInstance() {

Test Serialization and Deserialization of objects in Java

Earlier, I had a java class:
public enum Currency
{
PENNY,
NICKLE,
DIME,
QUARTER;
}
Now I have made changes in the class and it looks like this:
public enum Currency
{
PENNY("Penny"),
NICKLE("Nickle"),
DIME("Dime"),
QUARTER("Quarter");
/**
* Instantiates a new currency.
*
* #param currencyType the currency type
*/
Currency(String currencyType)
{
this.value = currencyType;
}
String value;
/**
* Gets the value
*
*/
public String getValue()
{
return this.value;
}
}
I want to check if the Currency class object is getting serialized and deserialized properly. How should I check this through unit testing in java ?
I'm using DynamoDB to store the values of Currency. Currently they are being stored as Enum constants. Now when I have customized the enum's and added the value field. As far as I understood, serialization and deserialization is necessary when data transfer takes place through network. Please correct me here if I'm wrong. Also I'm using this value field to write to excel file using Apache POI. My question is the serialization and deseralization check necessary here for the custom enum class here ?

How to keep track of non empty field in Joda date buffer

I'm writing a set of objects that should be able to alter fields in a Joda Time MutableDateTime instance.
Each object is applied in sequence to the buffer, and when all that set is applied, a complete valid MutableDateTime will be built.
Each instance have to be able to know which date time fields has already been set by other instances in the set.
I get stucked because I get following problems:
How to create the MutableDateTime instance with empty values in all
date time fields in order to use it as the initial value to build?
How could I know if some field of MutableDateTime has been set?
MutableDateTime internally keep track it's data in a long instance field initialized to number of milliseconds elapsed from start of era to now. It thus has all field already set to some value.
Do you know if MutableDateTime has some concept of an empty value?
Edit:
as I show in my response, I develop a solution using a manager class as Vladimir suggested.
You should create "Manager" class to remember fields which was already set. It should throw exception if user tries to retrieve instance of MutableDateTime before all fields was set.
And if you always set all fields for MutableDateTime then [1] is not important (values will be overwriten).
I finally changed my initial design, and I implemented it exactly as Vadim Ponomarev suggested. Since each field type in Joda buffer has a corresponding DateTimeFieldType instance, I use a private Set object to keep track of the fields present.
The code below show how I've done:
private final Set<DateTimeFieldType> fieldTypes = Sets.newHashSet();
/**
* Allow to set to or reset one of the DateTimeFieldType fields
* #param fieldType the DateTimeFieldType field to change
* #param value the value to set it
*/
public void changeField(DateTimeFieldType fieldType, boolean value) {
if (value)
fieldTypes.add(fieldType);
else
fieldTypes.remove(fieldType);
}
/**
* Check if one of the DateTimeFieldType is present in this set.
* #param fieldType The field type to check for presence.
* #return true if the DateTimeFieldType is present, otherwise false
*/
public boolean isFieldSet(DateTimeFieldType fieldType) {
return !fieldTypes.contains(fieldType);
}
I've also added some utility methods allowing to change all fields for the date and all fields for the time at once. This could be useful in client to code to easy a common operation on date field sets.
/**
* Allow to set the fields that build the time part
* of a date time
* <p/>
*
* #param value value to set the DateTime fields
*/
public void changeTimeFields(boolean value) {
changeField(DateTimeFieldType.hourOfDay(), value);
changeField(DateTimeFieldType.minuteOfHour(), value);
}
/**
* Allow to set the fields that build the date part
* of a date time
* <p/>
*
* #param value value to set the DateTime fields
*/
public void changeDateFields(boolean value) {
changeField(DateTimeFieldType.dayOfMonth(), value);
changeField(DateTimeFieldType.monthOfYear(), value);
changeField(DateTimeFieldType.yearOfEra(), value);
}
And finally, I also added some method to query if all date fields are set and if all time fields are set:
/**
* Allow to check if the DateTimeFieldType fields that build the
* date part of a datetime has been set in this instance.
* <p/>
*
* #return true if date part has yet to be applied to
* the instance, false otherwise
*/
public boolean isDateSet() {
return fieldTypes.contains(DateTimeFieldType.dayOfMonth()) &&
fieldTypes.contains(DateTimeFieldType.monthOfYear()) &&
fieldTypes.contains(DateTimeFieldType.yearOfEra());
}
/**
* Allow to check if the DateTimeFieldType fields that build the
* time part of a datetime has been set in this instance.
* <p/>
*
* #return true if time part has yet to be applied to
* the instance, false otherwise
*/
public boolean isTimeSet() {
return fieldTypes.contains(DateTimeFieldType.minuteOfHour()) &&
fieldTypes.contains(DateTimeFieldType.hourOfDay());
}
I finally made it a DateTimeFieldTypeSet class. I think it encapsulate well a common concept that is lacking in Joda classes. I hope it can be useful to some one else too.

Narrow a Joda-Time partial

What's the best way to construct a partial from another partial which conains all the necessary fields (e.g. YearMonth from LocalDate? One way I can see is to convert to a full instant and back, that is
YearMonth ld2ym(LocalDate ld) {
return new YearMonth(ld.toDateTime(DateTime.now()));
}
but it seems like a more efficient way should be possible.
On the YearMonth class we can find this method which seems to give the appropriate solution:
/**
* Parses a {#code YearMonth} from the specified string using a formatter.
*
* #param str the string to parse, not null
* #param formatter the formatter to use, not null
* #since 2.0
*/
public static YearMonth parse(String str, DateTimeFormatter formatter) {
LocalDate date = formatter.parseLocalDate(str);
return new YearMonth(date.getYear(), date.getMonthOfYear());
}

Categories