Java程序获取星期几作为字符串
要获取星期几作为字符串,首先让我们显示当前日期-
LocalDate currentDate = LocalDate.now();
现在,使用DayOfWeek从当前日期获取星期几-
DayOfWeek day = currentDate.getDayOfWeek();
获取星期几作为字符串-
String weekName = day.name();
示例
import java.time.DayOfWeek; import java.time.LocalDate; public class Demo { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); System.out.println("Current Date = "+currentDate); DayOfWeek day = currentDate.getDayOfWeek(); int weekVal = day.getValue(); String weekName = day.name(); System.out.println("Week Number = "+weekVal); System.out.println("Week Name = "+weekName); } }
输出结果
Current Date = 2019-04-12 Week Number = 5 Week Name = FRIDAY