• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using Dates and times

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a question about using Dates and Times, I want to have 2 JTextFields that take Date: and Time: i would then like a method which when someone clicks a button at a later time, to subtract the current date and time from say 600 Hours
to get the remainder of time from the 600 hours. Making a GUI to get the info isn't the problem graphically, but formatting the JTextField for proper intput, and how to do the method is...


hope someone can steer me in the right direction.

also a little side question, i haven't learned anything about XML or working with databases yet, what would be the easiest for a small program like this to save the time entered by a user to use in figuring the time later when the GUI is opened again?

Thanks
Mike
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a regular text field. To convert the string that the user enters in the text field into a Date object, you can use a java.text.SimpleDateFormat object (and also to convert a Date object back to a string).

To do arithmetic with dates (adding and subtracting amounts of time), use class java.util.Calendar.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper for the tip, sounds easy enough but i tried a few things with calender and so on and haven't had any luck yet.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example of creating a formatted date string



Here's a link with some time arithmetic

http://www.techrepublic.com/blog/howdoi/how-do-i-perform-datetime-arithmetic-with-javas-calendar-class/116
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike, can you explain in more detail what you tried and what problems you had? The more details you explain and the more specific questions you ask, the better we can help you.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,

I will remember to explain better in the future, I will show ya what i have now. Right now, it is showing todays date and not the date that is typed into the JTextField.





so at the moment it shows todays date and current time, instead of the date typed in.


Thanks
Mike
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
Since you have the current date in the System.out.println() it is showing the current date. Look closely and change it like below.

Some other things I noted...
1. Class names shall start with capital letters... so meinListener shall be changed to MeinListener
2. Have you intentionally left a : in the end of this date format ? "dd.mm.yy HH:mm:"
3. Did you intend to enclose this in a IF block?
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

Thanks for the tips! I didn't notice my MeinListener wasn't starting with a Capital letter and yes i had the wrong Object in the System.out.println(). thanks, am a lil tired today so that is probably why i missed it i guess.

however now, when i type for instance 21.08.11 , it says Fri Jan 21 23:22:00 CET 2011, instead of August for 08?? but i learned through my little mistake there that i can get the current date i will need by just creating a new Date() Object , so i can use that for the other part of the getStandzeit() method and have the current date subtract the difference from the dateSteri Object, that is gonna be much more tricky i think. Will have to look into the GregorianCalender Class..


Thanks
Mike
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want to review the SimpleDateFormat documentation to find out what the codes really are for "month" and "minute".
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Thanks, like i said i was a bit tired yesterday ;) ...so i got the method for the dates worked out using the API's , it turns out that i didn't need the GregorianCalendar to work it out.

heres the code if anyone wants to critique it, or add any ideas to it.




So the time a sterilised machine part is still sterile is 600 hours, the method gets the user input from the JTextField "dateFTSterilisiert" when the item was sterilised,
and subtracts the hours difference from the current date, and then prints it out. I think that is the first time i understood an API lol, i have had some hard times understanding them in the past few months...


Thanks for the help everyone!!

Mike
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also want to look at JFormattedTextField:
"JFormattedTextField extends JTextField adding support for formatting arbitrary values, as well as retrieving a particular object once the user has edited the text."
Tutorial Link
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic