DEV Community

Dhruv Joshi
Dhruv Joshi

Posted on

How to make a simple Android app that shows time and date?

Here is an example of how you could create a simple Android app that displays the current time and date:

First, install the Android Studio development environment and set it up on your computer.

Create a new project in Android Studio and choose "Empty Activity" as the template.

In the main activity layout file (activity_main.xml), add a TextView element to display the time and date.

In the MainActivity.java file, import the java.util.Calendar and java.text.SimpleDateFormat classes.

In the onCreate() method, get the current time and date using the Calendar and SimpleDateFormat classes, and set the text of the TextView element to the formatted time and date.

Run the app on an emulator or physical device to see the time and date displayed.

Here is an example of the code that you could use in the MainActivity.java file:

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the current time and date
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        String currentTime = dateFormat.format(calendar.getTime());

        // Set the text of the TextView element to the formatted time and date
        TextView textView = findViewById(R.id.textView);
        textView.setText(currentTime);
    }
}

Enter fullscreen mode Exit fullscreen mode

This is just a basic example of how you could create an Android app that displays the current time and date. There are many other features and functionality that you could add to the app, depending on your needs and goals.

Thanks for reading! Reach me if you want to develop apps quickly and in budget.

Latest comments (0)