import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
public class DateUtils {
public static final Integer ONE_MINTUE = 60;
public static final Integer ONE_HOUR = 60 * 60;
public static final Integer ONE_DAY = 60 * 60 * 24;
public static final Integer ONE_MONTH = 60 * 60 * 24 * 30;
public enum DateFormat {
SHORT("yy-MM-dd"), DATE("yyyy-MM-dd"), SIMPLE("yyyyMMdd"), FULL(
"yyyy-MM-dd HH:mm:ss"), YMDHM("yyyyMMddHHmm"), MINITUE(
"yyyyMMdd HHmm"), FULL_SECOND("yyyyMMddHHmmss"), YEAR_MONTH(
"yyyyMM"),MONTH("MM"),MONTH_S("M"),MINITUE_SECOND("HHmm"),DATE2("MM/dd/yyyy"),DATE3("yyyy-MM-dd HH:mm"),TIME("HH:mm:ss");
private String format;
private ThreadLocal<SimpleDateFormat> convert;
DateFormat(final String format) {
this.format = format;
convert = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
simpleDateFormat.setLenient(false);
return simpleDateFormat;
}
};
}
public String getFormat() {
return format;
}
public ThreadLocal<SimpleDateFormat> getConvert() {
return convert;
}
}
public static Integer retrivePreDate(String date) {
Calendar cal = new GregorianCalendar();
SimpleDateFormat oSdf = new SimpleDateFormat("", Locale.ENGLISH);
oSdf.applyPattern("yyyyMM");
try {
cal.setTime(oSdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
cal.add(Calendar.MONTH, -1);
return Integer.parseInt(DateFormat.YEAR_MONTH.getConvert().get().format(cal.getTime()));
}
public static Integer cauculateActualDay(String date) {
Calendar cal = new GregorianCalendar();
SimpleDateFormat oSdf = new SimpleDateFormat("", Locale.ENGLISH);
oSdf.applyPattern("yyyyMM");
try {
cal.setTime(oSdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
public static Long distinctDays(String fromDay, String toDay) {
SimpleDateFormat df = DateFormat.DATE.getConvert().get();
try {
long to = df.parse(toDay).getTime();
long from = df.parse(fromDay).getTime();
return ((to - from) / (1000 * 60 * 60 * 24));
} catch (ParseException e) {
e.printStackTrace();
}
return -1L;
}
public static Boolean verifyDateFormat(String date, DateFormat format) {
try {
format.getConvert().get().parse(date);
return Boolean.TRUE;
} catch (ParseException e) {
return Boolean.FALSE;
}
}
public static String convertDateFormat(Date date, DateFormat format) {
return format.getConvert().get().format(date);
}
public static Date convertDateText(String date, DateFormat format) {
try {
return format.getConvert().get().parse(date);
} catch (ParseException e) {
return null;
}
}
public static Long retriveTodayStartTime() {
Calendar currentDate = new GregorianCalendar();
currentDate.set(Calendar.HOUR_OF_DAY, 0);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 0);
return currentDate.getTime().getTime() / 1000 * 1000;
}
public static Date getNextDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
Date date1 = new Date(calendar.getTimeInMillis());
return date1;
}
public static Long convertSimpleDateToLong(String date) {
try {
return DateFormat.SIMPLE.getConvert().get().parse(date).getTime();
} catch (Exception e) {
return -1L;
}
}
public static Long convertDateToLong(String date) {
try {
return DateFormat.DATE.getConvert().get().parse(date).getTime();
} catch (Exception e) {
return -1L;
}
}
public static Long convertDateToLong(String date, DateFormat format) {
try {
return format.getConvert().get().parse(date).getTime();
} catch (Exception e) {
return -1L;
}
}
public static Date convertDateFormatFromDate(Date date, DateFormat format) {
String date1 = convertDateFormat(date, format);
return convertDateText(date1, format);
}
public static String convertDateTextFromString(String date, DateFormat format,DateFormat format2) {
Date date1 = convertDateText(date, format);
if (date1 == null) {
return null;
}
return convertDateFormat(date1, format2);
}
public static void main(String[] args) throws ParseException {
String cmmTime = DateUtils.convertDateFormat(new Date(), DateFormat.SHORT);
System.out.println("cmmTime:" + cmmTime);
System.out.println(convertDateToLong(cmmTime, DateFormat.SHORT));
Date date = DateUtils.convertDateText("2016-03-22", DateFormat.DATE);
System.out.println(DateDiff(new Date(),date));
}
public static Date caculateDateByDay(Date date,Integer value){
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, value);
return cal.getTime();
}
public static String caculateDateByDayForString(String dateStr,Integer value,DateFormat dateFormat){
Date date = DateUtils.convertDateText(dateStr, dateFormat);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, value);
return DateUtils.convertDateFormat(cal.getTime(), dateFormat);
}
public static String getMonthOfDate(Date date){
String month = DateUtils.convertDateFormat(date, DateFormat.MONTH_S);
return month;
}
public static int getAgeMonth(Date date){
int result = -1;
if(date==null) return -1;
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
int year1 = cal.get(Calendar.YEAR);
int month1 = cal.get(Calendar.MONTH);
cal.setTime(date);
int year2 = cal.get(Calendar.YEAR);
int month2 = cal.get(Calendar.MONTH);
if(year1>year2){
result = (year1-year2)*12 +month1-month2;
}else if(year1<year2){
result = (year2-year1)*12 +month2-month1;
}else{
result = month1-month2;
}
return result;
}
public static int getAge(Date date){
int result = -1;
if(date==null) return -1;
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
int year1 = cal.get(Calendar.YEAR);
cal.setTime(date);
int year2 = cal.get(Calendar.YEAR);
return year1-year2;
}
public static long DateDiff(Date date1 ,Date date2){
long result = 0;
if(date1==null||date2==null) return result;
long timeMills = date1.getTime()-date2.getTime();
result = timeMills/(3600*24*1000);
return result;
}
public static boolean validDateFormat(String str,DateFormat dateFormat){
try{
return str.equals(DateUtils.convertDateTextFromString(str, dateFormat, dateFormat));
}catch(Exception e){
return false;
}
}
public static int getTodayRestSecond(){
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
int second = DateUtils.subSecond(cal.getTime(), new Date());
if(second <= 0){
second = 0;
}
return second;
}
public static int subSecond(Date date1, Date date2) {
long d1 = date1.getTime();
long d2 = date2.getTime();
int sub = (int) ((d1 - d2) / 1000);
return sub;
}
public static Date getDayAfter(Date date,Integer day){
return new Date(day * 24 * 60 * 60 * 1000+ date.getTime());
}
public static Date getDayBefore(Date date,Integer day){
return new Date(date.getTime() - (24L * 60 * 60 * 1000 * day));
}
public static Date getHourBefore(Date date,Integer hour){
return new Date(date.getTime() - (60 * 60 * 1000 * hour));
}
}