非常有用的Java日期时间操作函数代码一览

By Minidxer | August 11, 2008

今天写的一个Log输出类,需要对日期时间进行特殊的格式化操作–这样的操作实际上经常需要用到,不过一直都没怎么保留过代码,所以又是一番查找,费时又费力。于是决定把用到的函数稍做整理,相信可以节省一些人的时间,注意并不是所有的函数都是必需的,代码也并非本人原创,而是取材于网络,放在这里纯粹是为了方便……大家可以看自己的情况按需拿取,具体函数罗列在下面:


  1. /**
  2. 日期类
  3. * @date  
  4. * @version 1.0
  5. */
  6. import java.util.*;
  7. import java.text.*;
  8. import java.util.Calendar;
  9.  
  10. public class VeDate {
  11.  /**
  12.   * 获取现在时间
  13.   *
  14.   * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
  15.   */
  16.  public static Date getNowDate() {
  17.   Date currentTime = new Date();
  18.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  19.   String dateString = formatter.format(currentTime);
  20.   ParsePosition pos = new ParsePosition(8);
  21.   Date currentTime_2 = formatter.parse(dateString, pos);
  22.   return currentTime_2;
  23.  }
  24.  
  25.  /**
  26.   * 获取现在时间
  27.   *
  28.   * @return返回短时间格式 yyyy-MM-dd
  29.   */
  30.  public static Date getNowDateShort() {
  31.   Date currentTime = new Date();
  32.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  33.   String dateString = formatter.format(currentTime);
  34.   ParsePosition pos = new ParsePosition(8);
  35.   Date currentTime_2 = formatter.parse(dateString, pos);
  36.   return currentTime_2;
  37.  }
  38.  
  39.  /**
  40.   * 获取现在时间
  41.   *
  42.   * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  43.   */
  44.  public static String getStringDate() {
  45.   Date currentTime = new Date();
  46.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  47.   String dateString = formatter.format(currentTime);
  48.   return dateString;
  49.  }
  50.  
  51.  /**
  52.   * 获取现在时间
  53.   *
  54.   * @return 返回短时间字符串格式yyyy-MM-dd
  55.   */
  56.  public static String getStringDateShort() {
  57.   Date currentTime = new Date();
  58.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  59.   String dateString = formatter.format(currentTime);
  60.   return dateString;
  61.  }
  62.  
  63.  /**
  64.   * 获取时间 小时:分;秒 HH:mm:ss
  65.   *
  66.   * @return
  67.   */
  68.  public static String getTimeShort() {
  69.   SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  70.   Date currentTime = new Date();
  71.   String dateString = formatter.format(currentTime);
  72.   return dateString;
  73.  }
  74.  
  75.  /**
  76.   * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
  77.   *
  78.   * @param strDate
  79.   * @return
  80.   */
  81.  public static Date strToDateLong(String strDate) {
  82.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  83.   ParsePosition pos = new ParsePosition(0);
  84.   Date strtodate = formatter.parse(strDate, pos);
  85.   return strtodate;
  86.  }
  87.  
  88.  /**
  89.   * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
  90.   *
  91.   * @param dateDate
  92.   * @return
  93.   */
  94.  public static String dateToStrLong(java.util.Date dateDate) {
  95.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  96.   String dateString = formatter.format(dateDate);
  97.   return dateString;
  98.  }
  99.  
  100.  /**
  101.   * 将短时间格式时间转换为字符串 yyyy-MM-dd
  102.   *
  103.   * @param dateDate
  104.   * @param k
  105.   * @return
  106.   */
  107.  public static String dateToStr(java.util.Date dateDate) {
  108.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  109.   String dateString = formatter.format(dateDate);
  110.   return dateString;
  111.  }
  112.  
  113.  /**
  114.   * 将短时间格式字符串转换为时间 yyyy-MM-dd
  115.   *
  116.   * @param strDate
  117.   * @return
  118.   */
  119.  public static Date strToDate(String strDate) {
  120.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  121.   ParsePosition pos = new ParsePosition(0);
  122.   Date strtodate = formatter.parse(strDate, pos);
  123.   return strtodate;
  124.  }
  125.  
  126.  /**
  127.   * 得到现在时间
  128.   *
  129.   * @return
  130.   */
  131.  public static Date getNow() {
  132.   Date currentTime = new Date();
  133.   return currentTime;
  134.  }
  135.  
  136.  /**
  137.   * 提取一个月中的最后一天
  138.   *
  139.   * @param day
  140.   * @return
  141.   */
  142.  public static Date getLastDate(long day) {
  143.   Date date = new Date();
  144.   long date_3_hm = date.getTime() - 3600000 * 34 * day;
  145.   Date date_3_hm_date = new Date(date_3_hm);
  146.   return date_3_hm_date;
  147.  }
  148.  
  149.  /**
  150.   * 得到现在时间
  151.   *
  152.   * @return 字符串 yyyyMMdd HHmmss
  153.   */
  154.  public static String getStringToday() {
  155.   Date currentTime = new Date();
  156.   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  157.   String dateString = formatter.format(currentTime);
  158.   return dateString;
  159.  }
  160.  
  161.  /**
  162.   * 得到现在小时
  163.   */
  164.  public static String getHour() {
  165.   Date currentTime = new Date();
  166.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  167.   String dateString = formatter.format(currentTime);
  168.   String hour;
  169.   hour = dateString.substring(11, 13);
  170.   return hour;
  171.  }
  172.  
  173.  /**
  174.   * 得到现在分钟
  175.   *
  176.   * @return
  177.   */
  178.  public static String getTime() {
  179.   Date currentTime = new Date();
  180.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  181.   String dateString = formatter.format(currentTime);
  182.   String min;
  183.   min = dateString.substring(14, 16);
  184.   return min;
  185.  }
  186.  
  187.  /**
  188.   * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  189.   *
  190.   * @param sformat
  191.   *            yyyyMMddhhmmss
  192.   * @return
  193.   */
  194.  public static String getUserDate(String sformat) {
  195.   Date currentTime = new Date();
  196.   SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  197.   String dateString = formatter.format(currentTime);
  198.   return dateString;
  199.  }
  200.  
  201.  /**
  202.   * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
  203.   */
  204.  public static String getTwoHour(String st1, String st2) {
  205.   String[] kk = null;
  206.   String[] jj = null;
  207.   kk = st1.split(":");
  208.   jj = st2.split(":");
  209.   if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
  210.    return "0";
  211.   else {
  212.    double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
  213.    double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
  214.    if ((y - u) > 0)
  215.     return y - u + "";
  216.    else
  217.     return "0";
  218.   }
  219.  }
  220.  
  221.  /**
  222.   * 得到二个日期间的间隔天数
  223.   */
  224.  public static String getTwoDay(String sj1, String sj2) {
  225.   SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  226.   long day = 0;
  227.   try {
  228.    java.util.Date date = myFormatter.parse(sj1);
  229.    java.util.Date mydate = myFormatter.parse(sj2);
  230.    day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  231.   } catch (Exception e) {
  232.    return "";
  233.   }
  234.   return day + "";
  235.  }
  236.  
  237.  /**
  238.   * 时间前推或后推分钟,其中JJ表示分钟.
  239.   */
  240.  public static String getPreTime(String sj1, String jj) {
  241.   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  242.   String mydate1 = "";
  243.   try {
  244.    Date date1 = format.parse(sj1);
  245.    long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
  246.    date1.setTime(Time * 1000);
  247.    mydate1 = format.format(date1);
  248.   } catch (Exception e) {
  249.   }
  250.   return mydate1;
  251.  }
  252.  
  253.  /**
  254.   * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
  255.   */
  256.  public static String getNextDay(String nowdate, String delay) {
  257.   try{
  258.   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  259.   String mdate = "";
  260.   Date d = strToDate(nowdate);
  261.   long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
  262.   d.setTime(myTime * 1000);
  263.   mdate = format.format(d);
  264.   return mdate;
  265.   }catch(Exception e){
  266.    return "";
  267.   }
  268.  }
  269.  
  270.  /**
  271.   * 判断是否润年
  272.   *
  273.   * @param ddate
  274.   * @return
  275.   */
  276.  public static boolean isLeapYear(String ddate) {
  277.  
  278.   /**
  279.    * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  280.    * 3.能被4整除同时能被100整除则不是闰年
  281.    */
  282.   Date d = strToDate(ddate);
  283.   GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  284.   gc.setTime(d);
  285.   int year = gc.get(Calendar.YEAR);
  286.   if ((year % 400) == 0)
  287.    return true;
  288.   else if ((year % 4) == 0) {
  289.    if ((year % 100) == 0)
  290.     return false;
  291.    else
  292.     return true;
  293.   } else
  294.    return false;
  295.  }
  296.  
  297.  /**
  298.   * 返回美国时间格式 26 Apr 2006
  299.   *
  300.   * @param str
  301.   * @return
  302.   */
  303.  public static String getEDate(String str) {
  304.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  305.   ParsePosition pos = new ParsePosition(0);
  306.   Date strtodate = formatter.parse(str, pos);
  307.   String j = strtodate.toString();
  308.   String[] k = j.split(" ");
  309.   return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
  310.  }
  311.  
  312.  /**
  313.   * 获取一个月的最后一天
  314.   *
  315.   * @param dat
  316.   * @return
  317.   */
  318.  public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
  319.   String str = dat.substring(0, 8);
  320.   String month = dat.substring(5, 7);
  321.   int mon = Integer.parseInt(month);
  322.   if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
  323.    str += "31";
  324.   } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
  325.    str += "30";
  326.   } else {
  327.    if (isLeapYear(dat)) {
  328.     str += "29";
  329.    } else {
  330.     str += "28";
  331.    }
  332.   }
  333.   return str;
  334.  }
  335.  
  336.  /**
  337.   * 判断二个时间是否在同一个周
  338.   *
  339.   * @param date1
  340.   * @param date2
  341.   * @return
  342.   */
  343.  public static boolean isSameWeekDates(Date date1, Date date2) {
  344.   Calendar cal1 = Calendar.getInstance();
  345.   Calendar cal2 = Calendar.getInstance();
  346.   cal1.setTime(date1);
  347.   cal2.setTime(date2);
  348.   int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  349.   if (0 == subYear) {
  350.    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  351.     return true;
  352.   } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
  353.    // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  354.    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  355.     return true;
  356.   } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
  357.    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  358.     return true;
  359.   }
  360.   return false;
  361.  }
  362.  
  363.  /**
  364.   * 产生周序列,即得到当前时间所在的年度是第几周
  365.   *
  366.   * @return
  367.   */
  368.  public static String getSeqWeek() {
  369.   Calendar c = Calendar.getInstance(Locale.CHINA);
  370.   String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  371.   if (week.length() == 1)
  372.    week = "0" + week;
  373.   String year = Integer.toString(c.get(Calendar.YEAR));
  374.   return year + week;
  375.  }
  376.  
  377.  /**
  378.   * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  379.   *
  380.   * @param sdate
  381.   * @param num
  382.   * @return
  383.   */
  384.  public static String getWeek(String sdate, String num) {
  385.   // 再转换为时间
  386.   Date dd = VeDate.strToDate(sdate);
  387.   Calendar c = Calendar.getInstance();
  388.   c.setTime(dd);
  389.   if (num.equals("1")) // 返回星期一所在的日期
  390.    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  391.   else if (num.equals("2")) // 返回星期二所在的日期
  392.    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  393.   else if (num.equals("3")) // 返回星期三所在的日期
  394.    c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  395.   else if (num.equals("4")) // 返回星期四所在的日期
  396.    c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  397.   else if (num.equals("5")) // 返回星期五所在的日期
  398.    c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  399.   else if (num.equals("6")) // 返回星期六所在的日期
  400.    c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  401.   else if (num.equals("0")) // 返回星期日所在的日期
  402.    c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  403.   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  404.  }
  405.  
  406.  /**
  407.   * 根据一个日期,返回是星期几的字符串
  408.   *
  409.   * @param sdate
  410.   * @return
  411.   */
  412.  public static String getWeek(String sdate) {
  413.   // 再转换为时间
  414.   Date date = VeDate.strToDate(sdate);
  415.   Calendar c = Calendar.getInstance();
  416.   c.setTime(date);
  417.   // int hour=c.get(Calendar.DAY_OF_WEEK);
  418.   // hour中存的就是星期几了,其范围 1~7
  419.   // 1=星期日 7=星期六,其他类推
  420.   return new SimpleDateFormat("EEEE").format(c.getTime());
  421.  }
  422.  public static String getWeekStr(String sdate){
  423.   String str = "";
  424.   str = VeDate.getWeek(sdate);
  425.   if("1".equals(str)){
  426.    str = "星期日";
  427.   }else if("2".equals(str)){
  428.    str = "星期一";
  429.   }else if("3".equals(str)){
  430.    str = "星期二";
  431.   }else if("4".equals(str)){
  432.    str = "星期三";
  433.   }else if("5".equals(str)){
  434.    str = "星期四";
  435.   }else if("6".equals(str)){
  436.    str = "星期五";
  437.   }else if("7".equals(str)){
  438.    str = "星期六";
  439.   }
  440.   return str;
  441.  }
  442.  
  443.  /**
  444.   * 两个时间之间的天数
  445.   *
  446.   * @param date1
  447.   * @param date2
  448.   * @return
  449.   */
  450.  public static long getDays(String date1, String date2) {
  451.   if (date1 == null || date1.equals(""))
  452.    return 0;
  453.   if (date2 == null || date2.equals(""))
  454.    return 0;
  455.   // 转换为标准时间
  456.   SimpleDateFormat