获取Java日期的上周日期范围 [英] Get last week date range for a date in Java

查看:734
本文介绍了获取Java日期的上周日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个日期2013年6月20日



如何获取上周的日期范围,即在这种情况下6月9日至6月15日。 p>

另外如果日期是2013年6月2日



范围应该是26可能到1月6日b $ b

解决方案

这是基于Java日历的解决方案

  Date date = new Date(); 
日历c = Calendar.getInstance();
c.setTime(date);
int i = c.get(Calendar.DAY_OF_WEEK) - c.getFirstDayOfWeek();
c.add(Calendar.DATE,-i-7);
日期start = c.getTime();
c.add(Calendar.DATE,6);
Date end = c.getTime();
System.out.println(start + - + end);

输出

  Mon Jun 10 13:22:01 EEST 2013  -  Sun Jun 16 13:22:01 EEST 2013 

它本地化,在我的区域周中从星期一开始


Suppose I have a Date 20 June 2013

How can I get the Date range for the last week, ie in this case 9 June to 15 June.

Also if the date was 2nd June 2013

the range should be 26 may to 1 june

解决方案

this is Java Calendar based solution

    Date date = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int i = c.get(Calendar.DAY_OF_WEEK) - c.getFirstDayOfWeek();
    c.add(Calendar.DATE, -i - 7);
    Date start = c.getTime();
    c.add(Calendar.DATE, 6);
    Date end = c.getTime();
    System.out.println(start + " - " + end);

output

Mon Jun 10 13:22:01 EEST 2013 - Sun Jun 16 13:22:01 EEST 2013

it's localized, in my Locale week starts with Monday

这篇关于获取Java日期的上周日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