Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System Date

 
terry oliver
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do I get the system date into a JTextfield

Olly
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check java.util.Date in combination with java.text.DateFormat / java.text.SimpleDateFormat
 
Nine Chan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic