Java J2ME JSP J2EE Servlet Android

Java By Example: Java Calendar day, month, week, year, hour getting, java.util.Calendar

Java provides a strong way to work with date and calendar. Here is an example of java calendar to get values of --


* Month

* Year

* Week of year

* Week of month

* Day of month

* Hour

etc...


In this way we can also get some other information from calendar, like

* day of year

* Hour of day

* Minutes

* Seconds

* Time Zone

etc ..




import java.util.Calendar;


publicclass JavaCalendar {

publicstaticvoid main(String args[]){

Calendar calendar = Calendar.getInstance();


/* Setting calendar for current time */

calendar.setTimeInMillis(System.currentTimeMillis());


/* Get calendar month */

System.out.println(calendar.get(Calendar.MONTH)+1);


/* Get calendar year */

System.out.println(calendar.get(Calendar.YEAR


/* Get calendar week of year */

System.out.println(calendar.get(Calendar.WEEK_OF_YEAR


/* Get calendar week of month */

System.out.println(calendar.get(Calendar.WEEK_OF_MONTH


/* Get calendar day of week */

System.out.println(calendar.get(Calendar.DAY_OF_WEEK


/* Get calendar day of the month */

System.out.println(calendar.get(Calendar.DAY_OF_MONTH


/* Get calendar Hour */

System.out.println(calendar.get(Calendar.HOUR


}

}