一、计算当前时间相差天数之后的时间
/**
*
* <p>Title: calculateDay</p>
* <p>Description: </p>
* @param date 前一次的时间
* @param days 需要减去的天数
*/
private static String calculateDay(Date date,int days){
SimpleDateFormat sj = new SimpleDateFormat("yyyy-MM-dd");
String time = null;
String today = DateUtil.getStrDate(date, DateUtil.DEFAULT_DATE_FORMAT);
Date d;
try {
Calendar calendar = Calendar.getInstance();
calendar.add(calendar.DATE, days);
time = sj.format(calendar.getTime());
System.out.println("前天:" + sj.format(calendar.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return time ;
}
二、天数转化时间
public static String minusDate(Date date, long day) throws ParseException {
long time = date.getTime(); // 得到指定日期的毫秒数
day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
time += day; // 相减得到新的毫秒数
// time -= day; // 相减得到新的毫秒数
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String newDate = dateFormat.format(new Date(time));
return newDate; // 将毫秒数转换成日期
}
三、封装的时间类
if (SERVER_TIME_ZONE < 1) {
TimeZone zone = TimeZone.getDefault();
SERVER_TIME_ZONE = zone.getRawOffset();
}
}
public static String getDate(Date date) {
if (date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DEFAULT_DATE_FORMAT);
return sdf.format(date);
}
public static String getStrDate(Date date, String formate) {
if (date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(formate);
return sdf.format(date);
}
public static String getStrDate(long time, String formate) {
Date date = new Date(time);
return getStrDate(date, formate);
}
public static String getDateNum() {
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_NUM_FORMAT);
return sdf.format(new Date());
}
public static String getCurrentDate() {
Date logonDate = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DEFAULT_DATE_FORMAT);
return sdf.format(logonDate);
}
public static String getDateTime(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DEFAULT_DATETIME_FORMAT);
return sdf.format(date);
}
public static String getTime(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DEFAULT_TIME_FORMAT);
return sdf.format(date);
}
public static Date getDateFromStr(String dateTime, String formart) {
if (formart == null) {
formart = DEFAULT_DATETIME_FORMAT;
}
if (dateTime == null || "".equals(dateTime.trim())) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(formart);
try {
return sdf.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取 years年前的日期
*
* @param years
* @return
*/
public static Date getDateByPreYears(int years) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -(years + 1));
return cal.getTime();
}
/**
* 获取两个时间的年限差
*
* @param preDate
* @param afterDate
* @return
*/
public static int getYearsByTowDate(Date preDate, Date afterDate) {
if (afterDate == null) {
afterDate = new Date();
}
int years = Math.abs(afterDate.getYear() - preDate.getYear());
years = years > 0 ? years - 1 : 0;
return years;
}
/**
* 获取显示的星期
*
* @param date
* @return
*/
public static String getWeekDaysByDate(Date date) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int weekDays = cal.get(Calendar.DAY_OF_WEEK) - 1;
return dayNames[weekDays];
}
/**
* 获取星期的值(0,1,2,3,4,5,6)
*
* @param date
* @return
*/
public static int getWeekDaysValueByDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int weekDays = cal.get(Calendar.DAY_OF_WEEK) - 1;
return weekDays;
}
/**
* 获取显示日期
*
* @param distance
* @return
*/
public static String getShowDateByDis(int distance) {
final String dayNames[] = { "今天", "明天", "后天" };
if (distance < dayNames.length) {
return dayNames[distance];
} else {
return null;
}
}
/**
* 获取当前preWeeks个星期的开始时间(星期天)
*
* @param preWeeks
* @return
*/
public static Date getBeginWeeksDate(Integer preWeeks) {
Date d = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(d);
int weekDays = cal.get(Calendar.DAY_OF_WEEK) - 1;
cal.add(Calendar.WEDNESDAY, -preWeeks);
cal.add(Calendar.DATE, -weekDays);
return cal.getTime();
}
/**
* 获取当前星期的星期(days) 的日期
*
* @param days
* @return
*/
public static Date getNowWeeksDate(Integer days, int weeks) {
Date d = new Date();
Integer nowDays = getWeekDaysValueByDate(d);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.WEDNESDAY, weeks);
cal.add(Calendar.DATE, days - nowDays);
return cal.getTime();
}
/**
* 获取当前preWeeks个星期的结束时间(星期六)
*
* @param preWeeks
* @return
*/
public static Date getEndWeeksDate(Integer preWeeks) {
Date d = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(d);
int weekDays = cal.get(Calendar.DAY_OF_WEEK) - 1;
cal.add(Calendar.WEDNESDAY, -preWeeks);
cal.add(Calendar.DATE, 6 - weekDays);
return cal.getTime();
}
// public static String getArea(Integer areaCode) {
// if (areaCode == 0) {
// return "大陆";
// } else if (areaCode == 1) {
// return "港台";
// } else if (areaCode == 2) {
// return "欧美";
// } else {
// return "日韩";
// }
// }
public static int houseStrToMin(String house) {
if (house == null || "".equals(house.trim())) {
return 0;
}
String[] houseArray = house.split(":");
if (houseArray.length != 3) {
return 0;
}
try {
int sum = Integer.parseInt(houseArray[0]) * 60 + Integer.parseInt(houseArray[1]) + Integer.parseInt(houseArray[1]) / 60;
return sum;
} catch (NumberFormatException e) {
return 0;
}
}
public static void main(String[] args) {
Date date = DateUtil.getNowWeeksDate(0, 1);
System.out.println(DateUtil.getStrDate(date, DEFAULT_DATE_FORMAT));
}
}