获取时间
/**
*
* @Description:获取当前时间,格式yyyy-MM-dd HH:mm:ss
* @param type
* (0,当前时间,1秒,2分,3小时,4天,5月,6年)
* @param time
* (当前时间前后时间点)
* @return
* @version:v1
* @date:2017年10月24日 下午3:14:18
*/
public static String getStringDate(String type, int time) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
Calendar c = new GregorianCalendar();
c.setTime(date);
if ("1".equals(type)) {
c.add(Calendar.SECOND, time);
} else if ("2".equals(type)) {
c.add(Calendar.MINUTE, time);
} else if ("3".equals(type)) {
c.add(Calendar.HOUR, time);
} else if ("4".equals(type)) {
c.add(Calendar.DATE, time);
} else if ("5".equals(type)) {
c.add(Calendar.MONTH, time);
} else if ("6".equals(type)) {
c.add(Calendar.YEAR, time);
}
date = c.getTime();
String dateString = df.format(date);
return dateString;
}
判断是否润年
public static boolean isLeapYear(String ddate) {
/**
* 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
* 3.能被4整除同时能被100整除则不是闰年
*/
Date d = strToDateLong1(ddate);
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(d);
int year = gc.get(Calendar.YEAR);
if ((year % 400) == 0)
return true;
else if ((year % 4) == 0) {
if ((year % 100) == 0)
return false;
else
return true;
} else
return false;
}
获取两个时间内所有月份
public static List<String> getMonthBetween(String minDate, String maxDate)
throws ParseException {
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M");// 格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(minDate));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(maxDate));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = max;
while (curr.after(min)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, -1);
}
return result;
}
获取指定的前一天日期
public static String getSpecifiedDayBefore(String specifiedDay)
throws ParseException {
Calendar c = Calendar.getInstance();
Date date = new SimpleDateFormat("yy/MM/dd").parse(specifiedDay);
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - 1);
String dayBefore = new SimpleDateFormat("yyyy/MM/dd").format(c
.getTime());
return dayBefore;
}
两个日期之间的天数
public static int daysBetween(String smdate, String bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}