Get information about the current date with Java
The following example shows some information about the current date. The example uses the Java class GregorianCalendar.
Code:
import java.util.*;
public class CurrentDateInformation
{
public static void main(String[] args)
{
GregorianCalendar gc = new GregorianCalendar();
System.out.println("DATE: " + gc.get(Calendar.DATE));
System.out.println("MONTH: " + gc.get(Calendar.MONTH));
System.out.println("YEAR: " + gc.get(Calendar.YEAR));
System.out.println();
System.out.println("WEEK_OF_YEAR: " + gc.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + gc.get(Calendar.WEEK_OF_MONTH));
System.out.println();
System.out.println("DAY_OF_WEEK: " + gc.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_MONTH: " + gc.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + gc.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + gc.get(Calendar.DAY_OF_WEEK_IN_MONTH));
}
}
Result:
>java -cp . CurrentDateInformation
DATE: 14
MONTH: 4
YEAR: 2012
WEEK_OF_YEAR: 20
WEEK_OF_MONTH: 3
DAY_OF_WEEK: 2
DAY_OF_MONTH: 14
DAY_OF_YEAR: 135
DAY_OF_WEEK_IN_MONTH: 2
>Exit code: 0