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

JAVA基础篇--Java 日期时间 JAVA基础篇日期和时间 阅读详情

今天写的一个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 myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  457.   java.util.Date date = null;
  458.   java.util.Date mydate = null;
  459.   try {
  460.    date = myFormatter.parse(date1);
  461.    mydate = myFormatter.parse(date2);
  462.   } catch (Exception e) {
  463.   }
  464.   long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  465.   return day;
  466.  }
  467.  
  468.  /**
  469.   * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  470.   * 此函数返回该日历第一行星期日所在的日期
  471.   *
  472.   * @param sdate
  473.   * @return
  474.   */
  475.  public static String getNowMonth(String sdate) {
  476.   // 取该时间所在月的一号
  477.   sdate = sdate.substring(0, 8) + "01";
  478.  
  479.   // 得到这个月的1号是星期几
  480.   Date date = VeDate.strToDate(sdate);
  481.   Calendar c = Calendar.getInstance();
  482.   c.setTime(date);
  483.   int u = c.get(Calendar.DAY_OF_WEEK);
  484.   String newday = VeDate.getNextDay(sdate, (1 - u) + "");
  485.   return newday;
  486.  }
  487.  
  488.  /**
  489.   * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  490.   *
  491.   * @param k
  492.   *            表示是取几位随机数,可以自己定
  493.   */
  494.  
  495.  public static String getNo(int k) {
  496.  
  497.   return getUserDate("yyyyMMddhhmmss") + getRandom(k);
  498.  }
  499.  
  500.  /**
  501.   * 返回一个随机数
  502.   *
  503.   * @param i
  504.   * @return
  505.   */
  506.  public static String getRandom(int i) {
  507.   Random jjj = new Random();
  508.   // int suiJiShu = jjj.nextInt(9);
  509.   if (i == 0)
  510.    return "";
  511.   String jj = "";
  512.   for (int k = 0; k < i; k++) {
  513.    jj = jj + jjj.nextInt(9);
  514.   }
  515.   return jj;
  516.  }
  517.  
  518.  /**
  519.   *
  520.   * @param args
  521.   */
  522.  public static boolean RightDate(String date) {
  523.  
  524.   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  525.   ;
  526.   if (date == null)
  527.    return false;
  528.   if (date.length() > 10) {
  529.    sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  530.   } else {
  531.    sdf = new SimpleDateFormat("yyyy-MM-dd");
  532.   }
  533.   try {
  534.    sdf.parse(date);
  535.   } catch (ParseException pe) {
  536.    return false;
  537.   }
  538.   return true;
  539.  }
  540.  
  541.  /***************************************************************************
  542.   * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1
  543.   * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回
  544.   **************************************************************************/
  545.  public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {
  546.   Date currentTime = new Date();
  547.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  548.   String dateString = formatter.format(currentTime);
  549.   String s_nd = dateString.substring(0, 4); // 年份
  550.   String s_yf = dateString.substring(5, 7); // 月份
  551.   String s_rq = dateString.substring(8, 10); // 日期
  552.   String sreturn = "";
  553.   roc.util.MyChar mc = new roc.util.MyChar();
  554.   if (sdate == null || sdate.equals("") || !mc.Isdate(sdate)) { // 处理空值情况
  555.    if (nd.equals("1")) {
  556.     sreturn = s_nd;
  557.     // 处理间隔符
  558.     if (format.equals("1"))
  559.      sreturn = sreturn + "年";
  560.     else if (format.equals("2"))
  561.      sreturn = sreturn + "-";
  562.     else if (format.equals("3"))
  563.      sreturn = sreturn + "/";
  564.     else if (format.equals("5"))
  565.      sreturn = sreturn + ".";
  566.    }
  567.    // 处理月份
  568.    if (yf.equals("1")) {
  569.     sreturn = sreturn + s_yf;
  570.     if (format.equals("1"))
  571.      sreturn = sreturn + "月";
  572.     else if (format.equals("2"))
  573.      sreturn = sreturn + "-";
  574.     else if (format.equals("3"))
  575.      sreturn = sreturn + "/";
  576.     else if (format.equals("5"))
  577.      sreturn = sreturn + ".";
  578.    }
  579.    // 处理日期
  580.    if (rq.equals("1")) {
  581.     sreturn = sreturn + s_rq;
  582.     if (format.equals("1"))
  583.      sreturn = sreturn + "日";
  584.    }
  585.   } else {
  586.    // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
  587.    sdate = roc.util.RocDate.getOKDate(sdate);
  588.    s_nd = sdate.substring(0, 4); // 年份
  589.    s_yf = sdate.substring(5, 7); // 月份
  590.    s_rq = sdate.substring(8, 10); // 日期
  591.    if (nd.equals("1")) {
  592.     sreturn = s_nd;
  593.     // 处理间隔符
  594.     if (format.equals("1"))
  595.      sreturn = sreturn + "年";
  596.     else if (format.equals("2"))
  597.      sreturn = sreturn + "-";
  598.     else if (format.equals("3"))
  599.      sreturn = sreturn + "/";
  600.     else if (format.equals("5"))
  601.      sreturn = sreturn + ".";
  602.    }
  603.    // 处理月份
  604.    if (yf.equals("1")) {
  605.     sreturn = sreturn + s_yf;
  606.     if (format.equals("1"))
  607.      sreturn = sreturn + "月";
  608.     else if (format.equals("2"))
  609.      sreturn = sreturn + "-";
  610.     else if (format.equals("3"))
  611.      sreturn = sreturn + "/";
  612.     else if (format.equals("5"))
  613.      sreturn = sreturn + ".";
  614.    }
  615.    // 处理日期
  616.    if (rq.equals("1")) {
  617.     sreturn = sreturn + s_rq;
  618.     if (format.equals("1"))
  619.      sreturn = sreturn + "日";
  620.    }
  621.   }
  622.   return sreturn;
  623.  }
  624.  
  625.  public static String getNextMonthDay(String sdate, int m) {
  626.   sdate = getOKDate(sdate);
  627.   int year = Integer.parseInt(sdate.substring(0, 4));
  628.   int month = Integer.parseInt(sdate.substring(5, 7));
  629.   month = month + m;
  630.   if (month < 0) {
  631.    month = month + 12;
  632.    year = year - 1;
  633.   } else if (month > 12) {
  634.    month = month - 12;
  635.    year = year + 1;
  636.   }
  637.   String smonth = "";
  638.   if (month < 10)
  639.    smonth = "0" + month;
  640.   else
  641.    smonth = "" + month;
  642.   return year + "-" + smonth + "-10";
  643.  }
  644.  
  645.  public static String getOKDate(String sdate) {
  646.   if (sdate == null || sdate.equals(""))
  647.    return getStringDateShort();
  648.  
  649.   if (!VeStr.Isdate(sdate)) {
  650.    sdate = getStringDateShort();
  651.   }
  652.   // 将“/”转换为“-”
  653.   sdate = VeStr.Replace(sdate, "/", "-");
  654.   // 如果只有8位长度,则要进行转换
  655.   if (sdate.length() == 8)
  656.    sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);
  657.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  658.   ParsePosition pos = new ParsePosition(0);
  659.   Date strtodate = formatter.parse(sdate, pos);
  660.   String dateString = formatter.format(strtodate);
  661.   return dateString;
  662.  }
  663.  
  664.  public static void main(String[] args) throws Exception {
  665.   try {
  666.    //System.out.print(Integer.valueOf(getTwoDay("2006-11-03 12:22:10", "2006-11-02 11:22:09")));
  667.   } catch (Exception e) {
  668.    throw new Exception();
  669.   }
  670.   //System.out.println("sss");
  671.  }
  672. }
