Java 示例 - 显示工作日
-
问题描述
如何显示工作日的名称? -
解决方案
此示例借助 DateFormatSymbols 类的 DateFormatSymbols().getWeekdays() 方法以简短形式显示工作日的名称。import java.text.SimpleDateFormat; import java.text.DateFormatSymbols; public class Main { public static void main(String[] args) { String[] weekdays = new DateFormatSymbols().getWeekdays(); for (int i = 2; i < (weekdays.length-1); i++) { String weekday = weekdays[i]; System.out.println("weekday = " + weekday); } } }
-
结果
上面的代码示例将产生以下结果。weekday = Monday weekday = Tuesday weekday = Wednesday weekday = Thursday weekday = Friday
以下是工作日显示名称的另一个示例import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.Calendar; public class Main { public static void main(String[] argv) throws Exception { Date d = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US); String str = dateFormat.format(d); System.out.println(str); } }
上面的代码示例将产生以下结果。Fri