Android Date Time :
Android Date Time, Almost in every app we require time and date so we can fetch them automatically without user interference if you want to know how please follow this tutorial completely.
In general we require Time & Date in many applications so here in this android tutorial i will be showing in general how to get Time and Date.
Starting with this tutorial i want to clarify one thing time and date are fetched from device in android device these time and date are automatically updated from internet.
This tutorial doesn’t require Internet or any thing more just will work with device in getting in device.
MainActivity.java :
Fetching the date, week, month, year and date as
Calendar Date = Calendar.getInstance(); int dayofyear = Date.get(Calendar.DAY_OF_YEAR); int year = Date.get(Calendar.YEAR); int week = Date.get(Calendar.WEEK_OF_MONTH); int month = Date.get(Calendar.MONTH); int day = Date.get(Calendar.DAY_OF_MONTH);
And here providing the full code for fetching the above specified details. As this is the basic tutorial explaining the usage of date and time you can make use of the data according to your requirement.
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.format.Time; import android.widget.TextView; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView hour12txt = (TextView)findViewById(R.id.hour12); TextView hour24txt = (TextView)findViewById(R.id.hour24); TextView minutetxt = (TextView)findViewById(R.id.minute); TextView secondtxt = (TextView)findViewById(R.id.second); TextView millisecondtxt = (TextView)findViewById(R.id.millisecond); TextView ampmtxt = (TextView)findViewById(R.id.ampm); TextView yeartxt = (TextView)findViewById(R.id.year); TextView monthtxt = (TextView)findViewById(R.id.month); TextView weektxt = (TextView)findViewById(R.id.week); TextView daytxt = (TextView)findViewById(R.id.day); Calendar Time = Calendar.getInstance(); int millisecond = Time.get(Calendar.MILLISECOND); int second = Time.get(Calendar.SECOND); int minute = Time.get(Calendar.MINUTE); int hour24 = Time.get(Calendar.HOUR_OF_DAY); int hour12 = Time.get(Calendar.HOUR); int ampm = Time.get(Calendar.AM_PM); hour24txt.setText(hour24+"(24hr format)"); hour12txt.setText(hour12+"(12hr format)"); minutetxt.setText(""+minute); secondtxt.setText(""+second); millisecondtxt.setText(""+millisecond); if(ampm==0){ ampmtxt.setText("AM"); }else{ ampmtxt.setText("PM"); } Calendar Date = Calendar.getInstance(); int dayofyear = Date.get(Calendar.DAY_OF_YEAR); int year = Date.get(Calendar.YEAR); int week = Date.get(Calendar.WEEK_OF_MONTH); int month = Date.get(Calendar.MONTH); int day = Date.get(Calendar.DAY_OF_MONTH); monthtxt.setText(""+month); weektxt.setText(""+week); yeartxt.setText(""+year); daytxt.setText(""+day); } }
activity_main.xml :
Designing the below view for displaying the date and time
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.androidcoding.abhi.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp"> <TextView android:id="@+id/date" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Time" android:textColor="#ffffff" android:background="#FE2E64" android:textAppearance="?android:attr/textAppearanceLarge" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="3dp" android:weightSum="2"> <TextView android:id="@+id/hourtitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Hour" android:textColor="#ffffff" android:background="#A9A9F5" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/hour12" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#000000" android:background="#F5DA81" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="3dp" android:weightSum="2"> <TextView android:id="@+id/ampmtitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="AM / PM" android:textColor="#ffffff" android:background="#A9A9F5" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/ampm" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#000000" android:background="#F5DA81" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="3dp" android:weightSum="2"> <TextView android:id="@+id/hour24title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Hour" android:textColor="#ffffff" android:background="#A9A9F5" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/hour24" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#000000" android:background="#F5DA81" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="3dp" android:weightSum="2"> <TextView android:id="@+id/minutetitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Minute" android:textColor="#ffffff" android:background="#A9A9F5" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/minute" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#000000" android:background="#F5DA81" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="3dp" android:weightSum="2"> <TextView android:id="@+id/secondtitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Second" android:textColor="#ffffff" android:background="#A9A9F5" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/second" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#000000" android:background="#F5DA81" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="3dp" android:weightSum="2"> <TextView android:id="@+id/millisecondtitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Milli Second" android:textColor="#ffffff" android:background="#A9A9F5" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/millisecond" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#000000" android:background="#F5DA81" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp"> <TextView android:id="@+id/time" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Date" android:textColor="#ffffff" android:background="#FE2E64" android:textAppearance="?android:attr/textAppearanceLarge" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="3dp" android:weightSum="2"> <TextView android:id="@+id/yeartitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Year" android:textColor="#ffffff" android:background="#A9A9F5" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/year" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#000000" android:background="#F5DA81" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="3dp" android:weightSum="2"> <TextView android:id="@+id/monthtitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Month" android:textColor="#ffffff" android:background="#A9A9F5" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/month" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#000000" android:background="#F5DA81" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="3dp" android:weightSum="2"> <TextView android:id="@+id/weektitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Week" android:textColor="#ffffff" android:background="#A9A9F5" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/week" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#000000" android:background="#F5DA81" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="3dp" android:weightSum="2"> <TextView android:id="@+id/daytitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Day" android:textColor="#ffffff" android:background="#A9A9F5" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/day" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#000000" android:background="#F5DA81" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> </LinearLayout> </LinearLayout>
Android Date Time Output :
The screen below depicts the implementation of Android Date Time.
If you have any query on this tutorial on Android Date Time let us know in the comment section below.
Like and share this tutorial for more interesting updates.
For more tutorials on date picker visit