Skip to content

Instantly share code, notes, and snippets.

@keating
Created June 1, 2012 08:26
Show Gist options
  • Save keating/2850312 to your computer and use it in GitHub Desktop.
Save keating/2850312 to your computer and use it in GitHub Desktop.
some helpers for java.util.Date
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* some helpers for java.util.Date
* @author keating_andy_given
*
*/
public class DateHelp {
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
/**
* 获取某日期后之后几天的日期
* @param someDate 某日期
* @param daysAfterSomeDate 天数
*/
public static Date dateAfterSomeDay(Date someDate, int daysAfterSomeDate){
long time=(someDate.getTime()/1000)+60L*60*24*daysAfterSomeDate;
Date newDate = new Date();
newDate.setTime(time*1000);
return newDate;
}
/**
* 日期明天,省略时间
*/
public static Date tomorrowWithoutTime(Date date) {
return dateWithoutTime(dateAfterSomeDay(date, 1));
}
/**
* 获取一个与时间无关的日期,即时间为零点的日期
*/
public static Date dateWithoutTime(Date date) {
try {
return DateFormat.getDateInstance().parse(
DateFormat.getDateInstance().format(date));
} catch (ParseException e) {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment