• Register
36,800 points
11 5 4

In this post I will show you how to get the current date and time on Android, using the time zone that you prefer. For this we will use SimpleDateFormatand any valid format.

Table of contents:

  • The helper function to get date and time on Android.
  • Get date.
  • Get the time.
  • invoke function to get date and time on Android.

The helper function to get date and time on Android:

Let's first look at a function that will give us any date or time according to the format we specify, as well as the time zone.

It looks like this:

@SuppressLint ("SimpleDateFormat")
public static String getDateWithFormat (String format String timezone) {
     Calendar calendar = Calendar. getInstance ();
     Date date = calendar. getTime ();
     SimpleDateFormat sdf;
     sdf = new SimpleDateFormat (format);
     sdf. setTimeZone (TimeZone. getTimeZone (timezone));
     return sdf. format (date);
}

The function will return the current time according to the format and time zone. Let's look at the other functions.

Get date:

To get only the date, we invoke it like this:

public  static  String getCurrentDate ( String timezone) {
    String format =  " yyyy-MM-dd " ;
    return getDateWithFormat (format, timezone);
}

If you are concerned about the time zone, relax, we will see it in a moment. In this case we would obtain the date in format yyyy-MM-ddthat is, something like 2020-10-19 , but you can use another format if you want.

Get the time:

In the same way we have a function that invokes the first one that we saw in this post. To obtain it, the following is done:

public  static  String getCurrentTime ( String timezone) {
    String format =  " HH: mm: ss " ;
    return getDateWithFormat (format, timezone);
}

The only thing that changes is the format, now HH:mm:ssit would give something like 15:45:20 , in the same way we will see the time zone later.

Invoke function to get date and time on Android:

I have all these methods in a class called Utiles, and it looks like this:

class Useful {

     public static String getCurrentTime (String timezone) {
         String format = "HH: mm: ss";
         return Useful. getDateWithFormat (format, timezone);
     }

     public static String getCurrentDate (String timezone) {
         String format = "yyyy-MM-dd";
         return Useful. getDateWithFormat (format, timezone);
     }

     @SuppressLint ("SimpleDateFormat")
     public static String getDateWithFormat (String format, String timezone) {
         Calendar calendar = Calendar. getInstance ();
         Date date = calendar. getTime ();
         SimpleDateFormat sdf;
         sdf = new SimpleDateFormat (format);
         sdf. setTimeZone (TimeZone. getTimeZone (timezone));
         return sdf. format (date);
     }
}

Now, every time I want to get it I just do:

Useful . getCurrentDate ( " Pakistan / Karachi " );
Useful . getCurrentTime ( " Pakistan / Karachi " );

About the time zone is Pakistan, but for example, for Spain I use "GMT+1".

36,800 points
11 5 4