时间戳转为yyyy-MM-dd
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatDate = sdf.format(new Date(Long.parseLong(时间戳)));
-----------------------------------------------------------------------------------------------------------------------------
DateUtil.toDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(String.valueOf(objects[4])), DateUtil.DATETIME_PATTERN);
public class DateUtil {
public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_PATTERN = "yyyy-MM-dd";
public static final String MONTH_PATTERN = "yyyy-MM";
/**
* 转换为字符串.
*/
public static final String toString(Date date, String pattern) {
String result = null;
if (date != null) {
result = DateFormatUtils.format(date, pattern);
}
return result;
}
/**
* 转换为java.util.Date对象.
*
* @param value
* 带转换对象
* @param pattern
* 日期格式
* @return 对应的Date对象
*/
public static final Date toDate(Object value, String pattern) {
Date result = null;
if (value instanceof String) {
if (StringUtils.isNotEmpty((String) value)) {
try {
result = DateUtils.parseDate((String) value,
new String[] { pattern });
} catch (ParseException e) {
}
if (result == null && StringUtils.isNotEmpty((String) value)) {
result = new Date(new Long((String) value).longValue());
}
}
} else if (value instanceof Object[]) {
Object[] array = (Object[]) value;
if ((array != null) && (array.length >= 1)) {
value = array[0];
result = toDate(value, pattern);
}
} else if (Date.class.isAssignableFrom(value.getClass())) {
result = (Date) value;
}
return result;
}
/**
* 转换为java.util.Date对象.
*
* @param value
* 带转换对象
* @return 对应的Date对象
*/
public static final Date toDate(Object value) {
Date result = null;
if (value instanceof String) {
if (StringUtils.isNotEmpty((String) value)) {
try {
result = DateUtils.parseDate((String) value, new String[] {
DATE_PATTERN, DATETIME_PATTERN, MONTH_PATTERN });
} catch (ParseException e) {
}
if (result == null && StringUtils.isNotEmpty((String) value)) {
result = new Date(new Long((String) value).longValue());
}
}
} else if (value instanceof Object[]) {
Object[] array = (Object[]) value;
if ((array != null) && (array.length >= 1)) {
value = array[0];
result = toDate(value);
}
} else if (Date.class.isAssignableFrom(value.getClass())) {
result = (Date) value;
}
return result;
}
/**
* 获取<code>date</code>同一年的第一天.
*/
public static final Date getFirstDateOfYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_YEAR, 1);
return cal.getTime();
}
/**
* 获取<code>date</code>同一年的第后一天.
*/
public static final Date getLastDateOfYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getFirstDateOfYear(date));
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1);
cal.add(Calendar.DAY_OF_YEAR, -1);
return cal.getTime();
}
/**
* 给<code>date</code>追加<code>year</code>年.
*/
public static final Date addYear(Date date, int year) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, year);
return cal.getTime();
}
/**
* 获取<code>date</code>同月份的第一天.
*/
public static final Date getFirstDateOfMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
/**
* 获取<code>date</code>同月份的第后一天.
*/
public static final Date getLastDateOfMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getFirstDateOfMonth(date));
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
cal.add(Calendar.DAY_OF_YEAR, -1);
return cal.getTime();
}
/**
* 给<code>date</code>追加<code>month</code>月.
*/
public static final Date addMonth(Date date, int month) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, month);
return cal.getTime();
}
/**
* 获取<code>date</code>同周的星期一.
*/
public static final Date getFirstDateOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return cal.getTime();
}
/**
* 获取<code>date</code>同周的星期日.
*/
public static final Date getLastDateOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return cal.getTime();
}
/**
* 给<code>date</code>追加<code>week</code>周.
*/
public static final Date addWeek(Date date, int week) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.WEEK_OF_YEAR, week);
return cal.getTime();
}
/**
* 给<code>date</code>追加<code>day</code>天.
*/
public static final Date addDay(Date date, int day) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, day);
return c.getTime();
}
/**
* 给<code>date</code>追加<code>hour</code>小时.
*/
public static final Date addHour(Date date, int hour) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.HOUR_OF_DAY, hour);
return c.getTime();
}
/**
* 给<code>date</code>追加<code>minute</code>分钟.
*/
public static final Date addMinute(Date date, int minute) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MINUTE, minute);
return c.getTime();
}
public static final Date stringToDate(String dateStr, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
}
return date;
}
/**
* 获取<code>date</code>当月总共天数
*/
public static final int getDays(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 获取<code>date</code>当月年
*/
public static final int getYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
}
/**
* 获取<code>date</code>当前月
*/
public static final int getMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH)+1;
}
/**
* 获取<code>date</code>当前日
*/
public static final int getDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_MONTH);
}
/**
* 构造函数.
*/
private DateUtil() {
}
}