• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Want to get your idea on how to design this class

 
Greenhorn
Posts: 28
1
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys! Learned a ton of stuff in the last thread and wasn't sure if I should ask in that one, but this is a completely different topic so I figured a new post was warranted. I am trying to design a class that I am calling Events. In my program, I am basically allowing my Food Truck owners to log in and create an Event that would- basically- tell a user when and where their food truck will be. It doesn't need to be a location like longitude and latitude, but more of a String literal, like "Forest Park". The date can simply be "Tuesday June 7th, 2016 at 7:00pm". That is it. I know this might seem trivial, but I just wanted some input. I have to also store this data into my mysql database and I would like to organize it when I print it off for the User. They could see like, three months in advance or that week. I am going to tie it via foreign key to a food truck's truckID that way they can also see that information when they pull up individual trucks.

All that being said, how should I design my class in a way that their input can be stored easily? Should I make individual fields like: String month, int day, int year and then write the code to limit what their input can be Or is there something built into Java kind of like this purpose. I mean, I've heard of Joda-Time, idk if that would be useful. How about the java.util.Calendar? Anyway, sorry if this is a stupid question, design always trips me up in the beginning.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Ward wrote:All that being said, how should I design my class in a way that their input can be stored easily? Should I make individual fields like: String month, int day, int year and then write the code to limit what their input can be


Don't use separate fields for date and time! You probably want to use a DATETIME column in your database table. Or maybe two columns: beginTime and endTime perhaps.

If you are using Java 8 for your application, there is no need to use JodaTime, simply use the new Date/Time API, which is a part of the SDK (and thus you need no external library dependency). If you are using Java 7 (or prior), you could use Calendar and Date classes, or add an additional library (like JodaTime). But it might be time to consider upgrading to Java 8.

Dustin Ward wrote:Anyway, sorry if this is a stupid question, design always trips me up in the beginning.


There are no stupid questions! In fact, thinking about a (good) design before starting to code right away will probably save you tons of work and refactorting
 
Dustin Ward
Greenhorn
Posts: 28
1
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Dustin Ward wrote:All that being said, how should I design my class in a way that their input can be stored easily? Should I make individual fields like: String month, int day, int year and then write the code to limit what their input can be


Don't use separate fields for date and time! You probably want to use a DATETIME column in your database table. Or maybe two columns: beginTime and endTime perhaps.

If you are using Java 8 for your application, there is no need to use JodaTime, simply use the new Date/Time API, which is a part of the SDK (and thus you need no external library dependency). If you are using Java 7 (or prior), you could use Calendar and Date classes, or add an additional library (like JodaTime). But it might be time to consider upgrading to Java 8.

Dustin Ward wrote:Anyway, sorry if this is a stupid question, design always trips me up in the beginning.


There are no stupid questions! In fact, thinking about a (good) design before starting to code right away will probably save you tons of work and refactorting



I am using Java 8 Thanks for the tip on the new java.time API. I think that will be helpful and I am trying to use LocalDateTime because I don't really have to worry about time zones or anything like that since technically this is a local program (only for local food trucks).... Anyway, I am having the frustrating problem of being able to check my input against a format. I am trying to let my user input a date/time so I can store it in my database. After extensive googling, I tried this:


Just as a simple method to get input. I am pretty sure that the ofPattern part is correct. I would like to then store it in my mysql database for future references. Not sure what I am doing wrong but it gives me all kinds of error messages:

 
Dustin Ward
Greenhorn
Posts: 28
1
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so I fiddled a little- I am not quite sure why this works now- but I got it to output the correct format...



I do get a 'T' in the middle of my output though, which is weird... So idk.. I'll keep playing and learning
 
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your statement
System.out.println(dateTime);
will use the default format which is an international standard which includes the 'T'.
 
Dustin Ward
Greenhorn
Posts: 28
1
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Your statement
System.out.println(dateTime);
will use the default format which is an international standard which includes the 'T'.



Ohhhh. Well, it actually works when I insert it into mysql table using DATETIME so I am cool with it
 
Dustin Ward
Greenhorn
Posts: 28
1
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, in case anyone comes upon this, because of the format I was using (MM-dd-yyyy hh:mm a) it requires the a (which indicates AM/PM) because the lowercase h signifies hours in 1-12 where as the uppercase H signifies what Americans call military time (1-24) and does NOT need the a
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Ward wrote:I do get a 'T' in the middle of my output though, which is weird...


No, that's not weird at all. It is just the textual (string) representation of a LocalDateTime instance as explained in the toString() method.

Dustin Ward wrote:Ohhhh. Well, it actually works when I insert it into mysql table using DATETIME so I am cool with it


Of course it will work when inserting it into a MySQL database table, because the textual representation is not used in the MySQL driver to insert/updata a DATETIME column. It only offers some information about the object in (hopefully) a human-readible format.
 
Dustin Ward
Greenhorn
Posts: 28
1
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Dustin Ward wrote:I do get a 'T' in the middle of my output though, which is weird...


No, that's not weird at all. It is just the textual (string) representation of a LocalDateTime instance as explained in the toString() method.

Dustin Ward wrote:Ohhhh. Well, it actually works when I insert it into mysql table using DATETIME so I am cool with it


Of course it will work when inserting it into a MySQL database table, because the textual representation is not used in the MySQL driver to insert/updata a DATETIME column. It only offers some information about the object in (hopefully) a human-readible format.



How and where would I overwrite the LocalDateTime .toString? Like, I've overwritten all of my CLASS' toString's but how do I just overwrite a random objects?

Because I would really like to print it off without the T and although I elected the military time, I'd like to write it out in standard 1-12 time without making people specify AM or PM when I get their input
 
Dustin Ward
Greenhorn
Posts: 28
1
Eclipse IDE Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Ward wrote:How and where would I overwrite the LocalDateTime .toString? Like, I've overwritten all of my CLASS' toString's but how do I just overwrite a random objects?

Because I would really like to print it off without the T and although I elected the military time, I'd like to write it out in standard 1-12 time without making people specify AM or PM when I get their input



Woohoo! Nevermind. I figured it out.



and then I just printed formattedDateTime instead of just dateTime!

Edit to add: Even cooler that I just found is that if you change the Formatter from HH:mm (which is military time) to hh:mm a, it will automatically convert it to the standard 1-12 time AND print off AM/PM! Awesome!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Ward wrote:How and where would I overwrite the LocalDateTime .toString? Like, I've overwritten all of my CLASS' toString's but how do I just overwrite a random objects?


You can't overwrite the toString() method of LocalDateTime. In fact, you can't overwrite the toString() method of any class in Java. What you could do, is create a subclass and override the toString() method and let it return whatever string representation you want. But if you check the javadoc of LocalDateTime, you'll notice that this class is marked final, so it is impossible to create a subclass from this class and override the toString() method. If the class would not have been final, you could do it like thisIf you run this code snippet, the output will be "Person@232204a1 Roel CodeRanch".

Dustin Ward wrote:Woohoo! Nevermind. I figured it out.


That's indeed what you have to do in this case: create a DateTimeFormatter and format the LocalDateTime object. In fact, you could create a DateTimeUtils class with a bunch of static utility methods to format LocalDateTime objects like you want. Have a cow for figuring out yourself how to format a LocalDateTime object.

Hope it helps!
Kind regards,
Roel
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic