In this post we will see how to format system date and time for your android application,for getting current date and time in 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()) + "\n\n"
    + format2.format(c.getTime()) + "\n\n"
    + format3.format(c.getTime()) + "\n\n"
    + format4.format(c.getTime()) + "\n\n"
    + 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


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


0 comments:

Post a Comment

 
Top