?
Solved

Inserting the date

Posted on 2003-03-19
5
Medium Priority
?
217 Views
Last Modified: 2010-03-31
Hi,

Could anyone tell me how to insert the todays date into a textfield when a button is pressed.

Thanks
0
Comment
Question by:rebel_vb
4 Comments
 
LVL 3

Accepted Solution

by:
allahabad earned 100 total points
ID: 8168746
// import these two classes into Java file
import  java.util.Date;
import java.text.SimpleDateFormat;

   yourTextField  = new TextField(15);
   yourButton = new Button("Put Date");
   yourButton.addActionListener(this);
   
// this is the method to put Date into text field
public void actionPerformed(ActionEvent e) {
         
    if (e.getSource() == yourButton){
         SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");
         Date currentDate = new Date();
         yourTextField.setText(formatter.format(currentDate););
    }  
}
0
 
LVL 2

Expert Comment

by:glottis
ID: 8171955
instead of Date class you can also use Calendar class.

like this:

Calendar cal = Calendar.getInstance();
formatter.format(cal.getTime());

I say this because nearly all methods of Date class are deprecated.
0
 

Expert Comment

by:CleanupPing
ID: 9058593
rebel_vb:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9727275
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept allahabad's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question