Java设置每周从周一开始

      我们有时候代码中会获取周数据,这时候会用到Calendar类,来获取当前是该年第几周。不过很多时候大家实现方式会有坑,我就碰到过。。。。。。一言难尽哪。

       国外都是以周日作为一周的开始,但我们中国人一般习惯把周一作为一周的开始。如果你不注意的话,当你在周日获取第几周,按周一来算本该是第5周的话,你可能会取到第6周的数据,这种感觉会让你非常酸爽的。     

       下面代码通过两种方式来实现周从周一开始,但是通过天数减一的方式是万万不可取的,因为大坑就在你面前,不行你就迈个步试试~ 

/**
 * Created by whc on 18/1/8.
 */
public class DateUtils {

    /**
     * 从周一开始获取当前周数(调Java原生方法)
     * @return
     * @throws ParseException
     */
    public static String getCurrentWeekWithYearStartWithMonday(String dateDesc) throws ParseException {
        Calendar c = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        c.setTime(df.parse(dateDesc));
        c.setFirstDayOfWeek(Calendar.MONDAY);
        return "" + c.get(Calendar.YEAR) + c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取当前周数(通过当前天数减1实现)
     * @param dateDesc
     * @return
     * @throws ParseException
     */
    public static String getCurrentWeekWithYearStartWithMondayV2(String dateDesc) throws ParseException {
        Calendar c = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        c.setTime(df.parse(dateDesc));

        c.add(Calendar.DAY_OF_YEAR, -1);

        return "" + c.get(Calendar.YEAR) + c.get(Calendar.WEEK_OF_YEAR);
    }

    public static String getCurrentWeekWithYearStartWithMondayV3(String dateDesc) throws ParseException {
        Calendar c = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        c.setTime(df.parse(dateDesc));

//        c.add(Calendar.DAY_OF_YEAR, -1);

        return "" + c.get(Calendar.YEAR) + c.get(Calendar.WEEK_OF_YEAR);
    }


    public static void main(String[] args) throws ParseException {

        /**
         * 2018.1.7是周日,按周日为每周第一天算的话,应该是2018年第二周的第一天;按周一为每周第一天算的话,应该是2018年第一周的最后一天。
         * 从结果来看,两种实现方式都是20181,没有问题
         */
        String dateDesc1 = "2018-01-07 05:00:00";
        System.out.println("get week numbers of 2018-01-07 || " + getCurrentWeekWithYearStartWithMonday(dateDesc1) + " VS " + getCurrentWeekWithYearStartWithMondayV2(dateDesc1));

        /**
         * 2018.1.8是周一,按周日为每周第一天算的话,应该是2018年第二周的第二天;按周一为每周第一天算的话,应该是2018年第二周的第一天。
         * 从结果来看,两种实现方式都是20182,没有问题
         */
        String dateDesc2 = "2018-01-08 05:00:00";
        System.out.println("get week numbers of 2018-01-08 || " + getCurrentWeekWithYearStartWithMonday(dateDesc2) + " VS " + getCurrentWeekWithYearStartWithMondayV2(dateDesc2));

        /**
         * 2018.1.1是周一,按周日为每周第一天算的话,应该属于2018年第一周;按周一为每周第一天算的话,应该是2018年第一周的第一天。
         * 从结果来看,第一种实现方式是20181,没问题;第二种实现方式是20171,完全错误,bug就此产生
         */
        String dateDesc3 = "2018-01-01 05:00:00";
        System.out.println("get week numbers of 2018-01-01 || " + getCurrentWeekWithYearStartWithMonday(dateDesc3) + " VS " + getCurrentWeekWithYearStartWithMondayV2(dateDesc3));

        String dateDesc4 = "2017-12-31 05:00:00";
        System.out.println("get week numbers of 2017-12-31 || " + getCurrentWeekWithYearStartWithMonday(dateDesc4) + " VS " + getCurrentWeekWithYearStartWithMondayV2(dateDesc4));




    }


}

下面是2018年1月日历截图,方便大家对照:

参考资料:http://blog.cangzhitao.com/post/java/first-day-of-week.htm

转载于:https://my.oschina.net/whc20011/blog/1604114

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值