File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes using Dates and times Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "using Dates and times" Watch "using Dates and times" New topic
Author

using Dates and times

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

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

Joined: Sep 26, 2010
Posts: 139
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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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

Joined: Aug 08, 2010
Posts: 210

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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Aug 08, 2010
Posts: 210

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
Bartender

Joined: Oct 14, 2005
Posts: 18126
    
    8

You'll want to review the SimpleDateFormat documentation to find out what the codes really are for "month" and "minute".
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

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

Joined: Oct 16, 2003
Posts: 1075
    
  10

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


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: using Dates and times
 
Similar Threads
timezone woes
Stumped By HashSet
dates, strings, and confusion
Timestamp Converter Applet
comparing dates