File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Spring in Action this week in the Spring 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: 19718
    
  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: 19718
    
  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: 19718
    
  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.
 
Don't get me started about those stupid light bulbs.
 
subject: System Date