功能:今天,昨天,本周,上周,本月,上月
范围:以周一作为一周的第一天,每周天数5天
代码:
import
java.util.Date;
import
java.util.GregorianCalendar;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
TestCalendar
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public static String begin = "" ;
public static String end = "" ;
public static String now = new java.sql.Date( new Date().getTime()).toString();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) ... {
// 今天
calcToday(begin,end,now, new GregorianCalendar());
// 昨天
calcYesterday(begin,end,now, new GregorianCalendar());
// 本周
calcThisWeek(begin,end,now, new GregorianCalendar());
// 上周
calcLastWeek(begin,end,now, new GregorianCalendar());
// 本月
calcThisMonth(begin,end,now, new GregorianCalendar());
// 上月
calcLastMonth(begin,end,now, new GregorianCalendar());
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void calcToday(String begin,String end,String now,GregorianCalendar calendar) ... {
begin = now;
end = now;
System.out.println( " begin: " + begin);
System.out.println( " end: " + end);
System.out.println( " ---------------------- " );
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void calcYesterday(String begin,String end,String now,GregorianCalendar calendar) ... {
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
calendar.add(GregorianCalendar.DATE, - 1 );
begin = new java.sql.Date(calendar.getTime().getTime()).toString();
end = begin;
System.out.println( " begin: " + begin);
System.out.println( " end: " + end);
System.out.println( " ---------------------- " );
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void calcThisWeek(String begin,String end,String now,GregorianCalendar calendar) ... {
end = now;
int minus = calendar.get(GregorianCalendar.DAY_OF_WEEK) - 2 ;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (minus < 0 ) ... {
System.out.println( " 本周还没有开始,请查询上周 " );
System.out.println( " ---------------------- " );
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ... {
calendar.add(GregorianCalendar.DATE, - minus);
begin = new java.sql.Date(calendar.getTime().getTime()).toString();
System.out.println( " begin: " + begin);
System.out.println( " end: " + end);
System.out.println( " ---------------------- " );
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void calcLastWeek(String begin,String end,String now,GregorianCalendar calendar) ... {
int minus = calendar.get(GregorianCalendar.DAY_OF_WEEK) + 1 ;
calendar.add(GregorianCalendar.DATE, - minus);
end = new java.sql.Date(calendar.getTime().getTime()).toString();
calendar.add(GregorianCalendar.DATE, - 4 );
begin = new java.sql.Date(calendar.getTime().getTime()).toString();
System.out.println( " begin: " + begin);
System.out.println( " end: " + end);
System.out.println( " ---------------------- " );
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void calcThisMonth(String begin,String end,String now,GregorianCalendar calendar) ... {
end = now;
int dayOfMonth = calendar.get(GregorianCalendar.DATE);
calendar.add(GregorianCalendar.DATE, - dayOfMonth + 1 );
begin = new java.sql.Date(calendar.getTime().getTime()).toString();
System.out.println( " begin: " + begin);
System.out.println( " end: " + end);
System.out.println( " ---------------------- " );
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void calcLastMonth(String begin,String end,String now,GregorianCalendar calendar) ... {
calendar.set(calendar.get(GregorianCalendar.YEAR),calendar.get(GregorianCalendar.MONTH), 1 );
calendar.add(GregorianCalendar.DATE, - 1 );
end = new java.sql.Date(calendar.getTime().getTime()).toString();
int month = calendar.get(GregorianCalendar.MONTH) + 1 ;
begin = calendar.get(GregorianCalendar.YEAR) + " - " + month + " -01 " ;
System.out.println( " begin: " + begin);
System.out.println( " end: " + end);
System.out.println( " ---------------------- " );
}
}
结果:
查询今天
begin:2007-12-18
end:2007-12-18
----------------------
查询昨天
begin:2007-12-17
end:2007-12-17
----------------------
查询本周
begin:2007-12-17
end:2007-12-18
----------------------
查询上周
begin:2007-12-10
end:2007-12-14
----------------------
查询本月
begin:2007-12-01
end:2007-12-18
----------------------
查询上月
begin:2007-11-01
end:2007-11-30
----------------------