Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

android - Get day of the week from GregorianCalendar

I have a date and I need to know the day of the week, so I used a GregorianCalendar object but I get back some dates that are incorrect.

GregorianCalendar calendar = new GregorianCalendar(year, month, day);
int i = calendar.get(Calendar.DAY_OF_WEEK);

What am I doing wrong?

Thanks!

EDIT SOLUTION:

mont--;
GregorianCalendar calendar = new GregorianCalendar(year, month, day);
int i = calendar.get(Calendar.DAY_OF_WEEK);

    if(i == 2){
        dayOfTheWeek = "Mon";           
    } else if (i==3){
        dayOfTheWeek = "Tue";
    } else if (i==4){
        dayOfTheWeek = "Wed";
    } else if (i==5){
        dayOfTheWeek = "Thu";
    } else if (i==6){
        dayOfTheWeek = "Fri";
    } else if (i==7){
        dayOfTheWeek = "Sat";
    } else if (i==1){
        dayOfTheWeek = "Sun";
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
TimeZone timezone = TimeZone.getDefault();
Calendar calendar = new GregorianCalendar(timezone);
calendar.set(year, month, day, hour, minute, second);

String monthName=calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());//Locale.US);
String dayName=calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault());//Locale.US);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...