一个能获取当前是星期几的java文件
时间: 2024-05-09 21:21:06 浏览: 86
以下是一个获取当前星期几的Java代码示例:
```java
import java.util.Calendar;
public class GetDayOfWeekExample {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
String dayOfWeekString = "";
switch (dayOfWeek) {
case Calendar.SUNDAY:
dayOfWeekString = "星期日";
break;
case Calendar.MONDAY:
dayOfWeekString = "星期一";
break;
case Calendar.TUESDAY:
dayOfWeekString = "星期二";
break;
case Calendar.WEDNESDAY:
dayOfWeekString = "星期三";
break;
case Calendar.THURSDAY:
dayOfWeekString = "星期四";
break;
case Calendar.FRIDAY:
dayOfWeekString = "星期五";
break;
case Calendar.SATURDAY:
dayOfWeekString = "星期六";
break;
}
System.out.println("今天是:" + dayOfWeekString);
}
}
```
此代码使用Java Calendar类获取当前日期,并将日期转换为星期几字符串。在switch语句中,使用Calendar类中定义的星期几常量来匹配当前日期,并将其转换为相应的字符串。最后,打印输出当前星期几的字符串。
阅读全文