android得到了星期天的日期

时间:2017-02-21 08:24:47

标签: android date

我正在使用以下代码来获取即将到来的星期日。代码在某些设备上工作,但在某些设备中,它在周日到周日后显示。请帮忙。为什么它不适用于某些设备。

try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            Calendar c=Calendar.getInstance();
            c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
            c.set(Calendar.HOUR_OF_DAY,0);
            c.set(Calendar.MINUTE,0);
            c.set(Calendar.SECOND,0);
            c.add(Calendar.DATE,7);

            Date currentTime = c.getTime();
            date = dateFormat.format(currentTime);

        } catch (Exception e) {
            e.printStackTrace();
        }

2 个答案:

答案 0 :(得分:0)

可能是TimeZone的问题?
试试这个:

 Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("some time zone"));

答案 1 :(得分:0)

private static Calendar getNextSundayDate() {
        Calendar calendarForNextSunday = Calendar.getInstance();
        int today = calendarForNextSunday.get(Calendar.DAY_OF_WEEK);
        //System.out.println("today" + today);
        if (today != Calendar.SUNDAY) {
            int offset = Calendar.SATURDAY - today + Calendar.SUNDAY;
            //System.out.println("offset" + offset);
            calendarForNextSunday.add(Calendar.DATE, offset);
            //System.out.println("new" + calendarForNextSunday.get(Calendar.DAY_OF_WEEK));
            //System.out.println("next sunday" + calendarForNextSunday.get(Calendar.DATE));
        }
        return calendarForNextSunday;
    }

这可能适合你