美文网首页
Java Calendar

Java Calendar

作者: 风雨楼兰 | 来源:发表于2018-02-02 16:48 被阅读0次

获取本周、本月第一天、最后一天

    
    public static void main(String[] args){
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"),Locale.CHINESE);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        System.out.println("first week date: "+sdf.format(cal.getTime()));
        cal.add(Calendar.WEEK_OF_YEAR,1);
        cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        System.out.println("last week date:"+sdf.format(cal.getTime()));
        cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"),Locale.CHINESE);
        cal.set(Calendar.DAY_OF_MONTH,1);
        System.out.println("first Month date"+sdf.format(cal.getTime()));
        cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"),Locale.CHINESE);
        cal.add(Calendar.MONTH,1);
        cal.set(Calendar.DAY_OF_MONTH,0);
        System.out.println("last Month date:"+sdf.format(cal.getTime()));
    }

相关文章

网友评论

      本文标题:Java Calendar

      本文链接:https://www.haomeiwen.com/subject/zxsezxtx.html