Get Current Time Programmatically in Android-Java

thumbnail

In this tutorial, you will learn to get current time in Android-Java programmatically. I will show how to get (display) current time in android application using previous tutorial, that is, Android navigation drawer with navigation component in Android-Java and Exit Application by Clicking the Back Button Twice Android-Java. To get current time in android application, we can use Calendar class, that is, Calendar calendar = Calendar.getInstance();.

Android Example: Get Current Time Programmatically in Android-Java

Xml Layout File

res/layout/fragment_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="true"
        >
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:layout_gravity="center" >

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tv_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Time"
                android:layout_gravity="center"
                android:gravity="center" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:layout_gravity="center" >

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/btn_get_current_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Get Current Time"
                android:layout_gravity="start"
                android:gravity="center" />

        </LinearLayout>
    </LinearLayout>


</LinearLayout>

Java Activity File

src/fragments/DashboardFragment.java

package com.inpows.creativejava.fragments;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;

import com.inpows.creativejava.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DashboardFragment extends Fragment {

    private AppCompatButton btnGetCurrentTime;
    private AppCompatTextView tvTime;
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);

        btnGetCurrentTime = rootView.findViewById(R.id.btn_get_current_time);
        tvTime = rootView.findViewById(R.id.tv_time);
        btnGetCurrentTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
                String strDate = "Current Time : " + mdformat.format(calendar.getTime());
                tvTime.setText(strDate);
            }
        });
        return rootView;
    }
}

The output will be:

Back To Top