วันอาทิตย์ที่ 15 มีนาคม พ.ศ. 2558

ตัวอย่างโค้ดการแสดงรูปแบบเดือนแบบต่าง ๆ M, MM, MMM, MMMM

ตัวอย่างโค้ดการแสดงรูปแบบเดือนแบบต่าง ๆ M, MM, MMM, MMMM

สวัสดีคับ บทความนี้เรามาดูว่าการแสดงรูปแบบเดือนในรูปแบบต่าง ๆ ควรใช้แบบไหนดี และมีความแตกต่างกันอย่างไร เช่น M, MM, MMM, MMMM

ตัวอย่างโค้ด

package javacodeexam;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ShowMonthFormatAll {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf;

sdf = new SimpleDateFormat("M");
System.out.println("Current Month in M format : " + sdf.format(date));

sdf = new SimpleDateFormat("MM");
System.out.println("Current Month in MM format : " + sdf.format(date));

sdf = new SimpleDateFormat("MMM");
System.out.println("Current Month in MMM format : " + sdf.format(date));

sdf = new SimpleDateFormat("MMMM");
System.out.println("Current Month in MMMM format : " + sdf.format(date));
}
}

ตัวอย่างผลลัพธ์ที่ได้

Current Month in M format : 3
Current Month in MM format : 03
Current Month in MMM format : Mar
Current Month in MMMM format : March

จากโค้ดตัวอย่างเราแสดงรูปแบบการกำหนดรูปแบบของเดือน เป็นแบบ M, MM, MMM, MMMM แต่ละแบบจะแสดงต่าง ๆ กันตามผลลัพธ์ที่ได้

yengo หรือ buzzcity

ไม่มีความคิดเห็น :

แสดงความคิดเห็น