Android DateTime 101

Jonathan Darwin
5 min readJul 12, 2020

--

Image from unsplash by Estée Janssens

Have you ever googling about “How to format datetime in Android”, “How to compare dates in Android” or even “How to get current date in Android”? Yes, most of you, and even me when i started my career as android developer. Most of android application nowadays uses many date time functionality, like parsing, comparing, adding, etc. And in this article, i will cover an approach how to do this things :

  1. Parse DateTime
  2. Get current DateTime
  3. Get Date Difference
  4. Add Date
  5. Compare Dates

The Application

First thing first, we don’t really build an app today. We just print the result in the Logcat :D. And also the code itself will be written in kotlin.

Preparing the Project

  1. Open your android studio and select start a new Android Studio project.

2. Select Empty Activity & click Next.

3. Give a name for this project. Mine is Android DateTime 101. Don’t forget to set the language to kotlin. Then, click finish.

What You Should Know

Before we write some code, let me explain briefly about this 3 classes that we’ll use afterward:

  1. Date
    A class that represent the date in form of milliseconds. All the date operation later should be converted to this class.
  2. SimpleDateFormat
    a class for formatting & parsing the string to Date and vice versa.
  3. Calendar
    an abstract class, mainly used for getting the specific calendar fields such as day, month, year, hour, etc. Can be used together with Date class.

Parse DateTime

Often we need to change the format of the date. For example, we get the date from the backend with yyyy-MM-dd format and we want to display the date in form of dd MMMM yyyy format. And to achieve this, we should do these things :

  1. Determine the pattern of the initial date (yyyy-MM-dd) and output date (dd MMM yyyy).
  2. Create a parser & formatter with their pattern.
  3. Put the date in the parser. The result will be in form of Date class.
  4. Format the result with formatter, and the result will be in form of String.

First of all, let’s open the MainActivity.kt and create a private method named parseDateTime

Inside the method, let’s write this code :

Don’t forget to call the method in onCreate().

Let’s run the application and open your Logcat. The result should be look like this

Get Current DateTime

Let say that you want to display the current date & time when you open the app. To achieve that, we can do this steps :

  1. Create a Date class.
  2. Call date.time to get the current time (in millis).
  3. Create a formatter and put the desired output pattern in it.
  4. Put the time (in millis) in formatter.

Again, create a private method named getCurrentDateTime(), and write this code inside the method :

Call this method inside onCreate() below parseDateTime() and run the app. The result should be look like this :

Get Date Difference

Let’s assume that you want to know the difference between 2 given dates. All you need to do is :

  1. Prepare the 2 dates.
  2. Parse both of the dates using SimpleDateFormat.
  3. Call date.time to get the time in millis.
  4. Subtract it.
  5. Convert it to day(s), hour(s), minute(s), second(s) as you need.

Then, let’s create a private method named getDateDifference(), and copy paste this code :

Don’t forget to call this function, run it, and let’s see the result :

Add Date

You have a scenario where your app wants to determine assignment submission’s deadline. The deadline itself is obtained from assignment upload date + 2 days. Here is the step :

  1. Prepare the assignment upload date.
  2. Create a parser and parse the date.
  3. Create a Calendar instance, and set the Calendar time with parsed date before.
  4. Call add method in calendar instance, specify the field and the number to be added.
  5. Create a formatter, format the result.
  6. Print the result.

Let’s create a private method named addDate() and write the this line of code :

Run your code and see the result :

Compare Dates

And the last, let say we want to decide whether the given date already passed the deadline or not. To do this, all we need to do is pretty similar with getDateDifference() method before :

  1. Prepare the 2 dates.
  2. Parse both of the dates using SimpleDateFormat.
  3. Call date.time to get the time in millis.
  4. Subtract it.
  5. Decide whether the date already passed or not.

Create this last private method named compareDates and put this code inside it :

Run the code and let’s the see the result

And, We Are Done!

Thank you for reading this article! Hope it can help in your current or maybe future project. I’ll leave all the code in my github repo.
Cheers!

--

--