1.常用型日期转换类:
import java.sql.Date; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; public class DateUtil { /** * 得到系统现在的时间 * * @param formatString * 返回的时间字符串形式 默认为: yyyy-MM-dd HH:mm:ss * @return String */ public static String getNowTime(String formatString) { if (StringUtil.isEmptyString(formatString)) formatString = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf; try { Timestamp nowtime = new Timestamp(System.currentTimeMillis()); sdf = new SimpleDateFormat(formatString); return sdf.format(nowtime); } catch (Exception e) { e.printStackTrace(System.err); } return ""; } /** * 把时间型Object对象转化成秒数 * * @return long * @param strDateTime * 需要转化的时间型字符串 如:2006-10-01 00:00:00 * @param strFormat * 需要转化的时间型字符串的格式 如:yyyy-MM-dd HH:mm:ss * */ public static long objectToSeconds(Object strDateTime, String strFormat) { if (StringUtil.isEmptyString(strDateTime)) strDateTime = getNowTime(null); if (StringUtil.isEmptyString(strFormat)) strFormat = "yyyy-MM-dd HH:mm:ss"; try { SimpleDateFormat format = new SimpleDateFormat(strFormat); java.util.Date date = format.parse(strDateTime.toString()); long seconds = (date.getTime() / 1000); return seconds; } catch (ParseException e) { } return 0; } /** * 把秒数转化成时间型字符串 * * @param seconds * 需要转化的时间总秒数 * @param strFormat * 返回的时间字符串形式 如:yyyy-MM-dd HH:mm:ss * @return String */ public static String formatSeconds(long seconds, String strFormat) { if (seconds <= 0) return ""; if (StringUtil.isEmptyString(strFormat)) strFormat = "yyyy-MM-dd HH:mm:ss"; try { SimpleDateFormat format = new SimpleDateFormat(strFormat); Date date = new Date(System.currentTimeMillis()); date.setTime(seconds * 1000); return format.format(date); } catch (Exception e) { ; } return ""; } /** * 把时间类型的Object对象转化为Timestamp * * @param strDateTime * Object * @param strFormat * 转化的字符串(strDateTime)形式 如:yyyy-MM-dd HH:mm:ss * @return Timestamp */ public static Timestamp objectToTimestamp(Object strDateTime, String strFormat) { if (StringUtil.isEmptyString(strDateTime)) strDateTime = getNowTime(null); if (StringUtil.isEmptyString(strFormat)) strFormat = "yyyy-MM-dd HH:mm:ss"; try { long Seconds = objectToSeconds(strDateTime, strFormat); Timestamp time = new Timestamp(Seconds * 1000); return time; } catch (Exception e) { ; } return null; } public static Timestamp objectToTimestamp(Object strDateTime) { if (StringUtil.isEmptyString(strDateTime)) return null; try { return Timestamp.valueOf(strDateTime.toString()); } catch (Exception e) { ; } return null; } /** * 把时间类型的Object对象转化为字符串 * * @param timestamp * Object * @param strFormat * 转化后的字符串形式 如:yyyy-MM-dd HH:mm:ss * @return String */ public static String formatObject(Object timestamp, String strFormat) { if (StringUtil.isEmptyString(timestamp)) return ""; if (StringUtil.isEmptyString(strFormat)) strFormat = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf; try { sdf = new SimpleDateFormat(strFormat); return sdf.format(timestamp); } catch (Exception e) { ; } return ""; } /** * 把时间型字符串转化成Date * * @return Date java.util.Date * @param strDateTime * 需要转化的时间型字符串 如:2006-10-01 00:00:00 * */ public static java.util.Date stringToDate(Object objDateTime) { java.util.Date date = null; if (StringUtil.isEmptyString(objDateTime)) return date; try { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); date = format.parse(objDateTime.toString()); String tmpDate = format.format(date); return Date.valueOf(tmpDate); } catch (Exception e) { ; } return date; } /** * 取得当天日期 yyyy-MM-dd * * @return Date java.util.Date * @param * */ public static String currentDay() { try { // 获取系统当前时间 Calendar rightNow = Calendar.getInstance(); SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); return fmt.format(rightNow.getTime()); } catch (Exception e) { ; return null; } } /** * 格式化当前日期 * @param pattern * @return */ public static java.util.Date currentDay(String pattern) { java.util.Date date = new java.util.Date(); if (StringUtil.isEmptyString(pattern)) pattern = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat fmt = new SimpleDateFormat(pattern); String tmpDate = fmt.format(date); return Date.valueOf(tmpDate); } /** * 取得前天日期 yyyy-MM-dd * * @return */ public static String yesterday(String pattern) { try { java.util.Date date = null; if (StringUtil.isEmptyString(pattern)) pattern = "yyyy-MM-dd HH:mm:ss"; // 获取前天的现在时间 Calendar c4 = Calendar.getInstance(); c4.add(Calendar.DATE, -1); date = c4.getTime(); java.text.SimpleDateFormat sf = new SimpleDateFormat(pattern); String str = sf.format(date); return str; } catch (Exception e) { return ""; } } /** * 取得当月第一天, yyyy-MM-dd * * @return Date java.util.Date * @param * */ public static String firstDayofMonth() { try { // 获取系统当前时间 Calendar rightNow = Calendar.getInstance(); SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); // 页面初始化的时候,设置开始时间为当月第一天,设置结束时间为当天 rightNow.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天 return fmt.format(rightNow.getTime()); } catch (Exception e) { ; return null; } } /** * 把秒数转化成时间型HH:mm:ss * * @param seconds * 需要转化的时间总秒数 * @return String */ public static String formatSecondstoHour(long seconds) { if (seconds <= 0) return "00:00:00"; try { long h = seconds / 3600; long m = (seconds - h * 3600) / 60; long s = (seconds - h * 3600) % 60; return h + ":" + m + ":" + s; } catch (Exception e) { ; return "00:00:00"; } } }
2.日期操作
a. JS 中转化将日期\时间转换成秒:
// 将时间转化为秒 <script type="text/javascript" language="javascript"> <!-- function timeFormat(dataTime){ var timeStr = new Array(); timeStr = dataTime.split(/[-\s:]/); var timeStr =(new Date(timeStr[0],timeStr[1]-1,timeStr[2],0,0,0)).valueOf(); return timeStr; } //--> </script>
b. java中的时间推算
// 上周六的时间 Calendar c2 = Calendar.getInstance(); c2.add(Calendar.DATE, 0 - c2.get(Calendar.DAY_OF_WEEK)); String date2 = DateUtil.formatObject(c2.getTime(), "yyyy-MM-dd"); // N周前的时间 Calendar c1 = Calendar.getInstance(); c1.add(Calendar.DATE, 0 - (searchPeriod * 7 + c1.get(Calendar.DAY_OF_WEEK) - 1)); String date1 = DateUtil.formatObject(c1.getTime(), "yyyy-MM-dd"); // 上月底时间 Calendar c1 = Calendar.getInstance(); c1.add(Calendar.DATE, 0 - c1.get(Calendar.DAY_OF_MONTH)); Date date = new Date(c1.getTimeInMillis()); String date1 = DateUtil.formatObject(date, "yyyy-MM-dd"); // 推算到前N年的时间 date.setYear(date.getYear()-searchPeriod); String date2 = DateUtil.formatObject(date, "yyyy-MM-dd");