This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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.
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?
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, 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...
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."