java中SimpleDateFormat的一点技巧

2022-11-13 13:30:31 浏览数 (1)

将星期几的中文变成英文输出:

代码语言:javascript复制
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

0 人点赞