/** * 根据pattern 转换日期对象为日期字符串,如"yyyy/MM/dd hh:mm:ss" * @param date 日期 * @param pattern 日期格式 * @return 日期字串 */ public static String getFormatDate(Date date, String pattern) { if (date == null) { return null; } SimpleDateFormat format = new SimpleDateFormat(pattern); String str = format.format(date); return str; }
/** * 根据参数得到当前时间值 * @param p (1:年;2:月;3:日;4:小时;5:分钟;6:秒) * @return */ public static int getTimeValue(int p) { Calendar calendar = Calendar.getInstance(); int result = 0; switch (p) { case 1: result = calendar.get(Calendar.YEAR); break; case 2: result = calendar.get(Calendar.MONTH) + 1; break; case 3: result = calendar.get(Calendar.DATE); break; } return result; }
/** * 得到两个时间的差,并以字符串的形式给出 * @param dt1 * @param dt2 * @return */ public static String getDiffTime(Date dt1, Date dt2) { long time = dt2.getTime() / 1000 - dt1.getTime() / 1000; String str = "0:0:0"; if (time > 0) { long day = time / 60 / 60 / 24; long hour = time / 60 / 60 % 24; long minute = time / 60 % 60; long second = time % 60; if (day > 0) { str = "" + day + "d " + hour + ":" + minute + ":" + second; } else { str = "" + hour + ":" + minute + ":" + second; } } return str; }
/** * 得到时间串精确到毫秒 * * @return */ public static String getTimeChar() { StringBuffer strResponseDate = new StringBuffer(); try { Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); format.setLenient(false);