有了这个,时间转换从此没问题 /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ public static Date getNowDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(currentTime); ParsePosition pos = new ParsePosition(8); Date currentTime_2 = formatter.parse(dateString, pos); return currentTime_2; } /** * 获取现在时间 * * @return返回短时间格式 yyyy-MM-dd */ public static Date getNowDateShort() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(currentTime); ParsePosition pos = new ParsePosition(8); Date currentTime_2 = formatter.parse(dateString, pos); return currentTime_2; } 立即下载

相关推荐

STM32H743IIT6+USB3300高速HID通讯实战:从CUBEMX配置到调试避坑全记录

本文详细记录了基于STM32H743IIT6微控制器与USB3300 PHY芯片实现高速USB HID通信的完整实战过程。内容涵盖从硬件选型、STM32CubeMX工程配置(重点规避引脚映射等常见陷阱)、自定义报告描述符编写,到驱动层数据收发实现与高级调试技巧的全链路解析,旨在帮助开发者快速构建稳定、高效的嵌入式高速数据通道。

vscode5coder的博客 362

收藏到一个好的java时间格式大全(类)

http://www.busfly.cn/post/java_string_date_time_class_all.html(原文) 又收藏到一个好的java时间格式大全,这个是做成了类的 java.util.*; import java.text.*; import java.util.Calendar; public class VeDate { /** * 获...

eminem 7241

安卓无障碍服务保活实战:如何用AccessibilityService实现后台常驻(附完整代码)

本文深入解析如何利用Android无障碍服务(AccessibilityService)实现后台服务保活,涵盖多版本系统适配、前台服务集成、开机自启机制等关键技术。通过详细的Kotlin代码示例和合规性指导,帮助开发者构建稳定可靠的后台服务解决方案,同时避免因滥用无障碍服务导致的应用下架风险。

milk5的博客 1071

Java时间日期格式转换

Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ public static Date getNowDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new S.

baidu_28705493的博客 4102

Zynq裸机调试RTL8211FS网卡踩坑记:从ping不通到完美兼容的完整代码修改

