Home‎ > ‎Applied Core Java‎ > ‎Introduction‎ > ‎

Test Date Class

Problem :

How to get system date from operating system? 

Solution :

There is a class java.util.Date that has all date related methods.

Program :

Step 1 - Coding

Create a text file c:\sunilos\TestDate.java  and copy below contents.

import java.util.Date;

/*
 * A program which shows the current date.
*/

public class TestDate {

    public static void main(String[] args) {
       
        Date d= new Date();
        System.out.println("Todays date is " +d);
    }

}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy TestDate.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TestDate.java with help of 
    • javac TestDate.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of
    • java TestDate command.

Output

Todays date is Mar 21, 2009

FAQ

What is Date class?

The class Date represents a specific instant in time, with millisecond precision.

Why do I import java.util.Date?

Java organizes its classes into different packages like java.lang, java.util, java.io etc. Classes those are exist in other than java.lang packages are required to be imported to use them. Since Date class belongs to java.util package so you need to import this package with help of below commands

import java.util.*;

or

import java.util.Date;



<<Previous
| Next>>

ċ
TestDate.java
(0k)
nakul shukla,
Apr 4, 2009, 5:38 AM
Comments