Press ESC to close

Android DateTime,Calender and DateTimePicker Tutorial and Libraries

Handling dates is one of the most fundamental aspects of any programming language. Date and Time are important because it allows us identify when a certain event or operation occurred. For example you can easily organize your posts by the date they were published or updated. Without date it would be very difficult for to identify the latest tutorials in this website for example.

Luckily for us, android as a framework leans on Java which has a filthy rich set of APIs to allow us handle dates. For example the Calender class is easy to use to manipulate dates.

In this post we want to explore:

The Calender class and examples.

DateTimePicker

Other datetime libraries.

Other DateTimePicker libraries.

Calender

The best way to understand the Calender class is to see it in action. Let’s look at some real world examples of this important class.

How to get the First Day of a Week

You have a date, we want you to get the first day of the week in which that current date is located. You are returning a Date object:

DateTime Libraries

(a). JodaTime

Joda-Time is a datetime library that provides a quality replacement for the Java date and time classes.

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8.

[notice]
Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) – a core part of the JDK which replaces this project.
[/notice]

Features of Joda-Time

1. Easy to Use.

Calendar makes accessing ‘normal’ dates difficult, due to the lack of simple methods. Joda-Time has straightforward field accessors such as getYear() or getDayOfWeek().

2. Easy to Extend.

The JDK supports multiple calendar systems via subclasses of Calendar. This is clunky, and in practice it is very difficult to write another calendar system. Joda-Time supports multiple calendar systems via a pluggable system based on the Chronology class.3. Comprehensive Feature Set.

The library is intended to provide all the functionality that is required for date-time calculations. It already provides out-of-the-box features, such as support for oddball date formats, which are difficult to replicate with the JDK.4. Up-to-date Time Zone calculations.

The time zone implementation is based on the public tz database, which is updated several times a year. New Joda-Time releases incorporate all changes made to this database. Should the changes be needed earlier, manually updating the zone data is easy.5. Calendar support.

The library provides 8 calendar systems.
Easy interoperability. The library internally uses a millisecond instant which is identical to the JDK and similar to other common time representations. This makes interoperability easy, and Joda-Time comes with out-of-the-box JDK interoperability.

6. Better Performance Characteristics.

Calendar has strange performance characteristics as it recalculates fields at unexpected moments. Joda-Time does only the minimal calculation for the field that is being accessed.

7. Good Test Coverage.

Joda-Time has a comprehensive set of developer tests, providing assurance of the library’s quality.

8. Complete Documentation.

There is a full User Guide which provides an overview and covers common usage scenarios. The javadoc is extremely detailed and covers the rest of the API.

9. Maturity.

The library has been under active development since 2002. It is a mature and reliable code base. A number of related projects are now available.

10. Open Source.
Joda-Time is licenced under the business friendly Apache License Version 2.0.

Awesome DateTimePicker and Calender Libraries

Let’s look at some of the best open source DateTimePicker libraries as well as their examples.

(a). HorizontalDateTimePicker

This is an android horizontal datetimepicker tutorial and example.

We see how to create a horizontally scrolling datetime picker. You can easily scroll to a certain date.

The selected date gets shown in a Toast message.

We will be using HorizontalPicker Library.

HorizontalPicker is a custom-build Android View used for choosing dates (similar to the native date picker) but draws horizontally into a vertically narrow container. It allows easy day picking using the horizontal pan gesture.

Video Tutorial

Well we have a video tutorial as an alternative to this. If you prefer tutorials like this one then it would be good you subscribe to our YouTube channel. Basically we have a TV for programming where do daily tutorials especially android.

Features of HorizontalPicker

Here are some of the features of HorizontalPicker library:

Date selection using a smooth swipe gesture

Date selection by clicking on a day slot

Date selection from code using the HorizontalPicker java object

Month and year view

Today button to jump to the current day

Localized day and month names

Configurable number of generated days (default: 120)

Configurable number of offset generated days before the current date (default: 7)

Share Article:

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site(camposha.info). Say hello or post me a suggestion: oclemmi@gmail.com .
Follow me below;
Github
, and on my channel:
ProgrammingWizards TV