วันจันทร์ที่ 16 มีนาคม พ.ศ. 2558

ตัวอย่างโค้ด java การแสดงเดือนทุกเดือนแบบสั้น ๆ

ตัวอย่างโค้ด java การแสดงเดือนทุกเดือนแบบสั้น ๆ 


สวัสดีคับ บทความนี้เรามาดูตัวอย่างโค้ดการแสดงเดือนแบบสั้นทุกเดือนกันคับ โดยการใช้ DateFormatSymbols

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

package javacodeexam;

import java.text.DateFormatSymbols;

public class ShowShortMonths {
public static void main(String[] args) {
String[] shortMonths = new DateFormatSymbols().getShortMonths();

for (int i = 0; i < (shortMonths.length - 1); i++) {
String shortMonth = shortMonths[i];
System.out.println("shortMonth = " + shortMonth);
}
}
}

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

shortMonth = Jan
shortMonth = Feb
shortMonth = Mar
shortMonth = Apr
shortMonth = May
shortMonth = Jun
shortMonth = Jul
shortMonth = Aug
shortMonth = Sep
shortMonth = Oct
shortMonth = Nov
shortMonth = Dec

จากโค้ดตัวอย่าง การใช้ DateFormatSymbols().getShortMonths() เพื่อ get ค่าเดือนแบบสั้น ๆ มา ซึ่งจะคืนค่ามาเป็น Array ของ String จากนั้นเราก็ใช้ for loop เพื่อแสดงชื่อเดือนแบบสั้น ๆ

yengo หรือ buzzcity

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

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