In this section we have presented a simple Date example that shows how you can use different constructors of Date. We can construct the Date class in a number of ways. Here we will be showing few ways to construct the Date object.
Date newDate = new java.util.Date(); creates a new instance of Date.
Date newDate = new java.util.Date(99,8,7); creates a new instance of Date with the year value 1999, month value 8 and day value 7.
Here is the example code of SimpleDate.java as follows:
SimpleDate.java
import java.util.Date;
public class SimpleDate
{
public static void main(String args[]){
// Creating date with the use of default constructor
Date newDate = new java.util.Date();
System.out.println("Create date via Default constructor ==>"+newDate);
// Creating date with the use of parameterized constructor
newDate = new java.util.Date(99,8,7);
System.out.println("Create date via Parameterized constructor ==>"+newDate);
}
}
Save SimpleDate.java and compile it with the javac command.
Output:
|
C:\DateExample>javac SimpleDate.java Note: SimpleDate.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. C:\DateExample>java SimpleDate Create date via Default constructor ==>Fri Oct 10 12:09:15 GMT+05:30 2008 Create date via Parameterized constructor ==>Tue Sep 07 00:00:00 GMT+05:30 1999 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Simple Date example
Post your Comment