package com.dsb.tool;
public class Test {
/**
* @param args
*/
public int year = 0;
public int month = 0;
public int day = 0;
public boolean flag = false;
public static void main(String[] args) {
System.out.println("前一天:" + new Test().getLastDay("2013/03/01"));
}
public String getLastDay(String datestr) {
String[] date = datestr.split("/");
String dateString = null;
year = Integer.parseInt(date[0]);
month = Integer.parseInt(date[1]);
day = Integer.parseInt(date[2]);
day = day - 1;
if (day == 0) {
month = month - 1;
if (month == 0) {
year = year - 1;
}
}
if (month == 0) {
month = 12;
}
if (day == 0) {
day = getDayByMonth(year, month);
}
dateString = year + "/" + month + "/" + day;
return dateString;
}
public boolean isLeapYear(int year) {
flag = false;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
flag = true;
}
return flag;
}
public int getDayByMonth(int year, int month) {
int day = 0;
int[] daybymonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2) {
if (isLeapYear(year)) {
day = 29;
} else {
day = 28;
}
} else {
day = daybymonth[month - 1];
}
return day;
}
}
日期一般用 java.util.Date 类的对象来表示,里面有getTime方法可以获得一个相对于GMT 1970年1月1日00:00:00的毫秒数(就是一个表示时间的毫秒数)。
我们都知道,一天有 24*60*60*1000 毫秒,所以,把时间向前推移一天,其实很简单,
就是把那个长整形的时间(毫秒数),减去一天所用的毫秒数,得到的结果,就是前一天的毫秒数
Date now = new Date();
long mili = now.getTime() - 24*60*60*1000;
Date result = new Date(mili);