原创:转载请注明出处
1、获取当天结束日期
/** * author:GaoFei * date:2017-01-09 * what:根据string日期获得当天结束时间 * * @param dateString * @return stringDate */ private String provideDayForEndTime(String dateString){ if(dateString == null){ return null;} SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); Date currDate = null; try { currDate = sdf.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } Calendar start = Calendar.getInstance(); start.setTime(currDate); start.set(Calendar.HOUR, 0); start.set(Calendar.MINUTE, 0); start.set(Calendar.SECOND, 0); Calendar end = Calendar.getInstance(); end.setTime(start.getTime()); end.set(Calendar.HOUR,23); end.set(Calendar.MINUTE,59); end.set(Calendar.SECOND,59); return TimeUtil.setSolrDate(end.getTime().toLocaleString()); }
2、获得时间戳的前一天截止时间点
/** * 获得时间戳的前一天时间 */ public static Timestamp getTimeStampBefore(Timestamp timeStamp) { Date date; date = new Date(timeStamp.getTime()); if(date == null) { return null; } long dateLong = date.getTime() - 24 * 60 * 60 * 1000; Date date2 = new Date(dateLong); int hourAmPm; if(date2.getHours()<12){ hourAmPm = 23; }else{ hourAmPm = 11; } Calendar start = Calendar.getInstance(); start.setTime(date2); start.set(Calendar.HOUR, hourAmPm); start.set(Calendar.MINUTE, 59); start.set(Calendar.SECOND, 59); return new Timestamp(start.getTime().getTime()); }
3、获得solr时区错误转换时间
/** * author:GaoFei * date:2016-12-21 * what:将字符串时间格式进行转换:Tue Dec 20 04:14:33 CST 2016 转换成 2016-12-21 * * @param dateStr * @return */ public static String getSolrDateString(String dateStr) { if(dateStr == null){ return null;}// SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US); SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH); Date date = null; try { date = sdf.parse(dateStr); long dateLong = date.getTime() - 8*60*60*1000; date = new Date(dateLong); } catch (ParseException e) { e.printStackTrace(); }// String formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dateStr); String formatStr2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); return formatStr2; }
4、将时间戳转换成solr时间
/** * author:Gao
/** * 将solr时间转为字符串时间 * * @param date * @return */ public static String solrDateToNormalDate(String date) { if (date == null) { return date; } try { date = date.replace("T"," "); date = date.replace("Z",""); } catch (Exception e) { e.printStackTrace(); return date; } return date; }
Fei * date:2016-12-26 * 将时间戳转为solr时间 * @param timestamp * @return */ public static String getSolrDate(Timestamp timestamp) { if (timestamp == null) { return null; } String[] dates = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp).split(" "); if (dates.length < 2) { return null; } return dates[0] + "T" + dates[1] + "Z"; }