当前位置:   article > 正文

一个简单的星期枚举类及应用_编程实现声明枚举类型包括sunday到saturday七个元素在程序中声明weekday类型

编程实现声明枚举类型包括sunday到saturday七个元素在程序中声明weekday类型
  1. /**
  2. * 枚举类
  3. */
  4. public enum WeekEnum {
  5. SUN("周日"),
  6. MON("周一"),
  7. TUE("周二"),
  8. WED("周三"),
  9. THU("周四"),
  10. FRI("周五"),
  11. SAT("周六");
  12. private String weekday;
  13. WeekEnum(String weekday) {
  14. this.weekday = weekday;
  15. }
  16. public String getWeekday() {
  17. return this.weekday;
  18. }
  19. }
  20. /**
  21. * 获取今天是星期几
  22. */
  23. class Test{
  24. public static void main(String[] args) {
  25. Date today = new Date();
  26. Calendar calendar = Calendar.getInstance();
  27. calendar.setTime(today);
  28. // 获取星期索引, 0 代表周日
  29. int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
  30. if (weekIndex < 0) {
  31. weekIndex = 0;
  32. }
  33. String weekday = WeekEnum.values()[weekIndex].getWeekday();
  34. System.out.println(weekday);
  35. // 当然也可以输出英文的星期
  36. WeekEnum enWeekday = WeekEnum.values()[weekIndex];
  37. System.out.println(enWeekday.toString());
  38. }
  39. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/76297
推荐阅读