现在的位置: 首页 > 综合 > 正文

非常有用的Java日期时间操作函数代码一览 | 中文Flex例子

2013年11月22日 ⁄ 综合 ⁄ 共 5570字 ⁄ 字号 评论关闭

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



  1. /**
  2. 日期类
  3. * @date  
  4. * @version 1.0
  5. */
  6. import java.util.*;
  7. import java.text.*;
  8. import java.util.Calendar;

  9. public class VeDate {
  10.  /**
  11.   * 获取现在时间
  12.   *
  13.   * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
  14.   */
  15.  public static Date getNowDate() {
  16.   Date currentTime = new Date();
  17.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  18.   String dateString = formatter.format(currentTime);
  19.   ParsePosition pos = new ParsePosition(8);
  20.   Date currentTime_2 = formatter.parse(dateString, pos);
  21.   return currentTime_2;
  22.  }

  23.  /**
  24.   * 获取现在时间
  25.   *
  26.   * @return返回短时间格式 yyyy-MM-dd
  27.   */
  28.  public static Date getNowDateShort() {
  29.   Date currentTime = new Date();
  30.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  31.   String dateString = formatter.format(currentTime);
  32.   ParsePosition pos = new ParsePosition(8);
  33.   Date currentTime_2 = formatter.parse(dateString, pos);
  34.   return currentTime_2;
  35.  }

  36.  /**
  37.   * 获取现在时间
  38.   *
  39.   * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
  40.   */
  41.  public static String getStringDate() {
  42.   Date currentTime = new Date();
  43.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  44.   String dateString = formatter.format(currentTime);
  45.   return dateString;
  46.  }

  47.  /**
  48.   * 获取现在时间
  49.   *
  50.   * @return 返回短时间字符串格式yyyy-MM-dd
  51.   */
  52.  public static String getStringDateShort() {
  53.   Date currentTime = new Date();
  54.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  55.   String dateString = formatter.format(currentTime);
  56.   return dateString;
  57.  }

  58.  /**
  59.   * 获取时间 小时:分;秒 HH:mm:ss
  60.   *
  61.   * @return
  62.   */
  63.  public static String getTimeShort() {
  64.   SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  65.   Date currentTime = new Date();
  66.   String dateString = formatter.format(currentTime);
  67.   return dateString;
  68.  }

  69.  /**
  70.   * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
  71.   *
  72.   * @param strDate
  73.   * @return
  74.   */
  75.  public static Date strToDateLong(String strDate) {
  76.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  77.   ParsePosition pos = new ParsePosition(0);
  78.   Date strtodate = formatter.parse(strDate, pos);
  79.   return strtodate;
  80.  }

  81.  /**
  82.   * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
  83.   *
  84.   * @param dateDate
  85.   * @return
  86.   */
  87.  public static String dateToStrLong(java.util.Date dateDate) {
  88.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  89.   String dateString = formatter.format(dateDate);
  90.   return dateString;
  91.  }

  92.  /**
  93.   * 将短时间格式时间转换为字符串 yyyy-MM-dd
  94.   *
  95.   * @param dateDate
  96.   * @param k
  97.   * @return
  98.   */
  99.  public static String dateToStr(java.util.Date dateDate) {
  100.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  101.   String dateString = formatter.format(dateDate);
  102.   return dateString;
  103.  }

  104.  /**
  105.   * 将短时间格式字符串转换为时间 yyyy-MM-dd
  106.   *
  107.   * @param strDate
  108.   * @return
  109.   */
  110.  public static Date strToDate(String strDate) {
  111.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  112.   ParsePosition pos = new ParsePosition(0);
  113.   Date strtodate = formatter.parse(strDate, pos);
  114.   return strtodate;
  115.  }

  116.  /**
  117.   * 得到现在时间
  118.   *
  119.   * @return
  120.   */
  121.  public static Date getNow() {
  122.   Date currentTime = new Date();
  123.   return currentTime;
  124.  }

  125.  /**
  126.   * 提取一个月中的最后一天
  127.   *
  128.   * @param day
  129.   * @return
  130.   */
  131.  public static Date getLastDate(long day) {
  132.   Date date = new Date();
  133.   long date_3_hm = date.getTime() - 3600000 * 34 * day;
  134.   Date date_3_hm_date = new Date(date_3_hm);
  135.   return date_3_hm_date;
  136.  }

  137.  /**
  138.   * 得到现在时间
  139.   *
  140.   * @return 字符串 yyyyMMdd HHmmss
  141.   */
  142.  public static String getStringToday() {
  143.   Date currentTime = new Date();
  144.   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  145.   String dateString = formatter.format(currentTime);
  146.   return dateString;
  147.  }

  148.  /**
  149.   * 得到现在小时
  150.   */
  151.  public static String getHour() {
  152.   Date currentTime = new Date();
  153.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  154.   String dateString = formatter.format(currentTime);
  155.   String hour;
  156.   hour = dateString.substring(11, 13);
  157.   return hour;
  158.  }

  159.  /**
  160.   * 得到现在分钟
  161.   *
  162.   * @return
  163.   */
  164.  public static String getTime() {
  165.   Date currentTime = new Date();
  166.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  167.   String dateString = formatter.format(currentTime);
  168.   String min;
  169.   min = dateString.substring(14, 16);
  170.   return min;
  171.  }

  172.  /**
  173.   * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  174.   *
  175.   * @param sformat
  176.   *            yyyyMMddhhmmss
  177.   * @return
  178.   */
  179.  public static String getUserDate(String sformat) {
  180.   Date currentTime = new Date();
  181.   SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  182.   String dateString = formatter.format(currentTime);
  183.   return dateString;
  184.  }

  185.  /**
  186.   * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
  187.   */
  188.  public static String getTwoHour(String st1, String st2) {
  189.   String[] kk = null;
  190.   String[] jj = null;
  191.   kk = st1.split(":");
  192.   jj = st2.split(":");
  193.   if (Integer.parseInt(kk[0]) Integer.parseInt(jj[0]))
  194.    return "0";
  195.   else {
  196.    double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
  197.    double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
  198.    if ((y - u) > 0)
  199.     return y - u + "";
  200.    else
  201.     return "0";
  202.   }
  203.  }

  204.  /**
  205.   * 得到二个日期间的间隔天数
  206.   */
  207.  public static String getTwoDay(String sj1, String sj2) {
  208.   SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  209.   long day = 0;
  210.   try {
  211.    java.util.Date date = myFormatter.parse(sj1);
  212.    java.util.Date mydate = myFormatter.parse(sj2);
  213.    day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  214.   } catch (Exception e) {
  215.    return "";
  216.   }
  217.   return day + "";
  218.  }

  219.  /**
  220.   * 时间前推或后推分钟,其中JJ表示分钟.
  221.   */
  222.  public static String getPreTime(String sj1, String jj) {
  223.   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  224.   String mydate1 = "";
  225.   try {
  226.    Date date1 = format.parse(sj1);
  227.    long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
  228.    date1.setTime(Time * 1000);
  229.    mydate1 = format.format(date1);
  230.   } catch (Exception e) {
  231.   }
  232.   return mydate1;
  233.  }

  234.  /**
  235.   * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
  236.   */
  237.  public static String getNextDay(String nowdate, String delay) {
  238.   try{
  239.   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  240.   String mdate = "";
  241.   Date d = strToDate(nowdate);
  242.   long myTime = (d.getTime() / 1000) + Integer.parseInt(delay)

抱歉!评论已关闭.