本文详细记录了在Zynq平台上使用Xilinx SDK(XSDK)调试RTL8211FS网卡的完整过程,从最初的ping不通问题到最终实现完美兼容的解决方案。通过分析PHY芯片的寄存器配置差异,提供了关键代码修改和调试技巧,帮助开发者快速解决类似问题。

weixin_42550597的博客 419

Django框架中settings文件的部署建议

为了更好地管理开发和生产环境的配置,建议将settings文件分为多个文件,分别用于不同的环境。然后,通过在启动应用程序时指定不同的settings文件,来加载相应的配置。在Django框架中,settings.py文件是非常重要的配置文件,它包含了项目的各种设置和选项。在Django框架中,settings.py文件是非常重要的配置文件,它包含了项目的各种设置和选项。可以使用Python的。为了更好地管理开发和生产环境的配置,建议将settings文件分为多个文件,分别用于不同的环境。

WELL_CODER的博客 128

java时间函数

 http://blog.sina.com.cn/s/blog_503cf9f80100b9lf.htmljava时间函数(2008-12-06 22:25:46)标签:杂谈  注意:java.util和java.sql中都有Date这个类,不知道用哪一个了,你可以写死java.sql.Date time = .....1.

FastThinking的专栏 1246

YOLO26模型应用过程中的报错处理及疑问解答,涉及环境搭建、模型训练、模块改进、论文写作

本文记录了YOLO26模型的这一系列过程中可能发生的问题,以及大家非常关心的问题,这里统一做了一个记录和解答。

Limiiiing的博客 239

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

<br />/** 日期类 * @date * @version 1.0 */ import java.util.*; import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ public static Date getNowDate

wenlele的专栏 681

Python 爬虫实战:华尔街见闻精选文章爬取(反反爬 + 投资情绪分析)

Python 爬虫实战:华尔街见闻精选文章爬取(反反爬 + 投资情绪分析)

yansideyucsdn的博客 969

CCAR-23《正常类飞机适航规定》和CCAR-25《运输类飞机适航规定》差异分析

通过详细比较CCAR-23和CCAR-25在适用范围、设计标准、性能要求、运行限制、安全冗余和环境适应性方面的差异,我们可以看出,CCAR-23适用于轻型飞机,开发和运营成本较低,设计要求相对简单。而CCAR-25适用于大规模运输飞机,设计、开发、测试和运营管理都需要更高的复杂性和投入。

qq_41854911的博客 3703

java取服务器时间的函数,非常有用的Java日期时间操作函数代码一览

/**日期类* @date* @version 1.0*/importjava.util.*;importjava.text.*;importjava.util.Calendar;publicclass VeDate {/*** 获取现在时间** @return 返回时间类型 yyyy-MM-dd HH:mm:ss*/public static Date getNowDate() {Dat...

weixin_31080559的博客 467

记录Android Studio连接华为(nova)手机鸿蒙系统踩过的坑

鸿蒙系统的华为老机子比较麻烦,坑有点多,兄弟们加油!

沉迷单车的追风少年 7920

java日期函数精确到日,java日期时间函数分享

前言:对于新手程序员的我来说,写业务代码是现在的日常,在此过程中经常需要对日期时间进行处理,我挑了几个较有用的日期处理函数分享给大家。正文:1、将某格式的时间字符串转化成毫秒时间戳表示的字符串:public static String dateTimeStrToMills(String dateTime,String format){String dateStr = null;SimpleDate...

weixin_31181381的博客 1453

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

MySQL常用的日期时间函数

*返回当前日期和时间**CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP,LOCALTIME(),LOCALTIME,LOCALTIMESTAMP(),LOCALTIMESTAMP,NOW(),SYSDATE()**返回当前日期**CURDATE(),CURRENT_DATE(),CURRENT_DATE。**返回当前时间**CURTIME(),CURRENT_TIME(),CURRENT_TIME。**YEAR()函数**取日期时间的年份。...

m0_67391683的博客 4384

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Java 日期时间

java.util 包提供了 Date 类来封装当前的日期和时间。Date 类提供两个构造函数来实例化 Date 对象。第一个构造函数使用当前日期和时间来初始化对象。Date( )第二个构造函数接收一个参数,该参数是从 1970 年 1 月 1 日起的毫秒数。Date 对象创建以后,可以调用下面的方法。

Aforxiaoxie 865

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Java处理日期时间API详解

较为详细的介绍了Java中处理日期时间的一些API,并有使用示例。

qq_53986627的博客 3257

我奶奶都能懂java8特性-日期时间

java每个版本的更新,都会给java使用者极大的方便。其中,java8中的日期时间较之前的版本的进步的不是一个数量级。借着这次针对java8基础的考试,本博客对两个版本的日期时间进行比较。

浦江之猿的博客 1670
上一篇: iBATIS教程之快速入门浅析
下一篇: Internet Explorer无法打开internet站点,已终止操作的解决方法合集
escode
博客等级 码龄22年 33粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值