Pin It

How to use Calendar class with current date, time and month in Java ?

Program to demonstrate use of Calendar class in Java

package com.hubberspot.example;

import java.util.Calendar;

public class CalendarDemo {

	public static void main(String[] args) {
        
	// Create a Calendar object by calling getInstance()
	// which returns concrete object of subclass
        Calendar calendar = Calendar.getInstance();
        // return hour of the current day
        int hours = calendar.get(Calendar.HOUR);
        // return minute of the current day
        int minutes = calendar.get(Calendar.MINUTE);
        // return hour of the current day
        int seconds = calendar.get(Calendar.SECOND);
        // return hour of the current day
        int milliseconds = calendar.get(Calendar.MILLISECOND);
        // return hour of the current day
        int day = calendar.get(Calendar.DATE);
        // return hour of the current day
        int month = calendar.get(Calendar.MONTH) + 1;
        // return hour of the current day
        int year = calendar.get(Calendar.YEAR);
        // return hour of the current day
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        // return hour of the current day
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        // return hour of the current day
        int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        // return hour of the current day
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
                
        System.out.println("Hours : " + hours);
        System.out.println("Minutes : " + minutes);
        System.out.println("Seconds : " + seconds);
        System.out.println("Milliseconds : " + milliseconds);
        System.out.println("Hour of the Day : " + hourOfDay);
        
        System.out.println("Current Date: " + calendar.getTime());
        System.out.println("Day : " + day);
        System.out.println("Month : " + month);
        System.out.println("Year : " + year);
        
        System.out.println("Day of Week : " + dayOfWeek);
        System.out.println("Day of Month : " + dayOfMonth);
        System.out.println("Day of Year : " + dayOfYear);
    }

}




Output of the program : 


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot