How to customize the date and time in any format using the calendar API in your Android App?

In this video it shows how one can customize the date and time to any format. It uses the calendar API to pull or extract the elements of date and time such as Days, Month, Year and Hours, Minute and Seconds.

It takes the instance from the calendar to capture the current date and time.

Then it fetches the individual element of the date using the GET method on the calendar object.

Further, it converts all the elements from int to string data type to form a complete text string which is displayed in a textView.

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Complete source code:

package com.programmerworld.datetimeformatcustomization;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}

public void buttonDisplayDateTime(View view){
Calendar calendar = Calendar.getInstance();
String stringDateTime; // DD-MM-YY HH:Mi:SS
stringDateTime = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)) + "-" +
String.valueOf(calendar.get(Calendar.MONTH) + 1) + "-" + // Shifted by 1 to take care of indexing
String.valueOf(calendar.get(Calendar.YEAR)) + " " +
String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) + ":" + // 24 hours clock
String.valueOf(calendar.get(Calendar.MINUTE)) + ":" +
String.valueOf(calendar.get(Calendar.SECOND));

textView.setText(stringDateTime);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="301dp"
android:layout_height="76dp"
android:text="Programmer World!"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.331" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="117dp"
android:layout_marginTop="72dp"
android:onClick="buttonDisplayDateTime"
android:text="Display Date and time"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.programmerworld.datetimeformatcustomization">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DateTimeFormatCustomization">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

plugins {
id 'com.android.application'
}

android {
compileSdk 31

defaultConfig {
applicationId "com.programmerworld.datetimeformatcustomization"
minSdk 31
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Leave a Reply

%d bloggers like this:
Available for Amazon Prime