package com.picooc.promoting.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import com.xuggle.xuggler.IContainer;
public class DateUtils extends org.apache.commons.lang.time.DateUtils {
private final static Logger LOG = Logger.getLogger(DateUtils.class);
// 一周的毫秒数
public final static long WEEK_MILLIS = 7 * 86400000;
// 一天的毫秒数86400000
public final static long DAY_MILLIS = 86400000;// day
public final static long hour_MILLIS = 3600000;// hour
public final static long minu_MILLIS = 60000;// minu
private static final String[] PATTERNS = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "yyyyMMdd" };
/**
* 预定义的日期格式:yyyy-MM-dd HH:mm:ss
*/
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/**
* 预定义的日期格式:yyyy-MM-dd HH:mm
*/
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
/**
* 预定义的日期格式:yyyy年MM月dd日
*/
public static final String YYYYNMMYDDR = "yyyy年MM月dd日";
/**
* 预定义的日期格式:MM月dd日
*/
public static final String MMYDDR = "MM月dd日";
/**
* 预定义的日期格式:yyyyMMdd_HHmmss
*/
public static final String YYYYMMDD_HHMMSS = "yyyyMMdd_HHmmss";
/**
* 预定义的日期格式:yyyyMMddHHmmss
*/
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
/**
* 预定义的日期格式:yyyy-MM-dd HH:mm
*/
public static final String YYYYMMDD_HHMM = "yyyyMMdd HHmm";
/**
* 预定义的日期格式:yyyy-MM-dd
*/
public static final String YYYY_MM_DD = "yyyy-MM-dd";
/**
* 预定义的日期格式:yyyy-MM
*/
public static final String YYYY_MM = "yyyy-MM";
/**
* 预定义的日期格式:yyyyMMddHH
*/
public static final String YYYYMMDDHH = "yyyyMMddHH";
/**
* 预定义的日期格式:yyyyMMdd
*/
public static final String YYYYMMDD = "yyyyMMdd";
/**
* 预定义的日期格式:yyyy.MM.dd
*/
public static final String YYYYMMDDR = "yyyy.MM.dd";
/**
* 预定义的日期格式:yyMMdd
*/
public static final String YYMMDD = "yyMMdd";
/**
* 预定义的日期格式:yyyyMM
*/
public static final String YYYYMM = "yyyyMM";
/**
* 预定义的日期格式:yyyy
*/
public static final String YYYY = "yyyy";
/**
* 预定义的日期格式:MM-dd
*/
public static final String MM_dd = "MM-dd";
/**
* 预定义的日期格式:MM.dd
*/
public static final String MMPdd = "MM.dd";
/**
* 预定义的日期格式:MMdd
*/
public static final String MMdd = "MMdd";
/**
* 预定义的日期格式:dd
*/
public static final String dd = "dd";
/**
* 预定义的日期格式:MM
*/
public static final String MM = "MM";
/**
* 预定义的日期格式:MM月dd日 HH:mm
*/
public static final String MMYDDR_HHMM = "MM月dd日 HH:mm";
/**
* 预定义的日期格式:yyyy年MM月dd日 HH:mm:ss
*/
public static final String YYYYNMMYDDR_HHMMSS = "yyyy年MM月dd日 HH:mm:ss";
/**
* 预定义的日期格式:HH:mm
*/
public static final String HH_mm = "HH:mm";
/**
* 预定义的日期格式:HH:mm:ss
*/
public static final String HH_mm_ss = "HH:mm:ss";
/**
* 预定义的日期格式:HHmm
*/
public static final String HHmm = "HHmm";
/**
* 预定义的日期格式:MM-dd HH:mm 月日时分
*/
public static final String MM_dd_HH_mm = "MM-dd HH:mm";
/**
* 预定义的日期格式:HHmmssSSS 时分秒毫秒
*/
public static final String YYYYMMDD_HHMMSSSSS = "yyyyMMdd_HHmmssSSS";
static public String formatDate(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
/**
* 讲一个时间戳转换秤某个格式的时间
* @param l
* @param format
* @return
*/
public static String changeTimeStampToFormatTime(long l, String format) {
Date date = new Date(l);
SimpleDateFormat f = new SimpleDateFormat(format);
return f.format(date);
}
/**
* 将一个时间戳转换成活跃时段需要的数值的方式,比如,将12:20转换为12.333333
* @param time
* @return
*/
public static float formatTimestampToFloat(long time){
String s = changeTimeStampToFormatTime(time, "HH:mm");
String[] ss = s.split(":");
return Integer.parseInt(ss[0]) + Integer.parseInt(ss[1]) / 60.0f;
}
static public Date parseDate(String date, String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(date);
}catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static long getHowManyDaysBetweenDates(Date newDate, Date oldDate) {
long newTime = newDate.getTime();
long oldTime = oldDate.getTime();
long days = (newTime / 1000 - oldTime / 1000) / (DAY_MILLIS / 1000);
// 如果days为0,代表是同一天,如果days大于0,代表newTime是oldTime之后的时间,如果days小于0,代表newTime是oldTime之前的时间
return days;
}
/**
* 判断两个时间是否跨年
* @param startDate
* @param endDate
* @return
*/
public static boolean isExceedYear(Date startDate, Date endDate) {
boolean result = false;
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
int startYear = calendar.get(Calendar.YEAR);
calendar.setTime(endDate);
int endYear = calendar.get(Calendar.YEAR);
if (startYear != endYear){
result = true;
}
return result;
}
/**
* 本年显示 月-日, 非本年显示年-月-日
*
* @param date
* @return
*/
public static String getFormatDate(Date date) {
String result = "";
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
String valueOf = formatDate(date, "yyyyMMdd");
boolean startsWith = valueOf.startsWith(String.valueOf(year));
if (startsWith) {
result = formatDate(date, "M月d日");
} else {
result = formatDate(date, "yyyy年M月d日");
}
return result;
}
public static String getFormatDateTwo(Date date) {
String result = "";
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
String valueOf = formatDate(date, "yyyyMMdd");
boolean startsWith = valueOf.startsWith(String.valueOf(year));
if (startsWith) {
result = formatDate(date, "MM月dd日");
} else {
result = formatDate(date, "yyyy年MM月dd日");
}
return result;
}
/**
* 生日转换为年龄
*
* @param a
* @return
* @throws Exception
*/
public static int getAge(String bir, String format) {
Calendar cal = Calendar.getInstance();
java.text.DateFormat df = new SimpleDateFormat(format);
Date a = null;
try {
a = df.parse(bir);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
if (cal.before(a)) {
throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
}
// cal.setTimeInMillis(System.currentTimeMillis());
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH) + 1;
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(a);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
// monthNow==monthBirth
if (dayOfMonthNow < dayOfMonthBirth) {
age--;
}
} else {
// monthNow>monthBirth
age--;
}
}
return age;
}
/*
* 获取某时间段内的年龄,精确到月份
*
* @param now
* @param birth
* @return
*/
@SuppressWarnings("deprecation")
public static int getAgeM(Date now, Date birth) {
int yearNow = now.getYear();
int monthNow = now.getMonth();
int yearBirth = birth.getYear();
int monthBirth = birth.getMonth();
int age = yearNow - yearBirth;
if (monthNow < monthBirth) {
age--;
}
return age;
}
// 根据一个时间戳,得到这个时间戳所处天的起始时间和结束时间
public static long[] getDayStartTimeAndEndTimeByTimestamp(long timeStamp) {
long time[] = new long[2];
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
time[0] = calendar.getTimeInMillis();
time[1] = time[0] + DAY_MILLIS - 1000;
return time;
}
/**
* 取得前后day天数的日期,day为负数表示以前的日期
* @param date
* @param day
* @return
*/
public static Date nextDate(Date date, int day) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
return calendar.getTime();
}
/**
* 间隔时间后的时间戳,day负数表示以前
*
* @param t
* @param day
* @return
*/
public static Timestamp nextTimestamp(Timestamp t, int day) {
return nextTimestamp(t, day, 0, 0, 0);
}
/**
* 间隔时间后的时间戳,day、hour负数表示以前
*
* @param t
* @param day
* @param hour
* @return
*/
public static Timestamp nextTimestamp(Timestamp t, int day, int hour) {
return nextTimestamp(t, day, hour, 0, 0);
}
/**
* 间隔时间后的时间戳
*
* @param t
* @param day
* @param hour
* @return
*/
public static Timestamp nextTimestamp(Timestamp t, int day, int hour, int minute, int second) {
if (t == null) {
return null;
}
return new Timestamp(t.getTime() + (day * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 + second) * 1000L);
}
/**
* 获得两个时间戳之间的时间差
* @param timestamp1
* @param timestamp2
* @return
*/
public static long getTimeDiffBetweenTimestamps(Timestamp timestamp1, Timestamp timestamp2){
return timestamp1.getTime() - timestamp2.getTime();
}
/**
* 计算两个日期天数差,如果date2 > date1 返回正数,否则返回负数
* <p>仅天数差,不考虑时分秒,将日期转换成当天的00:00:00后求天数差
*
* @param date1
* @param date2
* @return
*/
public static long dayDiff(Date date1, Date date2) {
Calendar calendar = Calendar.getInstance();
calendar.set(date1.getYear() + 1900, date1.getMonth(), date1.getDate(), 0, 0, 0);
date1 = calendar.getTime();
calendar.set(date2.getYear() + 1900, date2.getMonth(), date2.getDate(), 0, 0, 0);
date2 = calendar.getTime();
return (date2.getTime() - date1.getTime()) / 86400000;
}
/**
* 日期是否在某日期范围内
* @param date
* @param beginDate
* @param endDate
* @return
*/
public static boolean inDateRange(Date date, Date beginDate, Date endDate){
long time = date.getTime();
long beginTime = beginDate.getTime();
long endTime = endDate.getTime();
return (time >= beginTime && time <= endTime);
}
/**
* 获得年龄
*
* @param birthday
* @return
*/
public static int getAgeByBirthday(Date birthday) {
Date now = new Date();
int age = now.getYear() - birthday.getYear() - 1;
if (now.getMonth() > birthday.getMonth()) {
age++;
} else if (now.getMonth() == birthday.getMonth()) {
if (now.getDate() >= birthday.getDate())
age++;
}
return age;
}
/**
* 获得称重时年龄
*
* @param birthday
* @param weightDate
* @return
*/
public static int getAgeByBirthday(Date birthday, Date weightDate) {
int age = weightDate.getYear() - birthday.getYear() - 1;
if (weightDate.getMonth() > birthday.getMonth()) {
age++;
} else if (weightDate.getMonth() == birthday.getMonth()) {
if (weightDate.getDate() >= birthday.getDate())
age++;
}
return age;
}
/**
* 获取时段
*
* @param hour
* @return
*/
public static int getTimePeriod(int hour) {
int period = -1;
if (hour >= 20 && hour <= 23) {
period = 4;
} else if (hour >= 16 && hour <= 19) {
period = 3;
} else if (hour >= 12 && hour <= 15) {
period = 2;
} else if (hour >= 4 && hour <= 11) {
period = 1;
} else {
period = 0;
}
return period;
}
/**
* 获取时段(旧版本)
*
* @param hour
* @return
*/
public static int getTimePeriodForOldVersion(int hour) {
int period = -1;
if (hour >= 20 && hour <= 23) {
period = 4;
} else if (hour >= 16 && hour <= 19) {
period = 3;
} else if (hour >= 11 && hour <= 15) {
period = 2;
} else if (hour >= 4 && hour <= 10) {
period = 1;
} else {
period = 0;
}
return period;
}
/**
* 获取时段描述
*
* @param timePeriod
* @return
*/
public static String getTimePeriodDesc(int timePeriod){
String desc = null;
if(timePeriod == 0)
desc = "凌晨";
else if(timePeriod == 1)
desc= "上午";
else if(timePeriod == 2)
desc= "下午";
else if(timePeriod == 3)
desc= "傍晚";
else if(timePeriod == 4)
desc= "夜晚";
return desc;
}
public static final String campPeriodUrl = "http://cdn2.picooc.com/web/res/fatburn/image/bodyChange/{period}2.png";
/**
* 获取燃脂营时段图片url
*
* @param timePeriod
* @return
*/
public static String getCampTimePeriodUrl(int timePeriod) {
String url = null;
switch (timePeriod) {
case 0:
url = campPeriodUrl.replace("{period}", "start");
break;
case 1:
url = campPeriodUrl.replace("{period}", "sun");
break;
case 2:
url = campPeriodUrl.replace("{period}", "sunner");
break;
case 3:
url = campPeriodUrl.replace("{period}", "cloudy");
break;
case 4:
url = campPeriodUrl.replace("{period}", "night");
break;
default:
break;
}
return url;
}
/**
* 各时段具体时间
*
* @param timePeriod
* @return
*/
public static String getPeriodDetail(int timePeriod) {
String time = null;
switch (timePeriod) {
case 0:
time = "0:00-4:00";
break;
case 1:
time = "4:00-12:00";
break;
case 2:
time = "12:00-16:00";
break;
case 3:
time = "16:00-20:00";
break;
case 4:
time = "20:00-24:00";
break;
default:
break;
}
return time;
}
/**
* 获取当天是否是周末
*
* @return
*/
public static int isWeekend(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY)
return 1;
else
return 0;
}
public static Date[] getDayBeginAndEnd(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
Date begin = cal.getTime();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
Date end = cal.getTime();
return new Date[] { begin, end };
}
public static long getDayDiff(Timestamp time1, Timestamp time2) {
return (time2.getTime() - time1.getTime()) / 86400000;
}
public static String getTimeDiffDesc(Long time) {
String result = "";
Long currentTime = System.currentTimeMillis() / 1000;
Integer timeDiff = (int) (currentTime - time / 1000);
if (timeDiff < 60)
result = timeDiff + "秒前";
else if (timeDiff < 60 * 60)
result = timeDiff / 60 + "分钟前";
else if (timeDiff < 60 * 60 * 24)
result = timeDiff / 60 / 60 + "小时前";
else
result = timeDiff / 60 / 60 / 24 + "天前";
return result;
}
/**
* 当天日期显示HH:mm,非当天显示MM月DD日 HH:mm
*
* @param time
* @return
*/
public static String getDateFormatStr(Long time) {
String result = "";
long start = getDayStartTimeAndEndTimeByTimestamp(System.currentTimeMillis())[0];
if (time > start)
result = DateUtils.formatDate(new Date(time), DateUtils.HH_mm);
else
result = DateUtils.formatDate(new Date(time), DateUtils.MMYDDR_HHMM);
return result;
}
/**
* 替换当前时间的月日
*
* @param time
* @return
*/
public static Date replaceMonth(String time) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int year = calendar.get(calendar.YEAR);
String str = year + "年" + time + " " + hour + ":" + minute + ":" + second;
return parseDate(str, DateUtils.YYYYNMMYDDR_HHMMSS);
}
/**
*
* getSchemeTime:(获取当前时间后几周的时间)
*
*
* @param startTime
* @param count
* @return
* @since JDK 1.8
*/
public static Timestamp getWeeksTime(Timestamp startTime, int count) {
Timestamp time = null;
switch (count) {
case 1:
time = startTime;
break;
case 2:
time = DateUtils.nextTimestamp(startTime, 6);
break;
case 3:
time = DateUtils.nextTimestamp(startTime, 13);
break;
case 4:
time = DateUtils.nextTimestamp(startTime, 20);
break;
case 5:
time = DateUtils.nextTimestamp(startTime, 27);
break;
case 6:
time = DateUtils.nextTimestamp(startTime, 34);
break;
case 7:
time = DateUtils.nextTimestamp(startTime, 41);
break;
case 8:
time = DateUtils.nextTimestamp(startTime, 48);
break;
}
return time;
}
/**
*
* getSchemeTime:(获取当前时间前几周的时间)
*
*
* @param startTime
* @param count
* @return
* @since JDK 1.8
*/
public static Timestamp getBeforeWeeksTime(Timestamp startTime, int count) {
Timestamp time = null;
switch (count) {
case 1:
time = startTime;
break;
case 2:
time = DateUtils.nextTimestamp(startTime, -6);
break;
case 3:
time = DateUtils.nextTimestamp(startTime, -13);
break;
case 4:
time = DateUtils.nextTimestamp(startTime, -20);
break;
case 5:
time = DateUtils.nextTimestamp(startTime, -27);
break;
case 6:
time = DateUtils.nextTimestamp(startTime, -34);
break;
case 7:
time = DateUtils.nextTimestamp(startTime, -41);
break;
case 8:
time = DateUtils.nextTimestamp(startTime, -48);
break;
}
return time;
}
/**
*
* getAddOneWeeksTime:(获取当前时间后几周加一天的时间)
*
* @author wangxingbo
* @param startTime
* @param count
* @return
* @since JDK 1.8
*/
public static Timestamp getAddOneWeeksTime(Timestamp startTime, int count) {
Timestamp time = null;
switch (count) {
case 1:
time = startTime;
break;
case 2:
time = DateUtils.nextTimestamp(startTime, 7);
break;
case 3:
time = DateUtils.nextTimestamp(startTime, 14);
break;
case 4:
time = DateUtils.nextTimestamp(startTime, 21);
break;
case 5:
time = DateUtils.nextTimestamp(startTime, 28);
break;
case 6:
time = DateUtils.nextTimestamp(startTime, 35);
break;
case 7:
time = DateUtils.nextTimestamp(startTime, 42);
break;
case 8:
time = DateUtils.nextTimestamp(startTime, 49);
break;
}
return time;
}
/**
*
* compareDate:(判断两个时间大小)
*
*
* @param d1
* @param d2
* @return
* @since JDK 1.8
*/
public static int compareDate(Date d1, Date d2) {
if (d1.getTime() > d2.getTime()) {// dt1 在dt2前
return 1;
} else if (d1.getTime() < d2.getTime()) {// dt1在dt2后
return -1;
} else {// 相等
return 0;
}
}
/**
* 搜索离得最远的日期.适合只进行一次的快速搜索.
* @author wang
* @param list
* @param d
* @return
*/
public static Date findLast(List<Date> list, Date d){
if(list==null || list.size()<=0){
return null;
}
long gap=Long.MIN_VALUE;
Date r=null;
long time=d.getTime();
for(Date t:list){
long tm=Math.abs(time-t.getTime());
if(gap<tm){
gap=tm;
r=t;
}
}
return r;
}
/**
* 搜索离得最近的日期.适合只进行一次的快速搜索.
* @param list
* @param d
* @return
*/
public static Date findStart(List<Date> list,Date d){
if(list==null || list.size()<=0){
return null;
}
long gap=Long.MAX_VALUE;
Date r=null;
long time=d.getTime();
for(Date t:list){
long tm=Math.abs(time-t.getTime());
if(gap>tm){
gap=tm;
r=t;
}
}
return r;
}
/**
*
* getVideoLength:(获取视频时长)
*
*
* @param filePath
* @return
* @since JDK 1.8
*/
public static Map<String, Object> getVideoLength(String filePath) {
Map<String, Object> map = new HashMap<>();
IContainer container = IContainer.make();
int result = container.open(filePath, IContainer.Type.READ, null);
if (result < 0)
throw new RuntimeException("Failed to open media file");
long duration = container.getDuration() / 1000;
long fileSize = container.getFileSize();
map.put("duration", duration);
map.put("fileSize", fileSize);
// System.out.println("视频大小 :" + container.getFileSize() + "B");
// String talde = "";
// int hour = (int) (duration / 3600000);
// long hr = duration % 3600000;
// int minute = (int) ((hr) / 60000);
// long mr = hr % 60000;
// int second = (int) mr / 1000;
// long ms = mr % 1000;
// if (hour < 10) {
// talde += "0" + hour;
// } else {
// talde += hour;
// }
// if (minute < 10) {
// talde += ":0" + minute;
// } else {
// talde += ":" + minute;
// }
// if (second < 10) {
// talde += ":0" + second;
// } else {
// talde += ":" + second;
// }
// System.out.println("视频时常 :" + talde + "." + ms);
return map;
}
/**
* 现在到今天结束还剩多少毫秒(服务器时区)
* @return
*/
public static int getDayMsecRemaind() {
Calendar calendar = Calendar.getInstance();
long current = System.currentTimeMillis();
calendar.setTimeInMillis(current);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
int result = (int) (calendar.getTimeInMillis() - current);
return result < 0 ? 0 : result;
}
/**
*
*
* @param thisTime
* @param pastDay
* @return date
*/
public static Date getFetureDate(Date thisTime, int pastDay) throws ParseException {
if (Objects.isNull(thisTime)) {
LOG.error("Messages : getFetureDate parameters is nulls");
return new Date();
}
DateTime dateTime = new DateTime(thisTime);
return dateTime.plusDays(pastDay).toDate();
}
}
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import com.xuggle.xuggler.IContainer;
public class DateUtils extends org.apache.commons.lang.time.DateUtils {
private final static Logger LOG = Logger.getLogger(DateUtils.class);
// 一周的毫秒数
public final static long WEEK_MILLIS = 7 * 86400000;
// 一天的毫秒数86400000
public final static long DAY_MILLIS = 86400000;// day
public final static long hour_MILLIS = 3600000;// hour
public final static long minu_MILLIS = 60000;// minu
private static final String[] PATTERNS = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "yyyyMMdd" };
/**
* 预定义的日期格式:yyyy-MM-dd HH:mm:ss
*/
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/**
* 预定义的日期格式:yyyy-MM-dd HH:mm
*/
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
/**
* 预定义的日期格式:yyyy年MM月dd日
*/
public static final String YYYYNMMYDDR = "yyyy年MM月dd日";
/**
* 预定义的日期格式:MM月dd日
*/
public static final String MMYDDR = "MM月dd日";
/**
* 预定义的日期格式:yyyyMMdd_HHmmss
*/
public static final String YYYYMMDD_HHMMSS = "yyyyMMdd_HHmmss";
/**
* 预定义的日期格式:yyyyMMddHHmmss
*/
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
/**
* 预定义的日期格式:yyyy-MM-dd HH:mm
*/
public static final String YYYYMMDD_HHMM = "yyyyMMdd HHmm";
/**
* 预定义的日期格式:yyyy-MM-dd
*/
public static final String YYYY_MM_DD = "yyyy-MM-dd";
/**
* 预定义的日期格式:yyyy-MM
*/
public static final String YYYY_MM = "yyyy-MM";
/**
* 预定义的日期格式:yyyyMMddHH
*/
public static final String YYYYMMDDHH = "yyyyMMddHH";
/**
* 预定义的日期格式:yyyyMMdd
*/
public static final String YYYYMMDD = "yyyyMMdd";
/**
* 预定义的日期格式:yyyy.MM.dd
*/
public static final String YYYYMMDDR = "yyyy.MM.dd";
/**
* 预定义的日期格式:yyMMdd
*/
public static final String YYMMDD = "yyMMdd";
/**
* 预定义的日期格式:yyyyMM
*/
public static final String YYYYMM = "yyyyMM";
/**
* 预定义的日期格式:yyyy
*/
public static final String YYYY = "yyyy";
/**
* 预定义的日期格式:MM-dd
*/
public static final String MM_dd = "MM-dd";
/**
* 预定义的日期格式:MM.dd
*/
public static final String MMPdd = "MM.dd";
/**
* 预定义的日期格式:MMdd
*/
public static final String MMdd = "MMdd";
/**
* 预定义的日期格式:dd
*/
public static final String dd = "dd";
/**
* 预定义的日期格式:MM
*/
public static final String MM = "MM";
/**
* 预定义的日期格式:MM月dd日 HH:mm
*/
public static final String MMYDDR_HHMM = "MM月dd日 HH:mm";
/**
* 预定义的日期格式:yyyy年MM月dd日 HH:mm:ss
*/
public static final String YYYYNMMYDDR_HHMMSS = "yyyy年MM月dd日 HH:mm:ss";
/**
* 预定义的日期格式:HH:mm
*/
public static final String HH_mm = "HH:mm";
/**
* 预定义的日期格式:HH:mm:ss
*/
public static final String HH_mm_ss = "HH:mm:ss";
/**
* 预定义的日期格式:HHmm
*/
public static final String HHmm = "HHmm";
/**
* 预定义的日期格式:MM-dd HH:mm 月日时分
*/
public static final String MM_dd_HH_mm = "MM-dd HH:mm";
/**
* 预定义的日期格式:HHmmssSSS 时分秒毫秒
*/
public static final String YYYYMMDD_HHMMSSSSS = "yyyyMMdd_HHmmssSSS";
static public String formatDate(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
/**
* 讲一个时间戳转换秤某个格式的时间
* @param l
* @param format
* @return
*/
public static String changeTimeStampToFormatTime(long l, String format) {
Date date = new Date(l);
SimpleDateFormat f = new SimpleDateFormat(format);
return f.format(date);
}
/**
* 将一个时间戳转换成活跃时段需要的数值的方式,比如,将12:20转换为12.333333
* @param time
* @return
*/
public static float formatTimestampToFloat(long time){
String s = changeTimeStampToFormatTime(time, "HH:mm");
String[] ss = s.split(":");
return Integer.parseInt(ss[0]) + Integer.parseInt(ss[1]) / 60.0f;
}
static public Date parseDate(String date, String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(date);
}catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static long getHowManyDaysBetweenDates(Date newDate, Date oldDate) {
long newTime = newDate.getTime();
long oldTime = oldDate.getTime();
long days = (newTime / 1000 - oldTime / 1000) / (DAY_MILLIS / 1000);
// 如果days为0,代表是同一天,如果days大于0,代表newTime是oldTime之后的时间,如果days小于0,代表newTime是oldTime之前的时间
return days;
}
/**
* 判断两个时间是否跨年
* @param startDate
* @param endDate
* @return
*/
public static boolean isExceedYear(Date startDate, Date endDate) {
boolean result = false;
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
int startYear = calendar.get(Calendar.YEAR);
calendar.setTime(endDate);
int endYear = calendar.get(Calendar.YEAR);
if (startYear != endYear){
result = true;
}
return result;
}
/**
* 本年显示 月-日, 非本年显示年-月-日
*
* @param date
* @return
*/
public static String getFormatDate(Date date) {
String result = "";
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
String valueOf = formatDate(date, "yyyyMMdd");
boolean startsWith = valueOf.startsWith(String.valueOf(year));
if (startsWith) {
result = formatDate(date, "M月d日");
} else {
result = formatDate(date, "yyyy年M月d日");
}
return result;
}
public static String getFormatDateTwo(Date date) {
String result = "";
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
String valueOf = formatDate(date, "yyyyMMdd");
boolean startsWith = valueOf.startsWith(String.valueOf(year));
if (startsWith) {
result = formatDate(date, "MM月dd日");
} else {
result = formatDate(date, "yyyy年MM月dd日");
}
return result;
}
/**
* 生日转换为年龄
*
* @param a
* @return
* @throws Exception
*/
public static int getAge(String bir, String format) {
Calendar cal = Calendar.getInstance();
java.text.DateFormat df = new SimpleDateFormat(format);
Date a = null;
try {
a = df.parse(bir);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
if (cal.before(a)) {
throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
}
// cal.setTimeInMillis(System.currentTimeMillis());
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH) + 1;
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(a);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
// monthNow==monthBirth
if (dayOfMonthNow < dayOfMonthBirth) {
age--;
}
} else {
// monthNow>monthBirth
age--;
}
}
return age;
}
/*
* 获取某时间段内的年龄,精确到月份
*
* @param now
* @param birth
* @return
*/
@SuppressWarnings("deprecation")
public static int getAgeM(Date now, Date birth) {
int yearNow = now.getYear();
int monthNow = now.getMonth();
int yearBirth = birth.getYear();
int monthBirth = birth.getMonth();
int age = yearNow - yearBirth;
if (monthNow < monthBirth) {
age--;
}
return age;
}
// 根据一个时间戳,得到这个时间戳所处天的起始时间和结束时间
public static long[] getDayStartTimeAndEndTimeByTimestamp(long timeStamp) {
long time[] = new long[2];
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
time[0] = calendar.getTimeInMillis();
time[1] = time[0] + DAY_MILLIS - 1000;
return time;
}
/**
* 取得前后day天数的日期,day为负数表示以前的日期
* @param date
* @param day
* @return
*/
public static Date nextDate(Date date, int day) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
return calendar.getTime();
}
/**
* 间隔时间后的时间戳,day负数表示以前
*
* @param t
* @param day
* @return
*/
public static Timestamp nextTimestamp(Timestamp t, int day) {
return nextTimestamp(t, day, 0, 0, 0);
}
/**
* 间隔时间后的时间戳,day、hour负数表示以前
*
* @param t
* @param day
* @param hour
* @return
*/
public static Timestamp nextTimestamp(Timestamp t, int day, int hour) {
return nextTimestamp(t, day, hour, 0, 0);
}
/**
* 间隔时间后的时间戳
*
* @param t
* @param day
* @param hour
* @return
*/
public static Timestamp nextTimestamp(Timestamp t, int day, int hour, int minute, int second) {
if (t == null) {
return null;
}
return new Timestamp(t.getTime() + (day * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 + second) * 1000L);
}
/**
* 获得两个时间戳之间的时间差
* @param timestamp1
* @param timestamp2
* @return
*/
public static long getTimeDiffBetweenTimestamps(Timestamp timestamp1, Timestamp timestamp2){
return timestamp1.getTime() - timestamp2.getTime();
}
/**
* 计算两个日期天数差,如果date2 > date1 返回正数,否则返回负数
* <p>仅天数差,不考虑时分秒,将日期转换成当天的00:00:00后求天数差
*
* @param date1
* @param date2
* @return
*/
public static long dayDiff(Date date1, Date date2) {
Calendar calendar = Calendar.getInstance();
calendar.set(date1.getYear() + 1900, date1.getMonth(), date1.getDate(), 0, 0, 0);
date1 = calendar.getTime();
calendar.set(date2.getYear() + 1900, date2.getMonth(), date2.getDate(), 0, 0, 0);
date2 = calendar.getTime();
return (date2.getTime() - date1.getTime()) / 86400000;
}
/**
* 日期是否在某日期范围内
* @param date
* @param beginDate
* @param endDate
* @return
*/
public static boolean inDateRange(Date date, Date beginDate, Date endDate){
long time = date.getTime();
long beginTime = beginDate.getTime();
long endTime = endDate.getTime();
return (time >= beginTime && time <= endTime);
}
/**
* 获得年龄
*
* @param birthday
* @return
*/
public static int getAgeByBirthday(Date birthday) {
Date now = new Date();
int age = now.getYear() - birthday.getYear() - 1;
if (now.getMonth() > birthday.getMonth()) {
age++;
} else if (now.getMonth() == birthday.getMonth()) {
if (now.getDate() >= birthday.getDate())
age++;
}
return age;
}
/**
* 获得称重时年龄
*
* @param birthday
* @param weightDate
* @return
*/
public static int getAgeByBirthday(Date birthday, Date weightDate) {
int age = weightDate.getYear() - birthday.getYear() - 1;
if (weightDate.getMonth() > birthday.getMonth()) {
age++;
} else if (weightDate.getMonth() == birthday.getMonth()) {
if (weightDate.getDate() >= birthday.getDate())
age++;
}
return age;
}
/**
* 获取时段
*
* @param hour
* @return
*/
public static int getTimePeriod(int hour) {
int period = -1;
if (hour >= 20 && hour <= 23) {
period = 4;
} else if (hour >= 16 && hour <= 19) {
period = 3;
} else if (hour >= 12 && hour <= 15) {
period = 2;
} else if (hour >= 4 && hour <= 11) {
period = 1;
} else {
period = 0;
}
return period;
}
/**
* 获取时段(旧版本)
*
* @param hour
* @return
*/
public static int getTimePeriodForOldVersion(int hour) {
int period = -1;
if (hour >= 20 && hour <= 23) {
period = 4;
} else if (hour >= 16 && hour <= 19) {
period = 3;
} else if (hour >= 11 && hour <= 15) {
period = 2;
} else if (hour >= 4 && hour <= 10) {
period = 1;
} else {
period = 0;
}
return period;
}
/**
* 获取时段描述
*
* @param timePeriod
* @return
*/
public static String getTimePeriodDesc(int timePeriod){
String desc = null;
if(timePeriod == 0)
desc = "凌晨";
else if(timePeriod == 1)
desc= "上午";
else if(timePeriod == 2)
desc= "下午";
else if(timePeriod == 3)
desc= "傍晚";
else if(timePeriod == 4)
desc= "夜晚";
return desc;
}
public static final String campPeriodUrl = "http://cdn2.picooc.com/web/res/fatburn/image/bodyChange/{period}2.png";
/**
* 获取燃脂营时段图片url
*
* @param timePeriod
* @return
*/
public static String getCampTimePeriodUrl(int timePeriod) {
String url = null;
switch (timePeriod) {
case 0:
url = campPeriodUrl.replace("{period}", "start");
break;
case 1:
url = campPeriodUrl.replace("{period}", "sun");
break;
case 2:
url = campPeriodUrl.replace("{period}", "sunner");
break;
case 3:
url = campPeriodUrl.replace("{period}", "cloudy");
break;
case 4:
url = campPeriodUrl.replace("{period}", "night");
break;
default:
break;
}
return url;
}
/**
* 各时段具体时间
*
* @param timePeriod
* @return
*/
public static String getPeriodDetail(int timePeriod) {
String time = null;
switch (timePeriod) {
case 0:
time = "0:00-4:00";
break;
case 1:
time = "4:00-12:00";
break;
case 2:
time = "12:00-16:00";
break;
case 3:
time = "16:00-20:00";
break;
case 4:
time = "20:00-24:00";
break;
default:
break;
}
return time;
}
/**
* 获取当天是否是周末
*
* @return
*/
public static int isWeekend(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY)
return 1;
else
return 0;
}
public static Date[] getDayBeginAndEnd(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
Date begin = cal.getTime();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
Date end = cal.getTime();
return new Date[] { begin, end };
}
public static long getDayDiff(Timestamp time1, Timestamp time2) {
return (time2.getTime() - time1.getTime()) / 86400000;
}
public static String getTimeDiffDesc(Long time) {
String result = "";
Long currentTime = System.currentTimeMillis() / 1000;
Integer timeDiff = (int) (currentTime - time / 1000);
if (timeDiff < 60)
result = timeDiff + "秒前";
else if (timeDiff < 60 * 60)
result = timeDiff / 60 + "分钟前";
else if (timeDiff < 60 * 60 * 24)
result = timeDiff / 60 / 60 + "小时前";
else
result = timeDiff / 60 / 60 / 24 + "天前";
return result;
}
/**
* 当天日期显示HH:mm,非当天显示MM月DD日 HH:mm
*
* @param time
* @return
*/
public static String getDateFormatStr(Long time) {
String result = "";
long start = getDayStartTimeAndEndTimeByTimestamp(System.currentTimeMillis())[0];
if (time > start)
result = DateUtils.formatDate(new Date(time), DateUtils.HH_mm);
else
result = DateUtils.formatDate(new Date(time), DateUtils.MMYDDR_HHMM);
return result;
}
/**
* 替换当前时间的月日
*
* @param time
* @return
*/
public static Date replaceMonth(String time) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int year = calendar.get(calendar.YEAR);
String str = year + "年" + time + " " + hour + ":" + minute + ":" + second;
return parseDate(str, DateUtils.YYYYNMMYDDR_HHMMSS);
}
/**
*
* getSchemeTime:(获取当前时间后几周的时间)
*
*
* @param startTime
* @param count
* @return
* @since JDK 1.8
*/
public static Timestamp getWeeksTime(Timestamp startTime, int count) {
Timestamp time = null;
switch (count) {
case 1:
time = startTime;
break;
case 2:
time = DateUtils.nextTimestamp(startTime, 6);
break;
case 3:
time = DateUtils.nextTimestamp(startTime, 13);
break;
case 4:
time = DateUtils.nextTimestamp(startTime, 20);
break;
case 5:
time = DateUtils.nextTimestamp(startTime, 27);
break;
case 6:
time = DateUtils.nextTimestamp(startTime, 34);
break;
case 7:
time = DateUtils.nextTimestamp(startTime, 41);
break;
case 8:
time = DateUtils.nextTimestamp(startTime, 48);
break;
}
return time;
}
/**
*
* getSchemeTime:(获取当前时间前几周的时间)
*
*
* @param startTime
* @param count
* @return
* @since JDK 1.8
*/
public static Timestamp getBeforeWeeksTime(Timestamp startTime, int count) {
Timestamp time = null;
switch (count) {
case 1:
time = startTime;
break;
case 2:
time = DateUtils.nextTimestamp(startTime, -6);
break;
case 3:
time = DateUtils.nextTimestamp(startTime, -13);
break;
case 4:
time = DateUtils.nextTimestamp(startTime, -20);
break;
case 5:
time = DateUtils.nextTimestamp(startTime, -27);
break;
case 6:
time = DateUtils.nextTimestamp(startTime, -34);
break;
case 7:
time = DateUtils.nextTimestamp(startTime, -41);
break;
case 8:
time = DateUtils.nextTimestamp(startTime, -48);
break;
}
return time;
}
/**
*
* getAddOneWeeksTime:(获取当前时间后几周加一天的时间)
*
* @author wangxingbo
* @param startTime
* @param count
* @return
* @since JDK 1.8
*/
public static Timestamp getAddOneWeeksTime(Timestamp startTime, int count) {
Timestamp time = null;
switch (count) {
case 1:
time = startTime;
break;
case 2:
time = DateUtils.nextTimestamp(startTime, 7);
break;
case 3:
time = DateUtils.nextTimestamp(startTime, 14);
break;
case 4:
time = DateUtils.nextTimestamp(startTime, 21);
break;
case 5:
time = DateUtils.nextTimestamp(startTime, 28);
break;
case 6:
time = DateUtils.nextTimestamp(startTime, 35);
break;
case 7:
time = DateUtils.nextTimestamp(startTime, 42);
break;
case 8:
time = DateUtils.nextTimestamp(startTime, 49);
break;
}
return time;
}
/**
*
* compareDate:(判断两个时间大小)
*
*
* @param d1
* @param d2
* @return
* @since JDK 1.8
*/
public static int compareDate(Date d1, Date d2) {
if (d1.getTime() > d2.getTime()) {// dt1 在dt2前
return 1;
} else if (d1.getTime() < d2.getTime()) {// dt1在dt2后
return -1;
} else {// 相等
return 0;
}
}
/**
* 搜索离得最远的日期.适合只进行一次的快速搜索.
* @author wang
* @param list
* @param d
* @return
*/
public static Date findLast(List<Date> list, Date d){
if(list==null || list.size()<=0){
return null;
}
long gap=Long.MIN_VALUE;
Date r=null;
long time=d.getTime();
for(Date t:list){
long tm=Math.abs(time-t.getTime());
if(gap<tm){
gap=tm;
r=t;
}
}
return r;
}
/**
* 搜索离得最近的日期.适合只进行一次的快速搜索.
* @param list
* @param d
* @return
*/
public static Date findStart(List<Date> list,Date d){
if(list==null || list.size()<=0){
return null;
}
long gap=Long.MAX_VALUE;
Date r=null;
long time=d.getTime();
for(Date t:list){
long tm=Math.abs(time-t.getTime());
if(gap>tm){
gap=tm;
r=t;
}
}
return r;
}
/**
*
* getVideoLength:(获取视频时长)
*
*
* @param filePath
* @return
* @since JDK 1.8
*/
public static Map<String, Object> getVideoLength(String filePath) {
Map<String, Object> map = new HashMap<>();
IContainer container = IContainer.make();
int result = container.open(filePath, IContainer.Type.READ, null);
if (result < 0)
throw new RuntimeException("Failed to open media file");
long duration = container.getDuration() / 1000;
long fileSize = container.getFileSize();
map.put("duration", duration);
map.put("fileSize", fileSize);
// System.out.println("视频大小 :" + container.getFileSize() + "B");
// String talde = "";
// int hour = (int) (duration / 3600000);
// long hr = duration % 3600000;
// int minute = (int) ((hr) / 60000);
// long mr = hr % 60000;
// int second = (int) mr / 1000;
// long ms = mr % 1000;
// if (hour < 10) {
// talde += "0" + hour;
// } else {
// talde += hour;
// }
// if (minute < 10) {
// talde += ":0" + minute;
// } else {
// talde += ":" + minute;
// }
// if (second < 10) {
// talde += ":0" + second;
// } else {
// talde += ":" + second;
// }
// System.out.println("视频时常 :" + talde + "." + ms);
return map;
}
/**
* 现在到今天结束还剩多少毫秒(服务器时区)
* @return
*/
public static int getDayMsecRemaind() {
Calendar calendar = Calendar.getInstance();
long current = System.currentTimeMillis();
calendar.setTimeInMillis(current);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
int result = (int) (calendar.getTimeInMillis() - current);
return result < 0 ? 0 : result;
}
/**
*
*
* @param thisTime
* @param pastDay
* @return date
*/
public static Date getFetureDate(Date thisTime, int pastDay) throws ParseException {
if (Objects.isNull(thisTime)) {
LOG.error("Messages : getFetureDate parameters is nulls");
return new Date();
}
DateTime dateTime = new DateTime(thisTime);
return dateTime.plusDays(pastDay).toDate();
}
}