package com.baichang.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public final class DateUtil {
/**
* default date format pattern
*/
public final static String DATE_FORMAT = "yyyy-MM-dd";
public final static String MONTH_DATE_FORMAT = "MM-dd";
public final static String YEAR_WEEK_FORMAT = "yyyy-ww";
public final static String YEAR_MONTH_FORMAT = "yyyy-MM";
public final static String YEAR_WEEK_FORMAT_SHORT = "yy-ww";
public final static String YEAR_MONTH_FORMAT_SHORT = "yy-MM";
public final static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";
public final static String FULL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public final static String TIME_FORMAT = "HH:mm";
public final static String MONTH_DAY_HOUR_MINUTE_FORMAT = "MM-dd HH:mm";
public final static String LOCATE_DATE_FORMAT = "yyyyMMddHHmmss";
private static final int DAYS_OF_A_WEEK = 7;
private DateUtil() {
}
/**
* parse date with the default pattern
*
* @param date string date
* @return the parsed date
*/
public static Date parseDate(String date) {
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
try {
return format.parse(date);
} catch (Exception e) {
return null;
}
}
public static Date getTodayStartDate() {
return parseDate(getNowDate() + " 00:00:00",
"yyyy-MM-dd HH:mm:ss");
}
public static Date parseDate(String date, String pattern) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
return format.parse(date);
} catch (ParseException e) {
return new Date();
}
}
/**
* 获取增加小时后的 Date
*
* @param date
* @param i
* @return squall add 20100225
*/
public static Date addHour(Date date, int i) {
Calendar calendar = getDefaultCalendar();
calendar.setTime(date);
calendar.add(Calendar.HOUR, i);
return calendar.getTime();
}
/**
* format date with the default pattern
*
* @param date the date that want to format to string
* @return the formated date
*/
public static String formatDate(Date date) {
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
return format.format(date);
}
public static String formatTime(Date date) {
SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT);
return format.format(date);
}
/**
* format date with the given pattern
*
* @param date the date that want to format to string
* @param pattern the formated pattern
* @return the formated date
*/
public static String formatDate(Date date, String pattern) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(date);
}
/**
* get current date
*
* @return the string of current date
*/
public static String getCurrentDateFormat() {
return formatDate(new Date());
}
public static String getCurrentDateStr() {
return formatDate(new Date(),"yyyyMMdd");
}
public static String getCurrentTimeFormat() {
return formatTime(new Date());
}
/**
* get number of days between the two given date
*
* @param fromDate the begin date
* @param endDate the end date
* @return the number of days between the two date
*/
public static int getDateNum(Date fromDate, Date endDate) {
long days = (endDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24);
return (int) days;
}
/**
* add day to the date
*
* @param date the added date
* @param number the number to add to the date
* @return the added date
*/
public static Date addDate(Date date, int number) {
Calendar calendar = getDefaultCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE, number);
return calendar.getTime();
}
public static Date addMonth(Date date, int number) {
Calendar calendar = getDefaultCalendar();
calendar.setTime(date);
calendar.add(Calendar.MONTH, number);
return calendar.getTime();
}
public static Date addYear(Date date, int number) {
Calendar calendar = getDefaultCalendar();
calendar.setTime(date);
calendar.add(Calendar.YEAR, number);
return calendar.getTime();
}
/**
* get the default calendar
*
* @return the calendar instance
*/
public static Calendar getDefaultCalendar() {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
return calendar;
}
/**
* format the date into string value
*
* @param calendar the formated calendar
* @return the string date
*/
public static String getStringDate(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
return year + "-" + getNiceString(month) + "-" + getNiceString(day);
}
/**
* according to the pattern yyyy-MM-dd
*
* @param value the value
* @return the formated value
*/
public static String getNiceString(int value) {
String str = "00" + value;
return str.substring(str.length() - 2, str.length());
}
/**
* get calendar from date
*
* @param date the passing date
* @return the calendar instance
*/
public static Calendar getCalendarFromDate(Date date) {
Calendar calendar = getDefaultCalendar();
calendar.setTime(date);
return calendar;
}
public static String getInterval(Date startDate, Date endDate) {
long intervalTime = endDate.getTime() - startDate.getTime();
return getInterval(intervalTime);
}
public static int getIntervalMinute(Date startDate, Date endDate) {
long intervalTime = endDate.getTime() - startDate.getTime();
return (int) (intervalTime / (1000 * 60));
}
public static String getInterval(long intervalTime) {
int hour = (int) (intervalTime / (1000 * 60 * 60));
int minute = (int) (intervalTime / (1000 * 60) - hour * 60);
int second = (int) ((intervalTime / 1000) - hour * 60 * 60 - minute * 60);
if (hour > 0) {
return hour + "小时 " + minute + "分 " + second + "秒";
} else if (minute > 0) {
return minute + "分钟 " + second + "秒";
} else {
return second + "秒";
}
}
public static int getYear(Date date) {
Calendar calendar = getCalendarFromDate(date);
return calendar.get(Calendar.YEAR);
}
public static int getMonth(Date date) {
Calendar calendar = getCalendarFromDate(date);
return calendar.get(Calendar.MONTH) + 1;
}
public static int getDayOfMonth(Date date) {
Calendar calendar = getCalendarFromDate(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
public static int getHour(Date now) {
Calendar calendar = getCalendarFromDate(now);
return calendar.get(Calendar.HOUR_OF_DAY);
}
public static int getWeekOfYear(Date date) {
Calendar calendar = getCalendarFromDate(date);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
return calendar.get(Calendar.WEEK_OF_YEAR) - 1;
}
public static Date getCurrentDate() {
Calendar calendar = getCalenda
- 1
- 2
前往页