Home Java Example Java Util Getting the current date



Getting the current date
Posted on: April 16, 2007 at 12:00 AM
This section shows the way of getting the current date of the system.

Getting the current date

     

This section shows the way of getting  the current date of the system.

In this section, through the given program you can get the current date in the proper format (Day/Month/Year). This program shows the current date by using the various methods and fields of the Calendar class. These are explained as follows:

Code Description:

Calendar.MONTH:
This field of the Calendar class returns the static integer which is the value of the month of the year.

Calendar.DAY:
This field returns a static integer value which is the value of the day of the month in the year.

Calendar.YEAR:
This field returns a static integer value which is the current year.

Here is the code of the program:

import java.util.*;

public class CurrentDate{
  public static void main(String[] args){
  Calendar cal = new GregorianCalendar();
  int month = cal.get(Calendar.MONTH);
  int year = cal.get(Calendar.YEAR);
  int day = cal.get(Calendar.DAY_OF_MONTH);
  System.out.println("Current date : " 
  + day + 
"/" (month + 1"/" + year);
  }
}

Download this example.

Related Tags for Getting the current date:
cclassormdateformcalendariomethodsmethodformatgetfieldusingfieldsthisshowforieprogramramldwsshearemonthsectioncanvarpecurrentinrmcalasmntcandaclthroughendmehowhrprodayyearsatisivpropereaandarropevavariousssrirenthsholdspropprndodsonogro


More Tutorials from this section

Ask Questions?    Discuss: Getting the current date   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

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.