有一个时分时间,例如:08:00。我们需要根据这个时间段去数据库查询不同时段的数据,下面方法用于判定这个时分时间属于哪个时间段。适用场景,我们数据库中有早餐、午餐、晚餐、夜餐等时段配置,需要根据当前时间查询数据库在早餐或者午餐等时段有没有消费记录。
/**
* 给定一个时分时间,判定这个时间属于哪个时段
* @param nowTime 当前时间
* @param now_hh_mm 当前时分 10:29
* @param begin_end_time 长度为2的数组,0位置存储开始时间,1位置存储结束时间
* @param mealTime 要判定的时间段(里面有8个时间段) 例如:07:00 09:00 10:00 12:30 16:00 18:00 20:00 00:30 这儿也可以直接传递String数组
* @return 1:当前时间在07:00 ~ 09:00之间;2:当前时间在10:00 ~ 12:30之间
* 3:当前时间在16:00 ~ 18:00之间;4:当前时间在20:00 ~ 00:30之间
* 0:不在任何时段
*/
private Byte checkMealPeriodTime(Calendar nowTime, String now_hh_mm, Date[] begin_end_time, MealTime mealTime) {
Date nowDate = nowTime.getTime();
String time1 = mealTime.getPeriod1Start();
String time2 = mealTime.getPeriod1End();
if (!time1.equals(time2)) {
if (now_hh_mm.compareTo(time1) >= 0 && now_hh_mm.compareTo(time2) <= 0) {
String date_yy_mm_dd = DateUtils.formatDate(nowDate, DateUtils.FMT_YMD);
String temp_begin_time = date_yy_mm_dd + " " + time1 + ":00";
String temp_end_time = date_yy_mm_dd + " " + time2 + ":00";
begin_end_time[0] = DateUtils.parseDate(temp_begin_time, DateUtils.FMT_YMDHMS);
begin_end_time[1] = DateUtils.parseDate(temp_end_time, DateUtils.FMT_YMDHMS);
return 1;
}
}
String time3 = mealTime.getPeriod2Start();
String time4 = mealTime.getPeriod2End();
if (!time3.equals(time4)) {
if (now_hh_mm.compareTo(time3) >= 0 && now_hh_mm.compareTo(time4) <= 0) {
String date_yy_mm_dd = DateUtils.formatDate(nowDate, DateUtils.FMT_YMD);
String temp_begin_time = date_yy_mm_dd + " " + time3 + ":00";
String temp_end_time = date_yy_mm_dd + " " + time4 + ":00";
begin_end_time[0] = DateUtils.parseDate(temp_begin_time, DateUtils.FMT_YMDHMS);
begin_end_time[1] = DateUtils.parseDate(temp_end_time, DateUtils.FMT_YMDHMS);
return 2;
}
}
String time5 = mealTime.getPeriod3Start();
String time6 = mealTime.getPeriod3End();
if (!time5.equals(time6)) {
if (now_hh_mm.compareTo(time5) >= 0 && now_hh_mm.compareTo(time6) <= 0) {
String date_yy_mm_dd = DateUtils.formatDate(nowDate, DateUtils.FMT_YMD);
String temp_begin_time = date_yy_mm_dd + " " + time5 + ":00";
String temp_end_time = date_yy_mm_dd + " " + time6 + ":00";
begin_end_time[0] = DateUtils.parseDate(temp_begin_time, DateUtils.FMT_YMDHMS);
begin_end_time[1] = DateUtils.parseDate(temp_end_time, DateUtils.FMT_YMDHMS);
return 3;
}
}
String time7 = mealTime.getPeriod4Start();
String time8 = mealTime.getPeriod4End();
if (!time7.equals(time8)) {
//隔夜 结束时间在凌晨
if (time8.compareTo(time7) < 0) { //23:00: -- 01:00
String date_yy_mm_dd = DateUtils.formatDate(nowDate, DateUtils.FMT_YMD);//2020-01-08
//00:00:00 之后消费
if (now_hh_mm.compareTo(time8) <= 0) {//2020-01-08 00:20:00 2020-01-07 01:00
nowTime.add(Calendar.DAY_OF_MONTH, -1);
String begin_date_yy_mm_dd = DateUtils.formatDate(nowTime.getTime(), DateUtils.FMT_YMD);//2020-01-07
String temp_begin_time = begin_date_yy_mm_dd + " " + time7 + ":00";//2020-01-07 23:00:00
String temp_end_time = date_yy_mm_dd + " " + time8 + ":00";//2020-01-08 01:00:00
begin_end_time[0] = DateUtils.parseDate(temp_begin_time, DateUtils.FMT_YMDHMS);
begin_end_time[1] = DateUtils.parseDate(temp_end_time, DateUtils.FMT_YMDHMS);
return 4;
}
//00:00:00 之前消费
else if (now_hh_mm.compareTo(time7) >= 0) {//23:40:00
nowTime.add(Calendar.DAY_OF_MONTH, 1);
String end_date_yy_mm_dd = DateUtils.formatDate(nowTime.getTime(), DateUtils.FMT_YMD);//2020-01-09
String temp_begin_time = date_yy_mm_dd + " " + time7 + ":00";//2020-01-08 23:00:00
String temp_end_time = end_date_yy_mm_dd + " " + time8 + ":00";//2020-01-09 01:00:00
begin_end_time[0] = DateUtils.parseDate(temp_begin_time, DateUtils.FMT_YMDHMS);
begin_end_time[1] = DateUtils.parseDate(temp_end_time, DateUtils.FMT_YMDHMS);
return 4;
}
} else {
if (now_hh_mm.compareTo(time7) >= 0 && now_hh_mm.compareTo(time8) <= 0) {
String date_yy_mm_dd = DateUtils.formatDate(nowDate, DateUtils.FMT_YMD);
String temp_begin_time = date_yy_mm_dd + " " + time7 + ":00";
String temp_end_time = date_yy_mm_dd + " " + time8 + ":00";
begin_end_time[0] = DateUtils.parseDate(temp_begin_time, DateUtils.FMT_YMDHMS);
begin_end_time[1] = DateUtils.parseDate(temp_end_time, DateUtils.FMT_YMDHMS);
return 4;
}
}
}
return 0;
}
DateUtils工具类:
import java.sql.Time;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class DateUtils {
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String MM_DD = "MM-dd";
public static final String YYYYMMDD = "yyyyMMdd";
public static final String YYMMDD = "yyMMdd";
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
public static final String YYYY_MM_DD_HH_MM_SS_2 = "yyyy/MM/dd HH:mm:ss";
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static final String HH_MM_SS = "HH:mm:ss";
public static final String HH_MM = "HH:mm";
public static final String YYMMDDHHMMSS_SSS = "yyMMddHHmmssSSS";
public static final String YYYYMMDDHHMMSS_SSS = "yyyyMMddHHmmssSSS";
public static final String MMSS_SSS = "mmssSSS";
public static final String YYYY_MM = "yyyy-MM";
public static final ThreadLocal<SimpleDateFormat> FMT_MD = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(MM_DD);
}
};
public static final ThreadLocal<SimpleDateFormat> FMT_YMD = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(YYYY_MM_DD);
}
};
public static final ThreadLocal<SimpleDateFormat> FMT_YYYYMMDD = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(YYYYMMDD);
}
};
public static final ThreadLocal<SimpleDateFormat> FMT_YYMMDD = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(YYMMDD);
}
};
public static final ThreadLocal<SimpleDateFormat> FMT_YMDHMS = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
}
};
public static final ThreadLocal<SimpleDateFormat> FMT_YMDHMS_1 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(YYYYMMDDHHMMSS);
}
};
public static final ThreadLocal<SimpleDateFormat> FMT_YMDHMS_2 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_2);
}
};
public static final ThreadLocal<SimpleDateFormat> FMT_HMS = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(HH_MM_SS);
}
};
public static final ThreadLocal<SimpleDateFormat> FMT_HM = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(HH_MM);
}
};
public static final ThreadLocal<SimpleDateFormat> FMT_YYMMDDHHMMSS_SSS = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(YYMMDDHHMMSS_SSS);
}
};
public static final ThreadLocal<SimpleDateFormat> FMT_YYYYMMDDHHMMSS_SSS = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(YYYYMMDDHHMMSS_SSS);
}
};
private static final int[] DAYS_OF_MONTH = {31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31};
public static final String[] WEEKDAYS = {"星期日", "星期一", "星期二", "星期三", "星期四",
"星期五", "星期六"};
public static String formatDate(Date date, String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return returnValue;
}
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
public static Date parseDate(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public static Date getTodayDate() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
public static String formatDate(Date date, ThreadLocal<SimpleDateFormat> sdf) {
return sdf.get().format(date);
}
public static Date parseDate(String strDate, ThreadLocal<SimpleDateFormat> sdf) {
Date date = null;
try {
date = sdf.get().parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static Date addDateHour(Date date, Integer hour) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, hour);
return cal.getTime();
}
public static String addMinuteStr_ymdhms(String dt, int num) {
Date date = null;
try {
date = FMT_YMDHMS.get().parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, num);
return FMT_YMDHMS.get().format(cal.getTime());
}
public static String addDateStr_ymdhms(String dt, int num) {
Date date = addDates_ymdhms(dt, num);
return FMT_YMD.get().format(date);
}
public static String addDateStr_ymd(String dt, int num) {
Date date = addDates_ymd(dt, num);
return FMT_YMD.get().format(date);
}
public static Date addDates_ymd(String dt, int num) {
Date date = null;
try {
date = FMT_YMD.get().parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
return addDates(date, num);
}
public static Date addDates_ymdhms(String dt, int num) {
Date date = null;
try {
date = FMT_YMDHMS.get().parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
return addDates(date, num);
}
public static Date addDates(Date date, int num) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, num);
return cal.getTime();
}
public static String ymd_changeTo_ymdhms(String dt) {
return dt + " 00:00:00";
}
public static String ymdhms_changeTo_ymd(String dt) {
return dt.substring(0, YYYY_MM_DD.length());
}
// 2个日期相差多少天
public static long getDayNumberBetweenTwoDays(Date startDate, Date endDate) {
long t1 = startDate.getTime();
long t2 = endDate.getTime();
long count = (t2 - t1) / (24L * 60 * 60 * 1000);
return Math.abs(count);
}
// 2个日期相差多少分钟
public static long getMinuteNumBetweenTwoDays(String startDate, String endDate) {
long count = 0L;
try {
Date st_time = FMT_YMDHMS.get().parse(startDate);
Date ed_time = FMT_YMDHMS.get().parse(endDate);
long t1 = ed_time.getTime();
long t2 = st_time.getTime();
count = (t2 - t1) / (60 * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return Math.abs(count);
}
// 2个日期相差多少分钟
public static int getMinuteNumBetweenTwoDates(Date st_time, Date ed_time) {
int count = 0;
try {
long t1 = ed_time.getTime();
long t2 = st_time.getTime();
count = (int) (t2 - t1) / (60 * 1000);
} catch (Exception e) {
e.printStackTrace();
}
return Math.abs(count);
}
// 2个日期相差多少分钟
public static int getMinuteNumBetweenTwoDates2(Date st_time, Date ed_time) {
int count = 0;
try {
long t1 = ed_time.getTime();
long t2 = st_time.getTime();
if (t1 < t2) {
t1 += (24 * 60 * 60 * 1000);
}
count = (int) (t2 - t1) / (60 * 1000);
} catch (Exception e) {
e.printStackTrace();
}
return Math.abs(count);
}
// 得到星期几
public static String getWeekForDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return WEEKDAYS[w];
}
// 得到星期几下标
public static int getWeekNumForDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
if (w == 0)
w = 7;
return w;
}
// 判断日期是否是周六周末
public boolean isWeekend(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
return dayOfWeek == 1 || dayOfWeek == 7;
}
// date所处周的星期一
public static Date getFirstDayOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return cal.getTime();
}
// date所处周的星期天
public static Date getLastDayOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return cal.getTime();
}
// 得到日期月份最大的天数
public static int getMaxDayOfMonth(int year, int month) {
if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
return 29;
return DAYS_OF_MONTH[month - 1];
}
// 判断2个日期是否在同一天
public static boolean isSameDay(Date date, Date date2) {
String str = formatDate(date, YYYY_MM_DD);
String str2 = formatDate(date2, YYYY_MM_DD);
return str.equals(str2);
}
// yyyy-MM-dd 0:00:00
//days=0 今天,-1昨天,1明天下面的都一样
public static Date getDateStart(Date date, int days) {
Calendar startCal = Calendar.getInstance();
startCal.setTime(date);
startCal.set(5, startCal.get(5) + days);
startCal.set(11, 0);
startCal.set(14, 0);
startCal.set(13, 0);
startCal.set(12, 0);
return startCal.getTime();
}
// yyyy-MM-dd 23:59:59
public static Date getDateEnd(Date date, int days) {
Calendar endCal = Calendar.getInstance();
endCal.setTime(date);
endCal.set(5, endCal.get(5) + days);
endCal.set(11, 23);
endCal.set(14, 59);
endCal.set(13, 59);
endCal.set(12, 59);
return endCal.getTime();
}
// yyyy-MM-1 00:00:00
public static Date getMonthStart(Date date, int n) {
Calendar startCal = Calendar.getInstance();
startCal.setTime(date);
startCal.set(5, 1);
startCal.set(11, 0);
startCal.set(14, 0);
startCal.set(13, 0);
startCal.set(12, 0);
startCal.set(2, startCal.get(2) + n);
return startCal.getTime();
}
// yyyy-MM-end 23:59:59
public static Date getMonthEnd(Date date, int n) {
Calendar endCal = Calendar.getInstance();
endCal.setTime(date);
endCal.set(5, 1);
endCal.set(11, 23);
endCal.set(14, 59);
endCal.set(13, 59);
endCal.set(12, 59);
endCal.set(2, endCal.get(2) + n + 1);
endCal.set(5, endCal.get(5) - 1);
return endCal.getTime();
}
// yyyy-1-1 00:00:00
public static Date getYearStart(Date date, int n) {
Calendar startCal = Calendar.getInstance();
startCal.setTime(date);
startCal.set(2, 0);// JANUARY which is 0;
startCal.set(5, 1);
startCal.set(11, 0);
startCal.set(12, 0);
startCal.set(14, 0);
startCal.set(13, 0);
startCal.set(1, startCal.get(1) + n);
return startCal.getTime();
}
// yyyy-12-31 23:59:59
public static Date getYearEnd(Date date, int n) {
Calendar endCal = Calendar.getInstance();
endCal.setTime(date);
endCal.set(2, 12);
endCal.set(5, 1);
endCal.set(11, 23);
endCal.set(14, 59);
endCal.set(13, 59);
endCal.set(12, 59);
endCal.set(1, endCal.get(1) + n);
endCal.set(5, endCal.get(5) - 1);
return endCal.getTime();
}
// 日期加上n个月
public static Date addMonths(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
// 2个日期相差多少天 不考虑时分秒
public static long getDayDiffIgnoreHHMISS(Date startDate, Date endDate)
throws ParseException {
startDate = getDateStart(startDate, 0);
endDate = getDateStart(endDate, 0);
long t1 = startDate.getTime();
long t2 = endDate.getTime();
long count = (t2 - t1) / (24L * 60 * 60 * 1000);
return Math.abs(count);
}
public static Boolean date1IsMoreDate2(Date date1, Date date2) {
long t1 = date1.getTime();
long t2 = date2.getTime();
if (t1 > t2) {
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
}
// 2个日期相差多少年
public static int getYearDiff(Date minDate, Date maxDate) {
if (minDate.after(maxDate)) {
Date tmp = minDate;
minDate = new Date(maxDate.getTime());
maxDate = new Date(tmp.getTime());
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(minDate);
int year1 = calendar.get(Calendar.YEAR);
int month1 = calendar.get(Calendar.MONTH);
int day1 = calendar.get(Calendar.DATE);
calendar.setTime(maxDate);
int year2 = calendar.get(Calendar.YEAR);
int month2 = calendar.get(Calendar.MONTH);
int day2 = calendar.get(Calendar.DATE);
int result = year2 - year1;
if (month2 < month1) {
result--;
} else if (month2 == month1 && day2 < day1) {
result--;
}
return result;
}
// 2个日期相差多少月
public static int getMonthDiff(Date minDate, Date maxDate) {
if (minDate.after(maxDate)) {
Date tmp = minDate;
minDate = new Date(maxDate.getTime());
maxDate = new Date(tmp.getTime());
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(minDate);
int year1 = calendar.get(Calendar.YEAR);
int month1 = calendar.get(Calendar.MONTH);
int day1 = calendar.get(Calendar.DATE);
calendar.setTime(maxDate);
int year2 = calendar.get(Calendar.YEAR);
int month2 = calendar.get(Calendar.MONTH);
int day2 = calendar.get(Calendar.DATE);
int months = 0;
if (day2 >= day1) {
months = month2 - month1;
} else {
months = month2 - month1 - 1;
}
return (year2 - year1) * 12 + months;
}
// 得到2个日期之间的月份,返回值List<字符串>
public static List getMonthsBetween(Date minDate, Date maxDate) {
ArrayList result = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
if (minDate.after(maxDate)) {
Date tmp = minDate;
minDate = new Date(maxDate.getTime());
maxDate = new Date(tmp.getTime());
}
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(minDate);
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(maxDate);
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
return result;
}
/**
* 最近一周时间段 今天对应的上周星期n-今天
*/
public static Date[] getRecentWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Date to = calendar.getTime();
calendar.set(Calendar.WEEK_OF_MONTH,
calendar.get(Calendar.WEEK_OF_MONTH) - 1);
Date from = calendar.getTime();
return new Date[]{from, to};
}
// 最近一个月 今天对应的上月n日-今天
public static Date[] getRecentMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Date to = calendar.getTime();
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
Date from = calendar.getTime();
return new Date[]{from, to};
}
// 中文显示日期与当前时间差
public static String friendlyFormat(Date date) throws ParseException {
if (date == null) {
return "未知";
}
Date baseDate = new Date();
if (baseDate.before(date)) {
return "未知";
}
int year = getYearDiff(baseDate, date);
int month = getMonthDiff(baseDate, date);
if (year >= 1) {
return year + "年前";
} else if (month >= 1) {
return month + "月前";
}
int day = (int) getDayNumberBetweenTwoDays(baseDate, date);
if (day > 0) {
if (day > 2) {
return day + "天前";
} else if (day == 2) {
return "前天";
} else if (day == 1) {
return "昨天";
}
}
if (!isSameDay(baseDate, date)) {
return "昨天";
}
int hour = (int) ((baseDate.getTime() - date.getTime()) / (1 * 60 * 60 * 1000));
if (hour > 6) {
return "今天";
} else if (hour > 0) {
return hour + "小时前";
}
int minute = (int) ((baseDate.getTime() - date.getTime()) / (1 * 60 * 1000));
if (minute < 2) {
return "刚刚";
} else if (minute < 30) {
return minute + "分钟前";
} else if (minute > 30) {
return "半个小时以前";
}
return "未知";
}
/**
* 20060718164830 -> 2006-07-18 16:48:30
*
* @param dateTimeBytes
* @return
*/
public static String getTimefromByteArray(byte[] dateTimeBytes) {
String dateTime_1 = new String(dateTimeBytes);
Date date = DateUtils.parseDate(dateTime_1, DateUtils.FMT_YMDHMS_1);
String dateTime_2 = DateUtils.formatDate(date, DateUtils.FMT_YMDHMS);
return dateTime_2;
}
/**
* 将长时间格式字符串转换为时间 yyyyMMdd
*
* @param strDate
* @return
*/
public static Date stringToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
ParsePosition pos = new ParsePosition(0);
return formatter.parse(strDate, pos);
}
/**
* 将长时间格式字符串转换为时间 HHmm
*
* @param strDate
* @return
*/
public static Time stringToTime(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("HHmm");
ParsePosition pos = new ParsePosition(0);
return new Time(formatter.parse(strDate, pos).getTime());
}
public static String getWeekFromDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 1)
w = 7;
return "0" + w;
}
public static Date getPlugMinutes(Date currentDate, int minutes) {
Calendar date = Calendar.getInstance();
date.setTime(currentDate);
date.set(Calendar.MINUTE, date.get(Calendar.MINUTE) + minutes);
return date.getTime();
}
/**
* 获取两个日期之间的所有日期
*
* @param start 日期
* @param end 日期
* @return 返回日起列表
* @throws Exception
*/
public static List<Date> dateSplit(Date start, Date end) {
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = start;//sdf.parse(start);
Date endDate = end;//sdf.parse(end);
List<Date> dateList = new ArrayList<Date>();
if (start.compareTo(end) != 0 && !startDate.before(endDate))
return dateList;
Long spi = endDate.getTime() - startDate.getTime();
Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数
dateList.add(startDate);
for (int i = 0; i < step; i++) {
dateList.add(new Date(dateList.get(i).getTime()
+ (24 * 60 * 60 * 1000)));// 比上一天加一
}
return dateList;
}
/**
* 获取时间增加天数之后的时间
*
* @param Caldate 日期字符串
* @param startDt 时间
* @param day 增加天数
* @return 新的时间
* @throws Exception
*/
public static Date getFormatDateTimeStr(String Caldate, String startDt, int day) {
//String reStr = "";
Date dt1 = null;
try {
//String strDate = Caldate + " " + FMT_HMS.get().format(FMT_HM.get().parse(startDt));
String strDate = Caldate + " " + startDt;
Date dt = FMT_YMDHMS.get().parse(strDate);
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
//rightNow.add(Calendar.YEAR,-1);//日期减1年
//rightNow.add(Calendar.MONTH,3);//日期加3个月
rightNow.add(Calendar.DAY_OF_YEAR, day);//日期加10天
dt1 = rightNow.getTime();
//reStr = FMT_YMDHMS.get().format(dt1);
} catch (Exception e) {
e.printStackTrace();
}
return dt1;
}
/**
* 获取unix时间戳
* @param date
* @return
*/
public static Long getUnix(Date date) {
long time = date.getTime();
long unix = time / 1000;
return unix;
}
public static void main(String[] args) throws ParseException {
System.out.println(DateUtils.formatDate(new Date(), DateUtils.FMT_MD));
/*Date dateStart = DateUtils.FMT_YMD.get().parse("2017-06-11");
Date dateEnd = DateUtils.FMT_YMD.get().parse("2017-06-24");
System.out.println(DateUtils.getDayNumberBetweenTwoDays(dateStart,dateEnd));
System.out.println(DateUtils.getFormatDateTimeStr("2017-06-23","8:00:59",0));
System.out.println(addDates(new Date(),-1));
*/
/*Date date1 = DateUtils.parseDate("09:00",FMT_HM);
Date date2 = DateUtils.parseDate("11:00",FMT_HM);
//System.out.println(date1.getHours());
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
System.out.println(calendar1.get(Calendar.HOUR));
System.out.println(DateUtils.formatDate(date1,FMT_YMDHMS));
System.out.println(getMinuteNumBetweenTwoDates2(date1,date2));
System.out.println(getTodayDate());
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date newDate = calendar.getTime();
System.out.println(newDate.getTime());
System.out.println(calendar.getTimeInMillis());
System.out.println(getDayNumberBetweenTwoDays(new Date(),new Date()));*/
Calendar calendar = Calendar.getInstance();
//calendar.set(Calendar.DAY_OF_MONTH, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
System.out.println(DateUtils.formatDate(calendar.getTime(), FMT_YMDHMS));
calendar.add(Calendar.DATE, 9);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
System.out.println(DateUtils.formatDate(calendar.getTime(), FMT_YMDHMS));
}
}