• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

System Date

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do I get the system date into a JTextfield

Olly
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check java.util.Date in combination with java.text.DateFormat / java.text.SimpleDateFormat
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic