Thursday, March 28

About Time ตัวอย่างการใช้ SimpleDateFormat

Google+ Pinterest LinkedIn Tumblr +

ผมเองโดยปกติก็มีปัญหาเกี่ยวกับการแสดงวันเวลาใน App Android บ่อยๆ เพราะไม่ค่อยได้ใช้ เวลาจะใช้ทีจึงต้องหาข้อมูลใหม่ตลอด ผมจึงอยากจะรวมสิ่งที่ผมเคยเรียนรู้เกี่ยวกับ Date เอาไว้ที่เดียวกันเลย เผื่อคนอื่นอยากจะเรียนรู้จะได้เข้าใจ

เอาล่ะ เริ่มกันเลย ในการแสดงวันเวลาผ่าน Textview เราจะต้องเข้าใจ Format ของ Date ก่อนเพื่อให้เราสามารถแสดงวันเวลาได้อย่างถูกต้องตามที่เราต้องการ เริ่มกันที่ SimpleDateFormat

SimpleDateFormat เป็นการจัด Format ของเวลาให้อยู่ในรูปแบบที่เราต้องการ โดยเราสามารถกำหนดได้ว่าจะให้ วัน เดือน ปี ชั่วโมง นาที วินาที อยู่ในตำแหน่งไหนก็ได้ มาดูวิธีประกาศตัวแปรกัน

[java] Date today = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");
String dateToday = formatter.format(today);
[/java]

ตัวอย่างข้างบนเมื่อแสดงบน Textview จะได้ ผลลัพธ์แบบนี้ (Code ยังไม่ครับนะ เป็นตัวอย่างเฉยๆ)
[java] 2015-10-13-22.26.30
[/java]

มาดูกันว่าเราแต่ละตัวมีความหมายว่าอย่างไร
yyyy คือ ปี ค.ศ. เช่น 2015 ตัวอย่างแบบอื่นๆ
[java] “yy” จะได้ 15
“y” , “yyy”, “yyyy” จะได้ 2015
[/java]

MM คือ เดือน เป็นได้ทั้งตัวเลขและตัวอักษร แล้วแต่รูปแบบ
[java] “M” จะได้ 1 สำหรับมกราคม 12 สำหรับธันวาคม
“MM” จะได้ 01 สำหรับมกราคม 12 สำหรับธันวาคม
“MMM” จะได้ Jan สำหรับมกราคม Dec สำหรับธันวาคม
“MMMM” จะได้ January สำหรับมกราคม December สำหรับธันวาคม
[/java]

dd คือ วัน เป็นได้ทั้งเป็นเลขของวัน
[java] “d” จะได้ 1 ได้ 31
“dd” จะได้ 01 ได้ 31
[/java] [java] hh คือ จะเป็นเลขชั่วโมง 1-12 สำหรับ AM/PM
HH คือ จะได้เลขชั่วโมงเป็น 0 – 23
[/java] [java] mm คือ เลขนาที จะแสดงเป็นตัวเลข ใช้ได้ทั้ง “m” และ “mm”
[/java] [java] ss คือ เลขวินาที จะแสดงเป็นตัวเลข ใช้ได้ทั้ง “s” และ “ss”
[/java]

EEE จะแสดงวันของสัปดาห์ จันทร์ – ศุกร์
[java] “EEE” จะได้ Mon สำหรับวันจันทร์
“EEEE” จะได้ Monday สำหรับวันจันทร์
[/java]

D จะแสดงวันของปี เช่น วันที่ 65 ของปี จะมีได้ทั้ง 365 และ 364 วัน
[java] “D” จะได้ 65
“DDD” จะได้ 065
[/java]

หลักๆแล้วเราก็น่าจะใช้กันประมาณนี้ เราสามารถเรียงสลับไปมากันได้เต็มที่ ขอแค่ถูก Format ก็พอ
[java] “EEE d MMMM yyyy” จะได้ “Monday 12 October 2015”
“MM/dd/yy” จะได้ “10/12/15”
“MM-dd-yyyy hh:mm:ss” จะได้ “10-12-2015 22:55:20”
[/java]

ขอจบ About Time ภาคแรกกันที่ SimpleFotmatDate แล้วกันนะครับ กว่าจะยาว ตอนต่อไปเราจะมาดูวิธีใช้กัน ด้านล่างเป็น Code ของการหาวันเดือนปีปัจจุบัน

