在Java中获取当前周的开始和结束日期 – (MONDAY TO SUNDAY)

前端之家收集整理的这篇文章主要介绍了在Java中获取当前周的开始和结束日期 – (MONDAY TO SUNDAY)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
今天是2014-04-06 – SUNDAY.

输出使用以下代码是: –

开始日期= 2014-04-07

结束日期= 2014-04-13

我想输出为: –

开始日期= 2014-03-31

结束日期= 2014-04-06

// Get calendar set to current date and time
        Calendar c = GregorianCalendar.getInstance();

        System.out.println("Current week = " + Calendar.DAY_OF_WEEK);

        // Set the calendar to monday of the current week
        c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        System.out.println("Current week = " + Calendar.DAY_OF_WEEK);

        // Print dates of the current week starting on Monday
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault());
        String startDate = "",endDate = "";

        startDate = df.format(c.getTime());
        c.add(Calendar.DATE,6);
        endDate = df.format(c.getTime());

        System.out.println("Start Date = " + startDate);
        System.out.println("End Date = " + endDate);

解决方法

我相信只需使用:
c.setFirstDayOfWeek(Calendar.MONDAY);

解决你的问题… 原文链接:https://www.f2er.com/java/121468.html

猜你在找的Java相关文章