简体   繁体   中英

Android Studio: Bug with Calendar.DAY_OF_WEEK

I didn't found my "bug" in another question, so I really need help.

In my app, I have this code:

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MONTH, Calendar.MONTH);
    calendar.set(Calendar.YEAR, Calendar.YEAR);
    calendar.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH);

    calendar.set(Calendar.HOUR_OF_DAY, Calendar.HOUR);
    calendar.set(Calendar.MINUTE, Calendar.MINUTE);
    calendar.set(Calendar.SECOND, Calendar.SECOND);
    frase(calendar);

This code is inside onCreate and the code below is below onCreate();

     public void frase(Calendar calendar)
{
    int day = calendar.get(Calendar.DAY_OF_WEEK);

    switch (day) {
        case Calendar.MONDAY:textView.setText(getText(R.string.segunda));break;
        case Calendar.TUESDAY:textView.setText(getText(R.string.terca));break;
        case Calendar.WEDNESDAY:textView.setText(getText(R.string.quarta));break;
        case Calendar.THURSDAY:textView.setText(getText(R.string.quinta));break;
        case Calendar.FRIDAY:textView.setText(getText(R.string.sexta));break;
        case Calendar.SATURDAY:textView.setText(getText(R.string.sabado));break;
        case Calendar.SUNDAY:textView.setText(getText(R.string.domingo));break;
    }
}

When I run the app, my emulator always returns me the Saturday case but the emulator day of the week is wednesday.

The second argument to Calendar.set() is the value you want to that particular field defined by the first argument. Now you're setting constant values ie field index numbers, which makes no sense.

Remove your set() calls altogether. Calendar.getInstance() already returns an instance that is initialized to current date and time.

Get Date

public static String getDate() {
        Date myDate = new Date();
        String date = new SimpleDateFormat("yyyy-MM-dd").format(myDate);
       // new SimpleDateFormat("hh-mm-a").format(myDate);
        return date;
    }

Get day of week

 public static int dayOfWeek(String date) {
        try {
            int day = 0;
            SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy-MM-dd");
            Date now = simpleDateformat.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(now);
            day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            return day;
        } catch (Exception e) {
            return null;
        }
    }

Or simply call

getDay(new Date());

public static String getDay(Date date){

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
    //System.out.println("DAY "+simpleDateFormat.format(date).toUpperCase());                       
    return simpleDateFormat.format(date).toUpperCase();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM