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);
}
}
评论
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now=new Date();
long nowTime=now.getTime()/1000;
now.setTime((nowTime-24*60*60*7)*1000);
String nowStr=sdf.format(now);
System.out.println(nowStr);
}
}
- 浏览: 7757 次
- 性别:

- 来自: 河南 南阳

- 详细资料
搜索本博客
我的相册
共 1 张
最近加入圈子
最新评论
-
30岁前男人需要完成的事
-- by xinshoubobo -
程序员的爱情表白
兄弟,在这里忽悠没有前途的换换地儿比较合适
-- by trans -
程序员的爱情表白
lz太狠了
-- by shishna -
程序员的爱情表白
晕.... 楼主你实在太有才了..
-- by Wallian_hua -
程序员的爱情表白
》否则--我将释放掉所有系统资源.好可怜的一头狼
-- by ddd






评论排行榜