Forums Register Login

using Dates and times

+Pie Number of slices to send: Send
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
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
1
+Pie Number of slices to send: Send
You'll want to review the SimpleDateFormat documentation to find out what the codes really are for "month" and "minute".
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
The first person to drink cow's milk. That started off as a dare from this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1839 times.
Similar Threads
dates, strings, and confusion
comparing dates
timezone woes
Timestamp Converter Applet
Stumped By HashSet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:41:21.