Android获取系统当前日期及将日期转换成对应的星期

时间:2022-10-10 23:33:02
  Calendar c = Calendar.getInstance();
  Date date = c.getTime();
    String s = DateToWeek(date);
 Log.e("ssssss", "ssss" + s);
  public static String[] WEEK = {"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
    public static final int WEEKDAYS = 7;

    public static String DateToWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayIndex < 1 || dayIndex > WEEKDAYS) {
            return null;
        }

        return WEEK[dayIndex - 1];
    }

这样就可以很简单的把当前的时间转化成 星期几了!!
当然我们也可以通过这个类Calendar 得到 当前的年月日 小时,分钟,秒 都可以得到 下面看代码

         Calendar c = Calendar.getInstance();
        Date date = c.getTime();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
//        int date = c.get(Calendar.DATE);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);