package com.hh.ota.utils;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
public final static String DATE_PATTERN = "yyyy-MM-dd";
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
public final static String DATE_TIME_MIN_PATTERN = "yyyy-MM-dd HH:mm";
public final static String DATE_TIME_SI_PATTERN = "yyyyMMdd HHmmss";
public static Long expriredDate(){
Long time = null;
try {
Date date = new Date();
date.setTime(date.getTime()+10*60*1000);
return date.getTime();
} catch (Exception e) {
e.printStackTrace();
}
return time;
}
public static String format(String timeStr) {
SimpleDateFormat sdf=new SimpleDateFormat(DATE_TIME_MIN_PATTERN);
String sd = sdf.format(new Date(Long.parseLong(timeStr+"000")));
return sd;
}
public static String format(Date date, String pattern) {
if(date != null){
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
return null;
}
public static Date stringToDate(String strDate, String pattern) {
if (StringUtils.isBlank(strDate)){
return null;
}
DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
return fmt.parseLocalDateTime(strDate).toDate();
}
public static Date[] getWeekStartAndEnd(int week) {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime.plusWeeks(week));
date = date.dayOfWeek().withMinimumValue();
Date beginDate = date.toDate();
Date endDate = date.plusDays(6).toDate();
return new Date[]{beginDate, endDate};
}
public static Date addDateSeconds(Date date, int seconds) {
DateTime dateTime = new DateTime(date);
return dateTime.plusSeconds(seconds).toDate();
}
public static Date addDateMinutes(Date date, int minutes) {
DateTime dateTime = new DateTime(date);
return dateTime.plusMinutes(minutes).toDate();
}
public static Date addDateHours(Date date, int hours) {
DateTime dateTime = new DateTime(date);
return dateTime.plusHours(hours).toDate();
}
public static Date addDateDays(Date date, int days) {
DateTime dateTime = new DateTime(date);
return dateTime.plusDays(days).toDate();
}
public static Date addDateWeeks(Date date, int weeks) {
DateTime dateTime = new DateTime(date);
return dateTime.plusWeeks(weeks).toDate();
}
public static Date addDateMonths(Date date, int months) {
DateTime dateTime = new DateTime(date);
return dateTime.plusMonths(months).toDate();
}
public static Date addDateYears(Date date, int years) {
DateTime dateTime = new DateTime(date);
return dateTime.plusYears(years).toDate();
}
public static Date dateAddHours(Date startDate, int hours) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours);
return c.getTime();
}
public static Date dateAddMinutes(Date startDate, int minutes) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
return c.getTime();
}
public static Date dateAddSeconds(Date startDate, int seconds) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds);
return c.getTime();
}
public static Date dateAddDays(Date startDate, int days) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
return c.getTime();
}
public static Date dateParse(String dateTimeString, String pattern) throws ParseException {
if (StringUtils.isBlank(pattern)) {
pattern = DateUtils.DATE_PATTERN;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(dateTimeString);
}
public static Date getTheTimeInMinutes() throws ParseException {
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = new Date();
String timeStr = time.format(date);
return time.parse(timeStr);
}
public static String getDateTime() {
return format(new Date(), DATE_TIME_PATTERN);
}
}