您的位置:首页 > 其它

根据日期获取星期几的两种方法

2017-04-05 10:49 561 查看
private String date = "2017-05-21";//
SimpleDateFormat simpleDateFormat;


simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
b11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Date date1 = simpleDateFormat.parse(date);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("E");
String s1 = simpleDateFormat.format(date1);
String s2 = simpleDateFormat2.format(date1);
b11.setText(s1 + ":" + s2);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
b12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
String s1 = "";
try {
calendar.setTime(simpleDateFormat.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
if ((calendar.get(Calendar.DAY_OF_WEEK)) == 1) {
s1 = "周日";
}
if ((calendar.get(Calendar.DAY_OF_WEEK)) == 2) {
s1 = "周一";
}
if ((calendar.get(Calendar.DAY_OF_WEEK)) == 3) {
s1 = "周二";
}
if ((calendar.get(Calendar.DAY_OF_WEEK)) == 4) {
s1 = "周三";
}
if ((calendar.get(Calendar.DAY_OF_WEEK)) == 5) {
s1 = "周四";
}
if ((calendar.get(Calendar.DAY_OF_WEEK)) == 6) {
s1 = "周五";
}
if ((calendar.get(Calendar.DAY_OF_WEEK)) == 7) {
s1 = "周六";
}
b12.setText(s1);
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: