How to compose date Java code

Asked By 10 points N/A Posted on -
qa-featured

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.

SHARE
Best Answer by Parisi Petrusic
Answered By 0 points N/A #167466

How to compose date Java code

qa-featured

Hi,

You can use this example to compose dates in Java.

Enclose them in the date parenthesis.

I have attached them.

Best Answer
Best Answer
Answered By 0 points N/A #167467

How to compose date Java code

qa-featured

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
 
Answered By 0 points N/A #167469

How to compose date Java code

qa-featured
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.
Answered By 10 points N/A #167471

How to compose date Java code

qa-featured

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,

Related Questions