微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在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.jb51.cc/java/121468.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