Java Calender Introduction

April 9, 2018 Oclemy Java DateTime 0 minutes, 45 seconds

Inside the java.Util package, exists an abstract class called the Calender.

This class allows has the following roles:

  1. Conversion between a specific instant of time and a set of fields of such as:

    1. YEAR.
    2. MONTH.
    3. DAY_OF_MONTH
    4. HOUR etc.
  2. Manipulating those Calender fields.

As an abstract class, the Calender class cannot be instantiated directly.

Instead, it provides us with a method called getInstance() that gets for us the Calender object using the default timeone and locale.

Let's look at examples to see how to use Calender class.

Get current year, month and day:

      // Get Year, Month and Day From java.util.Calendar
        final Calendar c = Calendar.getInstance();
        myYear = c.get(Calendar.YEAR);
        myMonth = c.get(Calendar.MONTH);
        myDay = c.get(Calendar.DAY_OF_MONTH);

In android these can then be passed to DatePickerDialog to be shown.

new DatePickerDialog(this, myDateSetListener, myYear,myMonth, myDay);

See how we do that in here.

Comments