This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes System Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System Date" Watch "System Date" New topic
Author

System Date

terry oliver
Greenhorn

Joined: Jan 04, 2008
Posts: 22
how do I get the system date into a JTextfield

Olly
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Check java.util.Date in combination with java.text.DateFormat / java.text.SimpleDateFormat


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nine Chan
Greenhorn

Joined: Jun 15, 2008
Posts: 5
import java.sql.*;

import java.util.*;

import java.util.Date;

public class TestClass {

public static void main(String[] args) {

//Clendar Test code

int yyyy,mm,dd;

Calendar c=Calendar.getInstance();

yyyy=c.get(Calendar.YEAR);

mm=c.get(Calendar.MONTH) +1;

dd=c.get(Calendar.DAY_OF_MONTH);

System.out.println(yyyy);

System.out.println(mm);

System.out.println(dd);

//reach the system time

Timestamp ts=new Timestamp(System.currentTimeMillis());

String dateStr1=ts.toString();

System.out.println(dateStr1);

//another way to reach the system time

Date date=new Date();

String dateStr2=new Timestamp(date.getTime()).toString();

System.out.println(dateStr2);

//some basic characteristic

System.getProperties().list(System.out);

}

}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

"new niu",

Please check your private messages.

Also, UseCodeTags in the future.

There is no need to use java.sql.Timestamp; that is only needed for SQL transactions. It is created for the difference between date only (java.sql.Date), time only (java.sql.Time) and both (java.sql.Timestamp). For regular use java.util.Date will be just fine.
terry oliver
Greenhorn

Joined: Jan 04, 2008
Posts: 22
here is the code that i used and still cant set the JTextBox.

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
java.util.Date date = new java.util.Date();
String datetime = dateFormat.format(date);
System.out.println("date=" +date +datetime );
this.setVisible(true);
populateData();
}
public voidpopulateData()
{
dateValue.setText(datetime);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20


That should work. In the first method, datetime is a local variable. In the second method it was either an instance variable, or it wouldn't even compile. My solution solves that problem by passing the local variable to the second method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System Date