Showing posts with label Calendar. Show all posts
Showing posts with label Calendar. Show all posts

Calendar set(year, month, day) - How to Set a Date to the Calendar object

public final void set (int year, int month, int day) was added in API level 1
Sets the year, month, and day of the month fields. Other fields are not changed; call clear() first if this is not desired. The month value is 0-based, so it may be clearer to use a constant like JANUARY.

1. How to Declare a Calendar object

2. Add the below line of code to the onCreate method. This will set the Calendar.YEAR field to 1999, the Calendar.MONTH to 9, and Calendar.DAY_OF_MONTH field to 24. So, the date of 09/24/1999. Make sure to use numbers WITHOUT a prefix of 0, such as 09 for the month of September. 

myCalendar.set(1999, 9, 24);

3. Compile and run!

Next Recommended Article: How to Declare a SimpleDateFormat variable

Resources:
http://developer.android.com/reference/java/util/Calendar.html#set(int, int, int)



Calendar set(int field, int value) - How to Set a Field in the Calendar object

public void set (int field, int value) was added in API level 1
Sets the given field to the given value.
The calendar object has many fields in it. Some of those fields are:
AM_PM, DATE, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_WEEK_IN_MONTH, DAY_OF_YEAR, DST_OFFSET, ERA, HOUR, HOUR_OF_DAY, MILLISECOND, MINUTE, MONTH, SECOND, WEEK_OF_MONTH, WEEK_OF_YEAR, YEAR, ZONE_OFFSET

1. How to Declare a Calendar object

2. Add the below line of code to the onCreate method. This will set the Calendar.YEAR field in the Calendar object to 1999.

myCalendar.set(Calendar.YEAR, 1999);

3. Compile and run!

4. Below are the list of fields and their descriptions the Calendar can update.

AM_PM - indicating whether the HOUR is before or after noon.
DATE - indicating the day of the month.
DAY_OF_MONTH - indicating the day of the month.
DAY_OF_WEEK - indicating the day of the week.
DAY_OF_WEEK_IN_MONTH - indicating the ordinal number of the day of the week within the current month
DAY_OF_YEAR - indicating the day number within the current year.
DST_OFFSET - indicating the daylight savings offset in milliseconds.
ERA - indicating the era, e.g., AD or BC in the Julian calendar.
HOUR - indicating the hour of the morning or afternoon.
HOUR_OF_DAY - indicating the hour of the day.
MILLISECOND - indicating the millisecond within the second.
MINUTE - indicating the minute within the hour.
MONTH - indicating the month.
SECOND - indicating the second within the minute.
WEEK_OF_MONTH - indicating the week number within the current month.
WEEK_OF_YEAR - indicating the week number within the current year.
YEAR - indicating the year.
ZONE_OFFSET - indicating the raw offset from GMT in milliseconds.

Next Recommended Article: Calendar set(year, month, day) - How to Set a Date to the Calendar object

Resources:
http://developer.android.com/reference/java/util/Calendar.html#set(int, int)



getInstance() - How to Get Current Date/Time and Save to Calendar object

public static synchronized Calendar getInstance () was added in API level 1
Constructs a new instance of the Calendar subclass appropriate for the default Locale and default TimeZone, set to the current date and time.

1. Declare a Calendar object.

2. Add the below line to the onCreate method. This will get the current date,  time, locale and timezone and save it to the Calendar object.

myCalendar = Calendar.getInstance();

3. Compile and run!

Next Recommended Article: Calendar set(int field, int value) - How to Set a Field in the Calendar object

Resources:
http://developer.android.com/reference/java/util/Calendar.html#getInstance()
http://dtroeh.homeip.net/webshare/Drivers/Motorola%20Cell/android-sdk-windows/docs/guide/tutorials/views/hello-datepicker.html
http://developer.android.com/reference/java/util/Calendar.html


How to Declare a Calendar object

Calendar was added in API Level 1

1. In the MainActivity.java file, add the below line to the imports section.

import java.util.Calendar;

2. Add the below line to the onCreate method to declare a Calendar object.

 Calendar myCalendar;

3. Compile and run!

Next Recommended Article: getInstance() - How to Get Current Date/Time and Save to Calendar object

Resources:
http://developer.android.com/reference/java/util/Calendar.html


Set DatePickerDialog results to a Calendar object

public final void set (int year, int month, int day) was added in API level 1
Sets the year, month, and day of the month fields. Other fields are not changed; call clear() first if this is not desired. The month value is 0-based, so it may be clearer to use a constant like JANUARY.
0-January, 1-February, 2-March, 3-April, 4-May, 5-June, 6-July, 7-August, 8-September, 9-October, 10-November, 11-December


2. In theonDateSet method, add the line below to set the Calendar object date. If the user selects the date in the DatePickerDialog of  December 25, 2014, the Calendar object would be set to that date.

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
// this method will get the year, month and day the user chose on the datepickerdialog

    myCalendar.set(year, monthOfYear, dayOfMonth)
    // To test, add a Toast with a
        //context of getBaseContext()
       //and a message of: "Month: " + myCalendar.get(MONTH) + "Day: " + myCalendar.get(Calendar.DAY_OF_MONTH) + "Year: " + myCalendar.get(Calendar.YEAR)
}