java中SimpleDateFormat的一點技巧

我有健康發表於2014-11-22

將星期幾的中文變成英文輸出:

import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Locale;
import java.text.SimpleDateFormat;

public class abc{
	public static void main(String[] args){
		GregorianCalendar gc = new GregorianCalendar();
		int today = gc.get(Calendar.DAY_OF_MONTH);
		int month = gc.get(Calendar.MONTH);
		gc.set(Calendar.DAY_OF_MONTH, 1);
		
		int weekday = gc.get(Calendar.DAY_OF_WEEK);
		
		int weekOfFirstDay = gc.getFirstDayOfWeek();
		
		int indent = 0;
		while(weekday != weekOfFirstDay){
			indent ++;
			gc.add(Calendar.DAY_OF_MONTH, -1);;
			weekday = gc.get(Calendar.DAY_OF_WEEK);
		}

		SimpleDateFormat sdf = new SimpleDateFormat("F", Locale.US);
		String[] weekdayNames = sdf.getDateFormatSymbols().getShortWeekdays();
		
		do{
			System.out.printf("%4s", weekdayNames[weekday]);
			gc.add(Calendar.DAY_OF_MONTH, 1);
			weekday = gc.get(Calendar.DAY_OF_WEEK);
		}while(weekday != weekOfFirstDay);
		
		System.out.println();
	}
}
 輸出如下:


Sun Mon Tue Wed Thu Fri Sat




相關文章