Calendar date time getter

In this chapter you will learn:

  1. Get month, date and year from Calendar
  2. How to get Get hour, minute, second out of a Calendar
  3. Getting current week of the month
  4. Getting current week of the year
  5. Convert month index to month name
  6. Get the name for day of a week

Get month, date and year from Calendar

int get(int field) Returns the value of the given calendar field.

import java.util.Calendar;
/* j a va 2  s. c  o m*/
public class Main {
  public static void main(String args[]) {
    String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
        "Sep", "Oct", "Nov", "Dec" };

    Calendar calendar = Calendar.getInstance();

    // Display current time and date information.
    System.out.print("Date: ");
    System.out.print(months[calendar.get(Calendar.MONTH)]);
    System.out.print(" " + calendar.get(Calendar.DATE) + " ");
    System.out.println(calendar.get(Calendar.YEAR));

  }

}

The output:

Get hour, minute, second

import java.util.Calendar;
/*j a va2  s.c o  m*/
public class Main {
  public static void main(String args[]) {
    String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
        "Sep", "Oct", "Nov", "Dec" };

    Calendar calendar = Calendar.getInstance();

    System.out.print("Time: ");
    System.out.print(calendar.get(Calendar.HOUR) + ":");
    System.out.print(calendar.get(Calendar.MINUTE) + ":");
    System.out.println(calendar.get(Calendar.SECOND));

  }

}

The output:

Getting current week of the month

The following code gets current week of the month.

import java.util.Calendar;
/*from   j  a  v  a 2 s. co  m*/
public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
  
    System.out.println("Current week of month is : "
        + now.get(Calendar.WEEK_OF_MONTH));
    
    
  }
}

The output:

Getting current week of the year

The following code gets current week of the year.

import java.util.Calendar;
/*from   j  a  va 2s.c o  m*/
public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    
    System.out.println("Current week of year is : "
        + now.get(Calendar.WEEK_OF_YEAR));
    
  }
}

The output:

Convert month index to month name

The following code displays the month name rather than the number of a year.

import java.util.Calendar;
/*j a  v  a  2  s .c om*/
public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
  
    String[] strMonths = new String[] { "Jan", "Feb", "Mar", "Apr", "May",
        "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    System.out.println("Current month is : "
        + strMonths[now.get(Calendar.MONTH)]);
  }
}

The output:

Get the name for day of a week

import java.util.Calendar;
//from j a v a 2 s  .com
public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    String[] strDays = new String[] { "Sunday", "Monday", "Tuesday",
        "Wednesday", "Thusday", "Friday", "Saturday" };
    System.out.println("Current day is : "
        + strDays[now.get(Calendar.DAY_OF_WEEK) - 1]);
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Set hour, minute and second for a Calendar