Android System Date And Time In Different Format

By | March 4, 2014
There are many different formats of displaying date and time  , In this tutorial we will see how to show system date and time in different format  inside your android application .

we use make use of Calender class of java.util package , the static method getInstance of Calender class returns the  instance of Calender and this object can produce all the time field values  needed to implement the date-time format

 Calendar c = Calendar.getInstance();

After getting calendar Instance , we required a formatted object for date and time.

The SimpleDateFormat Class is used for setting the format , the constructor of this class takes the pattern describing what strings are to produced

SimpleDateFormat format = new SimpleDateFormat(“dd:MMMM:yyyy HH:mm:ss “);
String formatdate=format(c.getTime());

file : activity_main.xml

    tools:context=”.MainActivity” >

        android:textSize=”22sp” />


file : MainActivity

package com.example.sysdatetime;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView tv;
 Button btn;

 protected void onCreate(Bundle savedInstanceState) {

  tv = (TextView) findViewById(;

  Calendar c = Calendar.getInstance();

  SimpleDateFormat format1, format2, format3, format4, format5;

  format1 = new SimpleDateFormat(“dd:MM:yyyy:HH:mm:ss “);

  format2 = new SimpleDateFormat(“dd-MM-yyyy-HH-mm-ss”);

  format3 = new SimpleDateFormat(“dd-MMMM-yyyy-HH-mm-ss”);

  format4 = new SimpleDateFormat(“dd/MMM/yyyy/HH/mm/ss”);

  format5 = new SimpleDateFormat(“dd_MM_yy_HH_mm_ss a”);

  tv.setText(format1.format(c.getTime()) + “nn”
    + format2.format(c.getTime()) + “nn”
    + format3.format(c.getTime()) + “nn”
    + format4.format(c.getTime()) + “nn”
    + format5.format(c.getTime()));


In this below table you can see the patterns description for date time and also the separator which is used for formatting.

Date format

Date Format Description Value
d Single digit date eg 1 1
dd double digit date eg 01 01
M Single digit month eg: 1 1
MM Double digit month eg: 01 01
MMM three letter abbreviation for month ex: jan jan
MMMM month spelled out in full ex : january january
yy double digit year ex : 14 14
yyyy four digit year ex : 2014 2014

Time Format

Time Format Description Value
h single digit hours in 12hours format 9
hh double digit hours in 12 hour format 09
H single digit hours in 24 hour format 8AM as 8
8PM as 20
HH double digit hours in 24 hour format 8AM as 08
8PM as 20
m single digit minute 9
mm double digit minute 09
s single digit second 9
ss double digit second 09
a Marker am/pm


Format Description
” . “ Dots or full stops
” _ “ Hyphens or dashes
” ” Spaces
” : “ colon mostly used between time
” / “ Slash