MainActivity.java
[java] public class MainActivity extends AppCompatActivity {

TextView yearTxt, monthTxt, dayTxt, hourTxt, minuteTxt, secondTxt;
TextView dayOfWeekTxt, dayOfYearTxt, myFormatTxt;

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

yearTxt = (TextView) findViewById(R.id.date_year);
monthTxt = (TextView) findViewById(R.id.date_month);
dayTxt = (TextView) findViewById(R.id.date_day);
hourTxt = (TextView) findViewById(R.id.date_hour);
minuteTxt = (TextView) findViewById(R.id.date_hour);
secondTxt = (TextView) findViewById(R.id.date_second);
dayOfWeekTxt = (TextView) findViewById(R.id.date_dayOfWeek);
dayOfYearTxt = (TextView) findViewById(R.id.date_dayOfYear);
myFormatTxt = (TextView) findViewById(R.id.date_myFormat);

Date today = Calendar.getInstance().getTime();

//Year Format
String yearString = "Year format";
SimpleDateFormat yearFormat1 = new SimpleDateFormat("yy");
yearString += "\n yy " + yearFormat1.format(today);
SimpleDateFormat yearFormat2 = new SimpleDateFormat("yyyy");
yearString += "\n yyyy " + yearFormat2.format(today);
yearTxt.setText(yearString);

//Month Format
String monthString = "Month format";
SimpleDateFormat monthFormat1 = new SimpleDateFormat("M");
monthString += "\n M " + monthFormat1.format(today);
SimpleDateFormat monthFormat2 = new SimpleDateFormat("MM");
monthString += "\n MM " + monthFormat2.format(today);
SimpleDateFormat monthFormat3 = new SimpleDateFormat("MMM");
monthString += "\n MMM " + monthFormat3.format(today);
SimpleDateFormat monthFormat4 = new SimpleDateFormat("MMMM");
monthString += "\n MMMM " + monthFormat4.format(today);
monthTxt.setText(monthString);

//Day Format
String dayString = "Day format";
SimpleDateFormat dayFormat1 = new SimpleDateFormat("d");
dayString += "\n d " + dayFormat1.format(today);
SimpleDateFormat dayFormat2 = new SimpleDateFormat("dd");
dayString += "\n dd " + dayFormat2.format(today);
dayTxt.setText(dayString);

//Hour Format
String hourString = "Hour format";
SimpleDateFormat hourFormat1 = new SimpleDateFormat("hh");
hourString += "\n hh " + hourFormat1.format(today);
SimpleDateFormat hourFormat2 = new SimpleDateFormat("HH");
hourString += "\n HH " + hourFormat2.format(today);
hourTxt.setText(hourString);

//Minute Format
String minuteString = "Minute format";
SimpleDateFormat minuteFormat1 = new SimpleDateFormat("m");
minuteString += "\n m " + minuteFormat1.format(today);
SimpleDateFormat minuteFormat2 = new SimpleDateFormat("mm");
minuteString += "\n mm " + minuteFormat2.format(today);
minuteTxt.setText(minuteString);

//Second Format
String secondString = "Second format";
SimpleDateFormat secondFormat1 = new SimpleDateFormat("s");
secondString += "\n s " + secondFormat1.format(today);
SimpleDateFormat secondFormat2 = new SimpleDateFormat("ss");
secondString += "\n ss " + secondFormat2.format(today);
secondTxt.setText(secondString);

//Day of Week Format
String dayOfWeekString = "Day of Week format";
SimpleDateFormat dayOfWeekFormat1 = new SimpleDateFormat("EEE");
dayOfWeekString += "\n EEE " + dayOfWeekFormat1.format(today);
SimpleDateFormat dayOfWeekFormat2 = new SimpleDateFormat("EEEE");
dayOfWeekString += "\n EEEE " + dayOfWeekFormat2.format(today);
dayOfWeekTxt.setText(dayOfWeekString);

//Day of Year Format
String dayOfYearString = "Day of Year format";
SimpleDateFormat dayOfYearFormat1 = new SimpleDateFormat("D");
dayOfYearString += "\n D " + dayOfYearFormat1.format(today);
SimpleDateFormat dayOfYearFormat2 = new SimpleDateFormat("DDD");
dayOfYearString += "\n DDD " + dayOfYearFormat2.format(today);
dayOfYearTxt.setText(dayOfYearString);

//My Format
String myString = "Day of Year format";
SimpleDateFormat myFormat1 = new SimpleDateFormat("EEE d MMMM yyyy");
myString += "\n EEE d MMMM yyyy " + myFormat1.format(today);
SimpleDateFormat myFormat2 = new SimpleDateFormat("MM/dd/yy");
myString += "\n MM/dd/yy " + myFormat2.format(today);
SimpleDateFormat myFormat3 = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
myString += "\n MM-dd-yyyy hh:mm:ss " + myFormat3.format(today);
myFormatTxt.setText(myString);

}
}
[/java]

activity_main.xml
[xml] <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:orientation="vertical"
android:id="@+id/content_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_margin="10dp"
android:id="@+id/date_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_margin="10dp"
android:id="@+id/date_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_margin="10dp"
android:id="@+id/date_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_margin="10dp"
android:id="@+id/date_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_margin="10dp"
android:id="@+id/date_minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_margin="10dp"
android:id="@+id/date_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_margin="10dp"
android:id="@+id/date_dayOfWeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_margin="10dp"
android:id="@+id/date_dayOfYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_margin="10dp"
android:id="@+id/date_myFormat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

</ScrollView>

</RelativeLayout>

[/xml]

Reference
http://java2see.blogspot.com/2012/08/java-format-date-example-format-date.html
http://alvinalexander.com/java/simpledateformat-convert-date-to-string-formatted-parse
http://examples.javacodegeeks.com/core-java/text/format-date-in-custom-formats-with-simpledateformat/
http://developer.android.com/reference/java/text/SimpleDateFormat.html

Facebook Comments
Share.

About Author

สวัสดีครับ ผมไอซ์ กมลวัฒน์ ผู้ก่อตั้งช่อง Youtube Kamonway และเว็บไซต์ kamonway.com เพื่อช่วยแนะนำความรู้จากเกม และเป็นช่องทางที่ช่วยให้ทุกท่านเล่นเกมอย่างสนุกสนานยิ่งขึ้น

Comments are closed.