본문 바로가기
프로그래밍/DB/JAVA/안드로이드/GAE

[안드로이드] View에 날짜를 보여줄 경우 포맷 관련

by 아유카와 2015. 9. 15.




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 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