Android System Date And Time In Different Format

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
<RelativeLayout
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="22sp" />

</RelativeLayout>

file : MainActivity
package com.example.sysdatetime;

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

public class MainActivity extends Activity {

 TextView tv;
 Button btn;

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

  tv = (TextView) findViewById(R.id.display);

  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 FormatDescriptionValue
dSingle digit date eg 11
dddouble digit date eg 0101
MSingle digit month eg: 11
MMDouble digit month eg: 0101
MMMthree letter abbreviation for month ex: janjan
MMMMmonth spelled out in full ex : januaryjanuary
yydouble digit year ex : 1414
yyyyfour digit year ex : 20142014


Time Format
Time FormatDescriptionValue
hsingle digit hours in 12hours format9
hhdouble digit hours in 12 hour format09
Hsingle digit hours in 24 hour format8AM as 8
8PM as 20
HHdouble digit hours in 24 hour format8AM as 08
8PM as 20
msingle digit minute9
mmdouble digit minute09
ssingle digit second9
ssdouble digit second09
aMarkeram/pm


Separator
FormatDescription
" . "Dots or full stops
" _ "Hyphens or dashes
" " Spaces
" : "colon mostly used between time
" / "Slash

3 comments:

sarah lee said...

I believe that we are solely responsible for our choices, and we have to accept the consequences of every deed, word, and thought throughout our lifetime. See the link below for more info.


#lifetime
www.matreyastudios.com

Leslie Lim said...

I read, enjoy and learn from your blog. Thanks! Keep on posting.

Benjie
www.imarksweb.org

ologunde manasseh said...

Thank you billions

Post a comment