Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

How to get current system time and date in different Formats in android

Get Current System Time and Date in Android




In this post, we will discuss getting the current system time and date on a button click or activity startup or anytime you want.
https://youtu.be/Ls41YwRtmcg
We will get a different format of Date and Time and show on a Text view. you can adjust it according to your needs.

Firstly, we create some TextViews in our XML file.

activitymain.xml


 <?xml version="1.0" encoding="utf-8"?>  
 <android.support.constraint.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=".SampleActivity">  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Simple Date Format"  
     android:layout_margin="15dp"  
     android:textSize="16sp"  
     android:id="@+id/textview1"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="simple Date Format"  
     android:layout_margin="15dp"  
     android:textSize="16sp"  
     app:layout_constraintTop_toBottomOf="@id/textview1"  
     android:id="@+id/simple_date_format"/>  
   <ImageView  
     android:id="@+id/image_view1"  
     android:layout_width="match_parent"  
     android:layout_height="5dp"  
     android:src="@drawable/dotted"  
     android:layerType="software"  
     app:layout_constraintTop_toBottomOf="@id/simple_date_format"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="12 hour Format"  
     android:layout_margin="15dp"  
     android:textSize="16sp"  
     app:layout_constraintTop_toBottomOf="@id/simple_date_format"  
     android:id="@+id/textview2"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="12 hour format"  
     android:layout_margin="15dp"  
     android:textSize="16sp"  
     app:layout_constraintTop_toBottomOf="@id/textview2"  
     android:id="@+id/twelve_hour_format"/>  
   <ImageView  
     android:id="@+id/image_view2"  
     android:layout_width="match_parent"  
     android:layout_height="5dp"  
     android:src="@drawable/dotted"  
     android:layerType="software"  
     app:layout_constraintTop_toBottomOf="@id/twelve_hour_format"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="24 hour format"  
     android:layout_margin="15dp"  
     android:textSize="16sp"  
     app:layout_constraintTop_toBottomOf="@id/twelve_hour_format"  
     android:id="@+id/textview3"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="24 hour format"  
     android:layout_margin="15dp"  
     android:textSize="16sp"  
     app:layout_constraintTop_toBottomOf="@id/textview3"  
     android:id="@+id/twentyfour_hour_format"/>  
   <ImageView  
     android:id="@+id/image_view3"  
     android:layout_width="match_parent"  
     android:layout_height="5dp"  
     android:src="@drawable/dotted"  
     android:layerType="software"  
     app:layout_constraintTop_toBottomOf="@id/twentyfour_hour_format"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Date Only"  
     android:layout_margin="15dp"  
     android:textSize="16sp"  
     app:layout_constraintTop_toBottomOf="@id/twentyfour_hour_format"  
     android:id="@+id/textview4"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="only date"  
     android:layout_margin="15dp"  
     android:textSize="16sp"  
     app:layout_constraintTop_toBottomOf="@id/textview4"  
     android:id="@+id/onlydate"/>  
   <ImageView  
     android:id="@+id/image_view4"  
     android:layout_width="match_parent"  
     android:layout_height="5dp"  
     android:src="@drawable/dotted"  
     android:layerType="software"  
     app:layout_constraintTop_toBottomOf="@id/onlydate"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="time only"  
     android:layout_margin="15dp"  
     android:textSize="16sp"  
     app:layout_constraintTop_toBottomOf="@id/onlydate"  
     android:id="@+id/textview5"/>  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="time only"  
     android:layout_margin="15dp"  
     android:textSize="16sp"  
     app:layout_constraintTop_toBottomOf="@id/textview5"  
     android:id="@+id/only_time"/>  
 </android.support.constraint.ConstraintLayout>  

Then we use a SimpleDateFormat method in our activity and get time and date in different formats and show it on Text View.

MainActivity.java


 package studio.harpreet.sampleproject;  
 import android.os.Bundle;  
 import android.support.v7.app.AppCompatActivity;  
 import android.widget.TextView;  
 import java.text.SimpleDateFormat;  
 import java.util.Date;  
 import java.util.Locale;  
 public class SampleActivity extends AppCompatActivity {  
   TextView simpledateformat,twelvehour,twentyfourhour,dateonly,timeonly;  
   String ssimpledate,stwelvehour,stwentyfourhour,sdateonly,stimeonly;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_sample2);
  
     simpledateformat = findViewById(R.id.simple_date_format);  
     twelvehour = findViewById(R.id.twelve_hour_format);  
     twentyfourhour = findViewById(R.id.twentyfour_hour_format);  
     dateonly = findViewById(R.id.onlydate);  
     timeonly = findViewById(R.id.only_time);  

     SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY hh:mm:ss", Locale.getDefault());  
     ssimpledate = sdf.format(new Date());  
     simpledateformat.setText(ssimpledate); 
 
     SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-YYYY hh:mm a", Locale.getDefault());  
     stwelvehour = sdf1.format(new Date());  
     twelvehour.setText(stwelvehour);  

     SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss", Locale.getDefault());  
     stwentyfourhour = sdf2.format(new Date());  
     twentyfourhour.setText(stwentyfourhour);  

     SimpleDateFormat sdf3 = new SimpleDateFormat("dd-MM-YYYY , YYYY-MM-dd , MM-dd-YYYY", Locale.getDefault());  
     sdateonly = sdf3.format(new Date());  
     dateonly.setText(sdateonly);  

     SimpleDateFormat sdf4 = new SimpleDateFormat(" hh:mm:ss , HH:mm , hh:mm:ss a", Locale.getDefault());  
     stimeonly = sdf4.format(new Date());  
     timeonly.setText(stimeonly);  
   }  
 }  

Now we successfully show Time and Date in different formats.

 
Follow us for more posts like this, 
Subscribe to Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments