• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Retrieving and validating a date. DD/MM/YYYY at HH:MM

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!
I can't seem to find the right answer when it comes to retrieving and validating a date in java. I was able to find some answers but they only apply to the current date which is not what I want (although it's probably very similar).
So, what I'd like to ask for, is for you to give me some tips on how to for example, after declaring all the date parameters, i.e. that the Assignment is due for 23/11/2016 at 23:59, I am able to retrieve the day, the month, the minute, etc. individually.

Some rules to keep in mind:
a. None of the fields should be blank.
b. Any year fields must be a four digit number (i.e. greater than 999) and
less than or equal to 2016.
c. Any month field should be a number between 1 and 12 inclusive.
d. Any day field should be a number between 1 and 31 inclusive where
the month is January (1), March (3), May (5), July (7), August (8),
October (10), and December (12), between 1 and 30 inclusive where
the month is April (4), June (6), September (9), and November (11),
and between 1 and 28 for February (2). We are ignoring leap years for
this exercise.
e. The hour should be a number between 0 and 23 inclusive.
f. The minute should be a value between 0 and 59 inclusive

There is also a toString method which I'll have to carry out later on that includes the date mentioned above and I was wondering what would be the most efficient way of writing it. So far I've got that (below) and I'm stuck on the date part, hence the question above.

"The class should have a toString() method that will return a String of the form “<module code>: <assignment title> due on dd/mm/yyyy at hh:mm” where <module code> is the module code for the associated module, <assignment title> is the tile od the assignment, dd/mm/yyyy is the date on which the assignment is due, and hh:mm is the time when the assignment is due."



Hopefully you understand what I'm asking for here but if you have any further questions then I'll be more than happy to answer.
Thanks.
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using Java 8? Can you post what you have so far?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marshall Mathers wrote:So, what I'd like to ask for, is for you to give me some tips on how to for example, after declaring all the date parameters, i.e. that the Assignment is due for 23/11/2016 at 23:59, I am able to retrieve the day, the month, the minute, etc. individually.


OK, well that depends on TONS of things,

Do you want to validate a String containing that date (in whatever format) or do you want to prompt someone to input the individual components and validate them one by one? The two are totally different questions.

However, you might do worse than look at SimpleDateFormat (←click) for starters.

HIH

Winston
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:



When it comes to Java, I'm using 1.7.9
And now when it comes to how the date should work, all the parameters have to act as integers at some point or another, i.e. int Day, int Year, etc.
I don't think that prompting for input will be necessary as I'm going to set the 'due date for the assignment' straight away while coding. It will be used for later display but not editing by the user.
Here are some of the additional things to keep in mind that the program has to be able to do later on:

1. getAssignmentDay: This method should return the day that the current
assignment is due. There are no parameters for this method. If there is
no current assignment, it should return 0.
2. getAssignmentMonth: This method should return the month that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0

Hopefully, that clears some things out but if you have any further questions then by all means ask.
Nevertheless, thank you for the current input.

//
Btw., do you think that what you mentioned, SimpleDateFormat would work in this case? I remember trying to apply it to my task but I was never taught how to use it properly and that's why I came here for help, hoping to find an answer and learn something new along the way, and SimpleDateFormat might just be the thing that I need.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think SimpleDateFormat will work in this situation, but in Java 7 it's a little convoluted.

* Create a SimpleDateFormat object with a format string (see docs)
* Use this object to parse a java.util.Date
* Get a Calendar instance
* Set the "time" using the Date getTime()
* Use Calendar instance to get specific parts of the date.

(Java experts: is there a simpler way?)

I'm happy to explain more, but first I'd like to see what you can do with what I've given and the Java docs. Post your code here.
 
Marshal
Posts: 80230
424
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marshall Mathers wrote:. . . I'm using [JDK]1.7.9 . . .

That is probably not the most recent version; if you plan to let any of your code loose on an unsuspecting world, you should update your Java® installation. I would go for 8u73/74.
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:I think SimpleDateFormat will work in this situation, but in Java 7 it's a little convoluted.

I'm happy to explain more, but first I'd like to see what you can do with what I've given and the Java docs. Post your code here.



Ok so first off I just wanted to say that I updated Java to the version mentioned above so that shouldn't cause any problems.
Secondly, this: http://docs.oracle.com/javase/8/docs/api/java/util/Date.html seems to have everything that I need because it has all the getters and setters, toString as well as equals, which are all needed for the assignment.
However, when I try to set the date, it seems to just give me an error.
I tried to create that SimpleDateFormat in various ways but just can't seem to get it to work, maybe if I show you the code, you can tell me why. I'm still a beginner to Java so I'm probably missing some important step.
Thanks for the help.

