import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class DateUtil {
public static String getCurrentTime() {
SimpleDateFormat format = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
format.applyPattern("yyyyMMddHHmmss");
String timeStr = format.format(new Date());
return timeStr;
}
public static String getCurrentTimestamp() {
SimpleDateFormat format = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
format.applyPattern("yyyy-MM-dd HH:mm:ss");
String timeStr = format.format(new Date());
return timeStr;
}
/****
* 根据格式获取时间
*
* @param formatStr
* 如:“yyyy-MM-dd HH:mm:ss ”
* @return
*/
public static String getCurrentTimestamp(String formatStr) {
SimpleDateFormat format = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
format.applyPattern(formatStr);
String timeStr = format.format(new Date());
return timeStr;
}
/**
* 将Timestamp转换成指定的日期格式
*
* @param fomatStr
* 如(yyyy年MM月dd日 HH:mm)
* @param dateSource
* 源日期(Timestamp)
* @return
*/
public static String getTimeByFomat(String fomatStr, Timestamp dateSource) {
if (dateSource == null) {// 容错处理
return "";
}
SimpleDateFormat format = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
format.applyPattern(fomatStr);
return format.format(dateSource);
}
/**
* 将Date转换成指定的日期格式
*
* @param fomatStr
* 如(yyyy年MM月dd日 HH:mm)
* @param dateSource
* 源日期(Date)
* @return
*/
public static String getDateByFomat(String fomatStr, Date date) {
if (date == null) {// 容错处理
return "";
}
SimpleDateFormat format = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
format.applyPattern(fomatStr);
return format.format(date);
}
/**
* 获取当前时间
*
* @param date
* @return
*/
public static Date getCurrentDate() {
return new Date(System.currentTimeMillis());
}
/**
* 将时间置为23时59分钟59秒
*
* @param date
* @return
*/
public static Calendar getFullPassDay(Calendar original) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(original.getTimeInMillis());
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar;
}
/**
* 取给定时间对应的当天起始时间,即00:00:000
*
*/
public static Calendar getOriginOfDay(Calendar original) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(original.getTimeInMillis());
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar;
}
/**
* 将时间后退2小时
*
* @param date
* @return
*/
public static Date getFallBack2Hour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 2);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 将时间精确到小时
*
* @param date
* @return
*/
public static Date getTimeHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 获取两个时间间隔的天数
*
* @param date
* @return
*/
public static long getDiffDays(Date startDate, Date endDate) {
long difftime = endDate.getTime() - startDate.getTime();
return difftime / (24L * 60L * 60L * 1000L);
}
/**
* 根据日期获取当天起始时间
*
* @param date
* @return
*/
public static Date getStartDateOfCurrentDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 根据日期获取昨天起始时间
*
* @param date
* @return
*/
public static Date getStartYesterday(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 根据日期获取下一天起始时间
*
* @param date
* @return
*/
public static Date getStartDateOfNextDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 根据日期当前日期顺延一周后的起始时间
*
* @param date
* @return
*/
public static Date getStartDateOfNextSevenDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 7);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 根据日期当前日期顺延一周后的起始时间
*
* @param date
* @return
*/
public static Date getStartDateOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 根据日期当前日期顺延一月后的起始时间
*
* @param date
* @return
*/
public static Date getStartDateOfNextMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 封装一天只能的时间区域
*
* @param date
* @return
*/
public static List<Date> getStaticByDateDateArea(Date date) {
List<Date> dates = new ArrayList<Date>();
Date startdate = getStartDateOfCurrentDay(date);
Date nextday = getStartDateOfNextDay(date);
int step = 2;
dates.add(startdate);
for (int i = 1; i < 12; i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startdate);
calendar.add(Calendar.HOUR_OF_DAY, i * step);
dates.add(calendar.getTime());
}
dates.add(nextday);
return dates;
}
/**
* 封装一周之内时间区域
*
* @param date
* @return
*/
public static List<Date> getStaticByWeekDateArea(Date date) {
List<Date> dates = new ArrayList<Date>();
Date startdate = getStartDateOfCurrentDay(date);
Date nextday = getStartDateOfNextSevenDay(date);
dates.add(startdate);
for (int i = 1; i < 7; i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startdate);
calendar.add(Calendar.DAY_OF_MONTH, i);
dates.add(calendar.getTime());
}
dates.add(nextday);
return dates;
}
/**
* 封装一周之内时间区域List<String>
*
* @param date
* @return
*/
public static List<String> getStaticByWeekLabel(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
List<String> dates = new ArrayList<String>();
Date startdate = getStartDateOfCurrentDay(date);
dates.add(dateFormat.format(startdate));
for (int i = 1; i < 7; i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startdate);
calendar.add(Calendar.DAY_OF_MONTH, i);
dates.add(dateFormat.format(calendar.getTime()));
}
return dates;
}
/**
* 封装一月之内时间区域
*
* @param date
* @return
*/
public static List<Date> getStaticByMonthDateArea(Date date) {
List<Date> dates = new ArrayList<Date>();
Date startdate = getStartDateOfMonth(date);
Date nextday = getStartDateOfNextMonth(date);
long daydiff = getDiffDays(startdate, nextday);
dates.add(startdate);
for (int i = 1; i < daydiff; i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startdate);
calendar.add(Calendar.DAY_OF_MONTH, i);
dates.add(calendar.getTime());
}
dates.add(nextday);
return dates;
}
/**
* 封装一点时间之内的时间区域(天)
*
* @param startDate
* @param endDate
* @return
*/
public static List<Date> getStaticBySE(Date startDate, Date endDate) {
List<Date> dates = new ArrayList<Date>();
long daydiff = getDiffDays(startDate, endDate);
dates.add(startDate);
for (int i = 1; i < daydiff; i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
calendar.add(Calendar.DAY_OF_MONTH, i);
dates.add(calendar.getTime());
}
dates.add(endDate);
return dates;
}
/**
* 封装一月之内时间区域
*
* @param date
* @return
*/
public static List<String> getStaticByMonthLabel(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
List<String> dates = new ArrayList<String>();
Date startdate = getStartDateOfMonth(date);
Date nextday = getStartDateOfNextMonth(date);
long daydiff = getDiffDays(startdate, nextday);
dates.add(dateFormat.format(startdate));
for (int i = 1; i < daydiff; i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startdate);
calendar.add(Calendar.DAY_OF_MONTH, i);
dates.add(dateFormat.format(calendar.getTime()));
}
return dates;
}
/**
* 封装一月之内时间区域
*
* @param format
* @param date
* @return
*/
public static String formatDate(String format, Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.format(date);
}
/**
* 取得当月第一天
*
* @param date
* @return
*/
public static Calendar getFirstDayOfMonth(Calendar calendar) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(calendar.getTimeInMillis());
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal;
}
/**
* 取得当月最后一天
*
* @param date
* @return
*/
public static Calendar getLastDayOfMonth(Calendar calendar) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(calendar.getTimeInMillis());
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.roll(Calendar.DAY_OF_MONTH, -1);
return cal;
}
/**
* 取得下月第一天
*
* @param date
* @return
*/
public static Calendar getFirstDayOfNextMonth(Calendar calendar) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(calendar.getTimeInMillis());
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal;
}
/**
* 取得下月最后一天
*
* @param date
* @return
*/
public static Calendar getLastDayOfNextMonth(Calendar calendar) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(calendar.getTimeInMillis());
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.roll(Calendar.DAY_OF_MONTH, -1);
return cal;
}
}
网友评论