/** * 日期转时间戳 */ public static long dateToTimestamp(String datetime) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA); long timestamp = 0; try { Date date = sdr.parse(datetime); timestamp = date.getTime(); } catch (ParseException e) { e.printStackTrace(); } return timestamp; } /** * 时间戳转日期 */ public static String timestampToDate(String datetime) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); long timestamp = Long.valueOf(datetime); Date date = new Date(timestamp); return sdr.format(date); }
public static String formateMils(Long millSec) { Date date = new Date(millSec); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(date); }
/** * @param time * 时间戳转换成时分秒 * @return 时:分:秒 */ public static String getFormatFilterTime(long time) { time = time / 1000; long second = time % 60; long minute = (time % 3600) / 60; long hour = time / 3600; // 秒显示两位 String strSecond = ("00" + second) .substring(("00" + second).length() - 2); // 分显示两位 String strMinute = ("00" + minute) .substring(("00" + minute).length() - 2); // 时显示两位 String strHour = ("00" + hour).substring(("00" + hour).length() - 2); return strHour.equals("00") ? strMinute + ":" + strSecond : strHour + ":" + strMinute + ":" + strSecond; }
/** *用日历获取日期时间 */ // 获取某天0点的时间 public static Long getDayStartTime(Calendar calendar) { calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime().getTime(); } // 获取某天23点59分59秒的时间,也就是一天最后的时间 public static Long getDayEndTime(Calendar calendar) { calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 59); return calendar.getTime().getTime(); }