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

//获取当前日期
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();


public static final int WEEKDAYS = 7;
 
 public static String[] WEEK = {
  “SUNDAY”,
  “MONDAY”,
  “TUESDAY”,
  “WEDNESDAY",
  ”THRUSDAY",
  “FRIDAY",
  ”SATURDAY
};

	/**
	 * 日期变量转成对应的星期字符串
	 * @param date
	 * @return
	 */
	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];
	}


注意:int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);返回的index, 周日是每周的第一天, index = 1

 

你可能感兴趣的:(Android开发)