5 Replies - 595 Views - Last Post: 10 May 2012 - 05:10 AM Rate Topic: -----

#1 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

How to get new instance of date/time from a button click

Posted 10 May 2012 - 03:26 AM

I found a way to get the current date and time, but the way i am using it seems to only give me the date and time the moment i run the program. What i want is for it to give me the time whenever a certain button is clicked.

This is the code i am using currently

This is at the top of the code just under the start (by the way, i have the necessary imports for this to work so please dont respond saying they arent there and that it wont work)

DateFormat dateFormat = new SimpleDateFormat("MMM/dd/yyyy hh:mm:ssa");
    Calendar cal = Calendar.getInstance();



And then this is what i was using in the code to generate the date/time but it wont work the way i want it to.
dateFormat.format(cal.getTime())


So what i wanted to do was put it into it's own method so that it could be run whenever it was needed and would generate the current time when used, not the time that the program was first run. Would i use the existing code and just put it all into the method, or would i use something different for that?

Is This A Good Question/Topic? 0
  • +

Replies To: How to get new instance of date/time from a button click

#2 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

Re: How to get new instance of date/time from a button click

Posted 10 May 2012 - 04:01 AM

Yes, you can use the code you already have in a method.




// Fields (they don't need to be created and instantiated every time you click the jbutton)
private DateFormat dateFormat;
private Calendar cal;

// In constructor, instantiate
dateFormat = new SimpleDateFormat("MMM/dd/yyyy hh:mm:ssa");
cal = Calendar.getInstance();

// This should also happen in the class constructor
JButton dateTimeBtn = new JButton("Date & time");
dateTimeBtn.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	fetchDateAndTime();
	}
});

// Method for fetching date and time
private void fetchDateAndTime() {
	dateFormat.format(cal.getTime()); // Do something with this value
	}



Feel free to ask if you have any questions :)

This post has been edited by oha055: 10 May 2012 - 04:07 AM

Was This Post Helpful? 1
  • +
  • -

#3 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: How to get new instance of date/time from a button click

Posted 10 May 2012 - 04:05 AM

Alright thanks, was thinking it was something like that but didnt want to just randomly try things cause i tend to wind up forgetting where i was heading with things when i do that.
Was This Post Helpful? 0
  • +
  • -

#4 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

Re: How to get new instance of date/time from a button click

Posted 10 May 2012 - 04:18 AM

View Postrobgw3, on 10 May 2012 - 01:05 PM, said:

Alright thanks, was thinking it was something like that but didnt want to just randomly try things cause i tend to wind up forgetting where i was heading with things when i do that.


No problem mate
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,204
  • Joined: 20-September 08

Re: How to get new instance of date/time from a button click

Posted 10 May 2012 - 04:30 AM

Quote

// Method for fetching date and time

private void fetchDateAndTime() {

    dateFormat.format(cal.getTime()); // Do something with this value

}



won't help you. The time of a Calendar is only valid at the instant it's created

You need

private String fetchDateAndTime() {

    return dateFormat.format(new Date()); // Do something with this value

}

This post has been edited by g00se: 10 May 2012 - 04:31 AM

Was This Post Helpful? 1
  • +
  • -

#6 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: How to get new instance of date/time from a button click

Posted 10 May 2012 - 05:10 AM

That works, or seems to anyway. Havent seen any problems with it yet. (Found one but that turned out to be me just forgetting about old code i had forgot to remove.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1