如何从Android中选定的日期月份和年份中获取星期几名称?

Pra*_*kar -4 java android date dayofweek android-datepicker

我尝试了下面的代码,但它给了我两天前星期几的名称。

  DatePicker picker;
  int date = picker.DayOfMonth;
  int month = (picker.Month + 1);//month is 0 based
  int year = picker.Year;

  SimpleDateFormat simpledateformat = new SimpleDateFormat("EEE");
  Date dt = new Date(year, month, date);
Run Code Online (Sandbox Code Playgroud)

Nil*_*hod 5

首先使用将您的日期转换为特定的日期格式 SimpleDateFormat

使用 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");在本周获得国庆节的名称

WHERE EEEE-> 周中的日期名称

示例代码

 SimpleDateFormat inFormat = new SimpleDateFormat("dd-MM-yyyy");
 try {
       Date myDate = inFormat.parse(date+"-"+month+"-"+year);
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
       String dayName=simpleDateFormat.format(myDate);
 } catch (ParseException e) {
     e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)