Code so far (just for the 'Assignment' class):

... and all the other getters and setters.

*createAssignment and toString still have some parts missing from it to work.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're going to use Java 8, then things change -- they actually get easier.

* Create a DateTimeFormatter with the static ofPattern(PATTERN) method.
* Create a LocalDateTime object with the static parse(DATE_STRING, FORMATTER) method.
* Retrieve data from the LocalDateTime object, e.g. getYear().

* Your date format string is close. If the String to parse is "23/11/2016 at 23:59", the formatter should be "dd/MM/yyyy 'at' HH:mm". The single quote (') surrounds a literal.

* If you override equals(), you should override hash() too.
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:If you're going to use Java 8, then things change -- they actually get easier.

* Create a DateTimeFormatter with the static ofPattern(PATTERN) method.
* Create a LocalDateTime object with the static parse(DATE_STRING, FORMATTER) method.
* Retrieve data from the LocalDateTime object, e.g. getYear().

* Your date format string is close. If the String to parse is "23/11/2016 at 23:59", the formatter should be "dd/MM/yyyy 'at' HH:mm". The single quote (') surrounds a literal.



Thank you.
This is what I have so far:


But I am obviously making a mistake somewhere since I'm not linking the formatter with the LDT as this is what I was supposed to do right? Also, I am not entirely sure whether I am declaring the parameters correctly (or even if there is a need to do because maybe this is what the formatter was created for?). A little explanation would be useful here.
And when it comes to the toString and equals, I'll leave them for now and worry about them later. I'll just focus on being able to declare the date and then retrieve all its parts properly.
Once again, thanks.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use the parse() method of LocalDateTime. Assuming your date string is in a variable called dateString...
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:You should use the parse() method of LocalDateTime. Assuming your date string is in a variable called dateString...



Is this the correct way to do this?



If so, where should I now declare what each of the parameters is (as in day = 10, etc.)?
If not, then where have I gone wrong?

Thanks for all the help and sorry for being such a newbie.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.CharSequence is a good guess for the type of dateString, but it can just be a String.

This is assuming that you're setting the date string yourself. Posting your latest code will help with the context.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Some rules to keep in mind:
a. None of the fields should be blank.
b. Any year fields must be a four digit number (i.e. greater than 999) and
less than or equal to 2016.
c. Any month field should be a number between 1 and 12 inclusive.
d. Any day field should be a number between 1 and 31 inclusive where
the month is January (1), March (3), May (5), July (7), August (8),
October (10), and December (12), between 1 and 30 inclusive where
the month is April (4), June (6), September (9), and November (11),
and between 1 and 28 for February (2). We are ignoring leap years for
this exercise.
e. The hour should be a number between 0 and 23 inclusive.
f. The minute should be a value between 0 and 59 inclusive



It could be just me, but the above bit from the OP does not read like "use the existing Date Time functionality in Java".
That just reads like the task is to create a fairly simple date/time thing.

I could be wrong...it has been known.
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:java.lang.CharSequence is a good guess for the type of dateString, but it can just be a String.

This is assuming that you're setting the date string yourself. Posting your latest code will help with the context.



Ok so I don't seem to have any errors now when it comes to the formatter, a part from the 'dateTime' not being used.
To make things clearer, I'll post the main requirements for the assignment so that then you can understand it more and I'll show you all the declarations as well as the getters and setters that I've made so far. The only thing that I'm wondering on how to do now is how to use that LocalDateTime to actually retrieve each part of the date? Cos my getMonth is not going to retrieve the month that I set at the top at the moment, which I am aware of but not sure how to fix.

Code:

Main Assignment requirements to do with this part:
a) Create an Assignment class that can be used to create assignment objects. The
class should have fields for a reference to the module associated with the
assignment, the title of the assignment, the date when the assignment is due,
the time when the assignment is due, and the percentage of the module grade
that the assignment counts for.
b) The Assignment class should have an appropriate constructor method that
enables all the fields to be set. All fields should also have getters and setter
methods for each of the fields.

Thanks.

@UP
These are just some of the validations that will have to be put in place though so I don't think it's as simple as you say unfortunately but I am no Java expert as you can tell so if you have any suggestions then I'd be more than happy to discuss them.
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Update on what I've managed to come up with so far. I think that I more or less know how the date is supposed to work but now I encountered another issue and I am not sure how to solve it so hopefully after posting the code here, I'll be able to get an answer to my problem. In short, I am not sure about a few requirements that I need to implement for my solution to the assignment to work. That's why I'll also post the full requirements below.

