Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Get date using Calendar class (Java)

Complete source code below will show you, how to get date on local computer in java using Calendar class.

import java.util.Calendar;
import java.util.Locale;

public class GetDate
{
public static void main(String[]args)
{
 //Get a calendar using the default time zone and locale.
 Calendar currentComputerDate=Calendar.getInstance();

 //Print date
 System.out.print(currentComputerDate.get(Calendar.DATE));
 System.out.print(".");

 /*Print month in text representation. For example January is first month.
  *Month that will print is in long representation.
  *Short representation for January is Jan
  *You can try to change Calendar.LONG to Calendar.SHORT
  *Local.ROOT is useful constant for the root locale.
  */
    System.out.print(currentComputerDate.getDisplayName(Calendar.MONTH,Calendar.LONG,Locale.ROOT));
 System.out.print(".");

 //Print year
 System.out.print(currentComputerDate.get(Calendar.YEAR));
}
}