java代码求时间问题

我想求出 系统七天前的时间,怎么总是出错,

 

package com.pa;

public class DateTest
{
 private String year;
 private String month;
 private String day;
    private int yy,mm,dd;
 //初始化是取得系统时间
 public DateTest()
 {
 
  java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date currentTime = new java.util.Date();
        String date = formatter.format(currentTime);
          year = date.substring(0,4);
          month = date.substring(5,7);
          day = date.substring(8,10);
         
           yy = Integer.parseInt(year); 
           mm = Integer.parseInt(month);
           dd = Integer.parseInt(day);
 }
   
 
 
  //得到昨天的时间
    public int getLastMonthDay(int MM)
    {
     int new_mm = MM-1;
     int new_dd;
     if(yy%4==0)//判断闰年,平年,得到二月的实际天数
     {
       new_dd=29;
     }
  
  else if(new_mm==1||new_mm==3||new_mm==5||new_mm==7||new_mm==8||new_mm==10||new_mm==12)//判断月份,得到除二月外的具体天数
     {
         new_dd=31;
     }
     else if(mm==4||mm==6||mm==9||mm==11)
     {
      new_dd=30;
     }
     else
     {
      new_dd=28;
     }  
     return new_dd;
    }
  
    public String getYesterdayTime()
    {  
     String yesterday;
     int dd = Integer.parseInt(day);
     if(dd==1)
     {
      if(mm==1)
      {
       yesterday = String.valueOf(yy-1)+"-12-31";
      }
      else
      {
       yesterday = year+"-"+String.valueOf(mm-1)+"-"+String.valueOf(getLastMonthDay(mm));
      }
     }
     else
     {
      yesterday = year+"-"+month+"-"+String.valueOf(dd-1);
     }
     return yesterday;
    }
    //得到上个礼拜的时间
 
    public String getLastWeekTime()
    {
     String lastWeekTime;
     if(dd<7)
     {
       if(mm==1)
       {  
       
        lastWeekTime = String.valueOf(yy-1)+"-12"+"-"+String.valueOf(31+dd-7);
       }
       else
       {
        int m = mm-1;
        int d = getLastMonthDay(mm)+dd-7;
        lastWeekTime = year+"-"+String.valueOf(m)+"-"+String.valueOf(d);
       }
     }
     if(dd==7)
     {
      if(mm==1)
      {
       lastWeekTime = String.valueOf(yy-1)+"-12-31";
      }
      else
      {
       lastWeekTime = year+"-"+String.valueOf(mm-1)+"-"+String.valueOf(getLastMonthDay(mm));
      }
     }
     else
     {
      lastWeekTime = year+"-"+month+"-"+String.valueOf(dd-7);
     }
     return lastWeekTime;
    }
    //得到上个月的时间
    public String getLastMonth()
    {
     String lastMonthDay;
     if(mm==1)
     {
      lastMonthDay = String.valueOf(yy-1)+"-12-31";
     }
     else
     {
      lastMonthDay = year+"-"+String.valueOf(mm-1)+"-"+day;
     }
     return lastMonthDay;
    }
    //得到上个季度时间
    public String getLastQuarterTime()
    {
     String lastQuarterTime;
     String startMonthTime = null;
     String endMonthTime = null;
     if(mm>=1&&mm<=3)
     {
      startMonthTime = String.valueOf(yy-1)+"-10-31";
      endMonthTime = String.valueOf(yy-1)+"-12-31";
     }
     if(mm>=4&&mm<=6)
     {
      startMonthTime = year+"-01-31";
      endMonthTime = year+"-03-31";
     }
     if(mm>=7&&mm<=9)
     {
      startMonthTime = year+"-04-30";
      endMonthTime = year+"-06-30";
     }
     if(mm>=10&&mm<=12)
     {
      startMonthTime = year+"-07-31";
      endMonthTime = year+"-09-30";
     }
     lastQuarterTime = startMonthTime+"*"+endMonthTime;
     
  return lastQuarterTime;
     
    }
    public static void main(String args[])
    {
     DateTest dt = new DateTest();
   
     String lastweek = dt.getLastWeekTime();
     
     
     System.out.print(lastweek);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值