I would like to learn how to compose date Java code so that I can display the date and time in a Java program.
Please help me how doing it.
How to compose date Java code
Hi,
You can use this example to compose dates in Java.
Enclose them in the date parenthesis.
I have attached them.
How to compose date Java code
Hello,
Here is a piece of code for you.
class MyClockLabel extends JLabel implements ActionListener {
  java.util.Date date;
  public MyClockLabel() {
    super(getStrVAl());
    Timer timer = new Timer(1000, this);
    timer.start();
  }
Â
  @Override
  public void actionPerformed(ActionEvent ae) {
     SimpleDateFormat format = new SimpleDateFormat("MM dd yyyy HH:mm:ss"); // way-1
    txtclock.setText(format.format(new java.util.Date())); // way-1
    // txtclock.setText(getStrVAl()); way-2
  }
Â
}
Â
/* way-2
Â
public String getStrVAl() {
   date=new java.util.Date();
   String val= date.getMonth().toString()+" "
          +date.getDate().toString()+" "
          +date.getYear().toString()+" "
          +date.getHours().toString()+":"
          +date.getMinutes().toString()+":"
          +date.getSeconds()().toString()+" ");
   return val;
}
Â
*/
Â
You can use standard Date and Timer classes to instantiate objects from them. I am showing a custom-made class named MyClockLabel which extends the standard JLabel Class. Inside the constructor of MyClockLabel, I instantiate a Timer object which displays time every second.
Â
Now, there are two ways you can override the actionPerformed method. If you use the standard SimpleDateFormat class then the body of that function becomes simpler.Â
Â
Otherwise, you need to do a little more work by creating an extra method named getStrVal, converting each part of a human readable date into a String object, constructing a single String object from all parts, and finally returning the String object.
Â
Hope it helps.
Â
Best Regards,
Parisi
Â
How to compose date Java code
Hi,
Â
Actually you can use the data object with string () method. As you wanted you can display date and time by using following program. This program shows you the current date as well as the current time.
Â
You can also use gettime () or methods like before (), after ().
Â
We can also use <flag and it says that same argument as in previous format should be used again.Â
Â
Hope the solution I provide will help you.Â
Â
Thanks & regards.
How to compose date Java code
Hi,
Java gives the Date class available in java.util package. This class displays the current date and time.
The Date class supports two constructors. The first constructor initializes the object with the current date and time.
Date( )
The constructor below accepts one argument that equals the number of milliseconds that have elapsed since midnight, January 1, 1970.
Date (long millisec).
Use the following code to display date and time using Java program.
Import java.util.Date;
public class DateDemo {
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();
// display time and date using toString()
System.out.println(date.toString());
}
}
Thanks,