Java计算日期今天,昨天,上周,上月

功能:今天,昨天,本周,上周,本月,上月

范围:以周一作为一周的第一天,每周天数5天

代码:

 

import  java.util.Date;
import  java.util.GregorianCalendar;


public   class  TestCalendar  ... {

    
public   static  String begin = "" ;
    
public   static  String end = "" ;
    
public   static  String now = new  java.sql.Date( new  Date().getTime()).toString();
    
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());
        
    }

    
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(
" ---------------------- " );    
    }

    
    
public   static   void  calcYesterday(String begin,String end,String now,GregorianCalendar calendar) ... {

    
        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(
" ---------------------- " );    
    }

    
    
public   static   void  calcThisWeek(String begin,String end,String now,GregorianCalendar calendar) ... {
        end
= now;
        
int  minus = calendar.get(GregorianCalendar.DAY_OF_WEEK) - 2 ;
        
if (minus < 0 ) ... {
            System.out.println(
" 本周还没有开始,请查询上周 " );
            System.out.println(
" ---------------------- " );
        }
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(
" ---------------------- " );
        }

    }

    
    
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(
" ---------------------- " );
    }


    
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(
" ---------------------- " );
    }

    
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
----------------------

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值