Requirements:
1. Create an Assignment class that can be used to create assignment objects. The
class should have fields for a reference to the module associated with the
assignment, the title of the assignment, the date when the assignment is due,
the time when the assignment is due, and the percentage of the module grade
that the assignment counts for.
2. The Assignment class should have an appropriate constructor method that
enables all the fields to be set. All fields should also have getters and setter
methods for each of the fields.
3. There should be a factory method in the Assignment class that verifies the data
entered and only creates an assignment object if the supplied data is valid.
4. The class should have an equals(Object o) method that will return true if the
two assignment objects are for the same module and have the same title.
5. The class should have a toString() method that will return a String of the form
“<module code>: <assignment title> due on dd/mm/yyyy at hh:mm” where
<module code> is the module code for the associated module, <assignment
title> is the tile od the assignment, dd/mm/yyyy is the date on which the
assignment is due, and hh:mm is the time when the assignment is due.
6. You should also write a corresponding test class for the Assignment class to
verify the operation of all the methods in the Assignment class.

7. In order for the system to work, the Model class needs a number of methods
added. The view code checks whether these methods exist and reports an error
message if the required method does not exist. These methods are:
a. addAssignment: This method should create an Assignment object
using the currently selected Module object and the parameter data
passed to the method. It should return a reference to the created
Assignment object. The parameters for this method are a String title,
an integer day number, an integer month number, and integer year
number, an integer hour number, and an integer percentage. These
parameter values should be validated according to the rules below and
the object should only be created if the parameters are valid. NOTE:
The view code will use the returned reference to output a message to
say that the object was successfully created.
b. modifyAssignment: This method should modify the current
assignment object if one has been created. If there is no current
assignment object, it should do nothing. This method receives the same
parameters as the addAssignment method and it should validate the
parameter data in the same way as the addAssignment method. This
method should return a reference to the modified object.
c. getCurrentAssignment: This method should return a reference to the
current assignment. There are no parameters for this method. If there is
no current assignment, it should return null.
d. clearAssignment: This method should clear the currently selected
assignment. There are no parameters for this method and no return
value. This will have the effect of clearing the input fields on the
Assignment data entry panel. The following get… methods need to be
implemented to see this effect.
e. getAssignmentTitle: This method should return the title of the current
assignment. There are no parameters for this method. If there is no
current assignment, it should return null.
f. getAssignmentDay: This method should return the day that the current
assignment is due. There are no parameters for this method. If there is
no current assignment, it should return 0.
g. getAssignmentMonth: This method should return the month that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
h. getAssignmentYear: This method should return the year that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
i. getAssignmentHour: This method should return the hour that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
j. getAssignmentMinute: This method should return the minute that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
k. getAssignmentPercent: This method should return the percentage
assigned to the current assignment. There are no parameters for this
method. If there is no current assignment, it should return 0.
8. Write a test class to verify that the methods added to the Model class work as
expected. You may add additional methods to the model class to support these
methods and your testing.

9. The following data validation rules should be applied:
a. None of the string fields should be blank.
b. Any year fields must be a four digit number (i.e. greater than 999) and
less than or equal to 2016.
c. Any month field should be a number between 1 and 12 inclusive.
d. Any day field should be a number between 1 and 31 inclusive where
the month is January (1), March (3), May (5), July (7), August (8),
October (10), and December (12), between 1 and 30 inclusive where
the month is April (4), June (6), September (9), and November (11),
and between 1 and 28 for February (2). We are ignoring leap years for
this exercise.
e. The hour should be a number between 0 and 23 inclusive.
f. The minute should be a value between 0 and 59 inclusive.
g. The percentage should be a value between 1 and 100 inclusive. In the
next assignment, you will be expected to verify that the total of the
percentages for assignments assigned to a module do not exceed 100.
h. If the data is not valid then you should ensure that you add an error
message saying why the action was not completed. You should also
ensure that the error messages are cleared when the next action is
performed.
i. These validation rules should be verified by your test methods.

(Tests are crossed out as they can be dealt with later on.)

Code:
Model class (Everything to do with assignment was done by me and the rest was already provided):


Assignment class (done by me):


What I'm asking from you is to (if possible) tell me where I'm going wrong and why when I start up the program, I can't add a new assignment, etc.
If you have any further questions, would like me to clear something up or would like me to provide you with the full file then please ask. Thanks in advance for all the help!
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't expect people to read the whole of so much code. I can see errors in the factory method however; you can never expect a date time object to be equal to an empty String. Rather than returning null from that method, if there is an error, throw a NullPointerException if a null is passed.

That instruction will prompt a flood of posts saying to use a different Exception
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I usually go for IllegalArgumentException.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What I'm asking from you is to (if possible) tell me where I'm going wrong and why when I start up the program, I can't add a new assignment, etc.


How are you starting the program?
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic