明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1090|回复: 1

各种获取当前时间的方法

[复制链接]
发表于 2018-5-17 17:44 | 显示全部楼层 |阅读模式
引自博客 https://www.cnblogs.com/wangying222/p/5761733.html
在这里做个记录,方便查找

  1. rzsj.setText(df.format(new Date()));
  2. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
复制代码

  1. import java.util.*;
  2. import java.text.*;
  3. import java.util.Calendar;

  4. public class VeDate {
  5. /**
  6.   * 得到现在时间
  7.   *
  8.   * @return
  9.   */
  10. public static Date getNow() {
  11.   Date currentTime = new Date();
  12.   return currentTime;
  13. }

  14. /**
  15.   * 提取一个月中的最后一天
  16.   *
  17.   * @param day
  18.   * @return
  19.   */
  20. public static Date getLastDate(long day) {
  21.   Date date = new Date();
  22.   long date_3_hm = date.getTime() - 3600000 * 34 * day;
  23.   Date date_3_hm_date = new Date(date_3_hm);
  24.   return date_3_hm_date;
  25. }

  26. /**
  27.   * 得到现在时间
  28.   *
  29.   * @return 字符串 yyyyMMdd HHmmss
  30.   */
  31. public static String getStringToday() {
  32.   Date currentTime = new Date();
  33.   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
  34.   String dateString = formatter.format(currentTime);
  35.   return dateString;
  36. }

  37. /**
  38.   * 得到现在小时
  39.   */
  40. public static String getHour() {
  41.   Date currentTime = new Date();
  42.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  43.   String dateString = formatter.format(currentTime);
  44.   String hour;
  45.   hour = dateString.substring(11, 13);
  46.   return hour;
  47. }

  48. /**
  49.   * 得到现在分钟
  50.   *
  51.   * @return
  52.   */
  53. public static String getTime() {
  54.   Date currentTime = new Date();
  55.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  56.   String dateString = formatter.format(currentTime);
  57.   String min;
  58.   min = dateString.substring(14, 16);
  59.   return min;
  60. }

  61. /**
  62.   * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  63.   *
  64.   * @param sformat
  65.   *            yyyyMMddhhmmss
  66.   * @return
  67.   */
  68. public static String getUserDate(String sformat) {
  69.   Date currentTime = new Date();
  70.   SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  71.   String dateString = formatter.format(currentTime);
  72.   return dateString;
  73. }

  74. /**
  75.   * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
  76.   */
  77. public static String getTwoHour(String st1, String st2) {
  78.   String[] kk = null;
  79.   String[] jj = null;
  80.   kk = st1.split(":");
  81.   jj = st2.split(":");
  82.   if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
  83.    return "0";
  84.   else {
  85.    double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
  86.    double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
  87.    if ((y - u) > 0)
  88.     return y - u + "";
  89.    else
  90.     return "0";
  91.   }
  92. }

  93. /**
  94.   * 得到二个日期间的间隔天数
  95.   */
  96. public static String getTwoDay(String sj1, String sj2) {
  97.   SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  98.   long day = 0;
  99.   try {
  100.    java.util.Date date = myFormatter.parse(sj1);
  101.    java.util.Date mydate = myFormatter.parse(sj2);
  102.    day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  103.   } catch (Exception e) {
  104.    return "";
  105.   }
  106.   return day + "";
  107. }

  108. /**
  109.   * 时间前推或后推分钟,其中JJ表示分钟.
  110.   */
  111. public static String getPreTime(String sj1, String jj) {
  112.   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  113.   String mydate1 = "";
  114.   try {
  115.    Date date1 = format.parse(sj1);
  116.    long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
  117.    date1.setTime(Time * 1000);
  118.    mydate1 = format.format(date1);
  119.   } catch (Exception e) {
  120.   }
  121.   return mydate1;
  122. }

  123. /**
  124.   * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
  125.   */
  126. public static String getNextDay(String nowdate, String delay) {
  127.   try{
  128.   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  129.   String mdate = "";
  130.   Date d = strToDate(nowdate);
  131.   long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
  132.   d.setTime(myTime * 1000);
  133.   mdate = format.format(d);
  134.   return mdate;
  135.   }catch(Exception e){
  136.    return "";
  137.   }
  138. }

  139. /**
  140.   * 判断是否润年
  141.   *
  142.   * @param ddate
  143.   * @return
  144.   */
  145. public static boolean isLeapYear(String ddate) {

  146.   /**
  147.    * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  148.    * 3.能被4整除同时能被100整除则不是闰年
  149.    */
  150.   Date d = strToDate(ddate);
  151.   GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  152.   gc.setTime(d);
  153.   int year = gc.get(Calendar.YEAR);
  154.   if ((year % 400) == 0)
  155.    return true;
  156.   else if ((year % 4) == 0) {
  157.    if ((year % 100) == 0)
  158.     return false;
  159.    else
  160.     return true;
  161.   } else
  162.    return false;
  163. }

  164. /**
  165.   * 返回美国时间格式 26 Apr 2006
  166.   *
  167.   * @param str
  168.   * @return
  169.   */
  170. public static String getEDate(String str) {
  171.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  172.   ParsePosition pos = new ParsePosition(0);
  173.   Date strtodate = formatter.parse(str, pos);
  174.   String j = strtodate.toString();
  175.   String[] k = j.split(" ");
  176.   return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
  177. }

  178. /**
  179.   * 获取一个月的最后一天
  180.   *
  181.   * @param dat
  182.   * @return
  183.   */
  184. public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
  185.   String str = dat.substring(0, 8);
  186.   String month = dat.substring(5, 7);
  187.   int mon = Integer.parseInt(month);
  188.   if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
  189.    str += "31";
  190.   } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
  191.    str += "30";
  192.   } else {
  193.    if (isLeapYear(dat)) {
  194.     str += "29";
  195.    } else {
  196.     str += "28";
  197.    }
  198.   }
  199.   return str;
  200. }

  201. /**
  202.   * 判断二个时间是否在同一个周
  203.   *
  204.   * @param date1
  205.   * @param date2
  206.   * @return
  207.   */
  208. public static boolean isSameWeekDates(Date date1, Date date2) {
  209.   Calendar cal1 = Calendar.getInstance();
  210.   Calendar cal2 = Calendar.getInstance();
  211.   cal1.setTime(date1);
  212.   cal2.setTime(date2);
  213.   int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  214.   if (0 == subYear) {
  215.    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  216.     return true;
  217.   } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
  218.    // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  219.    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  220.     return true;
  221.   } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
  222.    if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
  223.     return true;
  224.   }
  225.   return false;
  226. }

  227. /**
  228.   * 产生周序列,即得到当前时间所在的年度是第几周
  229.   *
  230.   * @return
  231.   */
  232. public static String getSeqWeek() {
  233.   Calendar c = Calendar.getInstance(Locale.CHINA);
  234.   String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  235.   if (week.length() == 1)
  236.    week = "0" + week;
  237.   String year = Integer.toString(c.get(Calendar.YEAR));
  238.   return year + week;
  239. }

  240. /**
  241.   * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  242.   *
  243.   * @param sdate
  244.   * @param num
  245.   * @return
  246.   */
  247. public static String getWeek(String sdate, String num) {
  248.   // 再转换为时间
  249.   Date dd = VeDate.strToDate(sdate);
  250.   Calendar c = Calendar.getInstance();
  251.   c.setTime(dd);
  252.   if (num.equals("1")) // 返回星期一所在的日期
  253.    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  254.   else if (num.equals("2")) // 返回星期二所在的日期
  255.    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  256.   else if (num.equals("3")) // 返回星期三所在的日期
  257.    c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  258.   else if (num.equals("4")) // 返回星期四所在的日期
  259.    c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  260.   else if (num.equals("5")) // 返回星期五所在的日期
  261.    c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  262.   else if (num.equals("6")) // 返回星期六所在的日期
  263.    c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  264.   else if (num.equals("0")) // 返回星期日所在的日期
  265.    c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  266.   return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  267. }

  268. /**
  269.   * 根据一个日期,返回是星期几的字符串
  270.   *
  271.   * @param sdate
  272.   * @return
  273.   */
  274. public static String getWeek(String sdate) {
  275.   // 再转换为时间
  276.   Date date = VeDate.strToDate(sdate);
  277.   Calendar c = Calendar.getInstance();
  278.   c.setTime(date);
  279.   // int hour=c.get(Calendar.DAY_OF_WEEK);
  280.   // hour中存的就是星期几了,其范围 1~7
  281.   // 1=星期日 7=星期六,其他类推
  282.   return new SimpleDateFormat("EEEE").format(c.getTime());
  283. }
  284. public static String getWeekStr(String sdate){
  285.   String str = "";
  286.   str = VeDate.getWeek(sdate);
  287.   if("1".equals(str)){
  288.    str = "星期日";
  289.   }else if("2".equals(str)){
  290.    str = "星期一";
  291.   }else if("3".equals(str)){
  292.    str = "星期二";
  293.   }else if("4".equals(str)){
  294.    str = "星期三";
  295.   }else if("5".equals(str)){
  296.    str = "星期四";
  297.   }else if("6".equals(str)){
  298.    str = "星期五";
  299.   }else if("7".equals(str)){
  300.    str = "星期六";
  301.   }
  302.   return str;
  303. }

  304. /**
  305.   * 两个时间之间的天数
  306.   *
  307.   * @param date1
  308.   * @param date2
  309.   * @return
  310.   */
  311. public static long getDays(String date1, String date2) {
  312.   if (date1 == null || date1.equals(""))
  313.    return 0;
  314.   if (date2 == null || date2.equals(""))
  315.    return 0;
  316.   // 转换为标准时间
  317.   SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  318.   java.util.Date date = null;
  319.   java.util.Date mydate = null;
  320.   try {
  321.    date = myFormatter.parse(date1);
  322.    mydate = myFormatter.parse(date2);
  323.   } catch (Exception e) {
  324.   }
  325.   long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  326.   return day;
  327. }

  328. /**
  329.   * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  330.   * 此函数返回该日历第一行星期日所在的日期
  331.   *
  332.   * @param sdate
  333.   * @return
  334.   */
  335. public static String getNowMonth(String sdate) {
  336.   // 取该时间所在月的一号
  337.   sdate = sdate.substring(0, 8) + "01";

  338.   // 得到这个月的1号是星期几
  339.   Date date = VeDate.strToDate(sdate);
  340.   Calendar c = Calendar.getInstance();
  341.   c.setTime(date);
  342.   int u = c.get(Calendar.DAY_OF_WEEK);
  343.   String newday = VeDate.getNextDay(sdate, (1 - u) + "");
  344.   return newday;
  345. }

  346. /**
  347.   * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
  348.   *
  349.   * @param k
  350.   *            表示是取几位随机数,可以自己定
  351.   */

  352. public static String getNo(int k) {

  353.   return getUserDate("yyyyMMddhhmmss") + getRandom(k);
  354. }

  355. /**
  356.   * 返回一个随机数
  357.   *
  358.   * @param i
  359.   * @return
  360.   */
  361. public static String getRandom(int i) {
  362.   Random jjj = new Random();
  363.   // int suiJiShu = jjj.nextInt(9);
  364.   if (i == 0)
  365.    return "";
  366.   String jj = "";
  367.   for (int k = 0; k < i; k++) {
  368.    jj = jj + jjj.nextInt(9);
  369.   }
  370.   return jj;
  371. }

  372. /**
  373.   *
  374.   * @param args
  375.   */
  376. public static boolean RightDate(String date) {

  377.   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  378.   ;
  379.   if (date == null)
  380.    return false;
  381.   if (date.length() > 10) {
  382.    sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  383.   } else {
  384.    sdf = new SimpleDateFormat("yyyy-MM-dd");
  385.   }
  386.   try {
  387.    sdf.parse(date);
  388.   } catch (ParseException pe) {
  389.    return false;
  390.   }
  391.   return true;
  392. }

  393. /***************************************************************************
  394.   * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1
  395.   * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回
  396.   **************************************************************************/
  397. public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {
  398.   Date currentTime = new Date();
  399.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  400.   String dateString = formatter.format(currentTime);
  401.   String s_nd = dateString.substring(0, 4); // 年份
  402.   String s_yf = dateString.substring(5, 7); // 月份
  403.   String s_rq = dateString.substring(8, 10); // 日期
  404.   String sreturn = "";
  405.   roc.util.MyChar mc = new roc.util.MyChar();
  406.   if (sdate == null || sdate.equals("") || !mc.Isdate(sdate)) { // 处理空值情况
  407.    if (nd.equals("1")) {
  408.     sreturn = s_nd;
  409.     // 处理间隔符
  410.     if (format.equals("1"))
  411.      sreturn = sreturn + "年";
  412.     else if (format.equals("2"))
  413.      sreturn = sreturn + "-";
  414.     else if (format.equals("3"))
  415.      sreturn = sreturn + "/";
  416.     else if (format.equals("5"))
  417.      sreturn = sreturn + ".";
  418.    }
  419.    // 处理月份
  420.    if (yf.equals("1")) {
  421.     sreturn = sreturn + s_yf;
  422.     if (format.equals("1"))
  423.      sreturn = sreturn + "月";
  424.     else if (format.equals("2"))
  425.      sreturn = sreturn + "-";
  426.     else if (format.equals("3"))
  427.      sreturn = sreturn + "/";
  428.     else if (format.equals("5"))
  429.      sreturn = sreturn + ".";
  430.    }
  431.    // 处理日期
  432.    if (rq.equals("1")) {
  433.     sreturn = sreturn + s_rq;
  434.     if (format.equals("1"))
  435.      sreturn = sreturn + "日";
  436.    }
  437.   } else {
  438.    // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
  439.    sdate = roc.util.RocDate.getOKDate(sdate);
  440.    s_nd = sdate.substring(0, 4); // 年份
  441.    s_yf = sdate.substring(5, 7); // 月份
  442.    s_rq = sdate.substring(8, 10); // 日期
  443.    if (nd.equals("1")) {
  444.     sreturn = s_nd;
  445.     // 处理间隔符
  446.     if (format.equals("1"))
  447.      sreturn = sreturn + "年";
  448.     else if (format.equals("2"))
  449.      sreturn = sreturn + "-";
  450.     else if (format.equals("3"))
  451.      sreturn = sreturn + "/";
  452.     else if (format.equals("5"))
  453.      sreturn = sreturn + ".";
  454.    }
  455.    // 处理月份
  456.    if (yf.equals("1")) {
  457.     sreturn = sreturn + s_yf;
  458.     if (format.equals("1"))
  459.      sreturn = sreturn + "月";
  460.     else if (format.equals("2"))
  461.      sreturn = sreturn + "-";
  462.     else if (format.equals("3"))
  463.      sreturn = sreturn + "/";
  464.     else if (format.equals("5"))
  465.      sreturn = sreturn + ".";
  466.    }
  467.    // 处理日期
  468.    if (rq.equals("1")) {
  469.     sreturn = sreturn + s_rq;
  470.     if (format.equals("1"))
  471.      sreturn = sreturn + "日";
  472.    }
  473.   }
  474.   return sreturn;
  475. }

  476. public static String getNextMonthDay(String sdate, int m) {
  477.   sdate = getOKDate(sdate);
  478.   int year = Integer.parseInt(sdate.substring(0, 4));
  479.   int month = Integer.parseInt(sdate.substring(5, 7));
  480.   month = month + m;
  481.   if (month < 0) {
  482.    month = month + 12;
  483.    year = year - 1;
  484.   } else if (month > 12) {
  485.    month = month - 12;
  486.    year = year + 1;
  487.   }
  488.   String smonth = "";
  489.   if (month < 10)
  490.    smonth = "0" + month;
  491.   else
  492.    smonth = "" + month;
  493.   return year + "-" + smonth + "-10";
  494. }

  495. public static String getOKDate(String sdate) {
  496.   if (sdate == null || sdate.equals(""))
  497.    return getStringDateShort();

  498.   if (!VeStr.Isdate(sdate)) {
  499.    sdate = getStringDateShort();
  500.   }
  501.   // 将“/”转换为“-”
  502.   sdate = VeStr.Replace(sdate, "/", "-");
  503.   // 如果只有8位长度,则要进行转换
  504.   if (sdate.length() == 8)
  505.    sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);
  506.   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  507.   ParsePosition pos = new ParsePosition(0);
  508.   Date strtodate = formatter.parse(sdate, pos);
  509.   String dateString = formatter.format(strtodate);
  510.   return dateString;
  511. }

  512. public static void main(String[] args) throws Exception {
  513.   try {
  514.    //System.out.print(Integer.valueOf(getTwoDay("2006-11-03 12:22:10", "2006-11-02 11:22:09")));
  515.   } catch (Exception e) {
  516.    throw new Exception();
  517.   }
  518.   //System.out.println("sss");
  519. }
复制代码



发表于 2018-5-18 09:16 | 显示全部楼层
嗯,不错,收藏了,谢谢分享!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-25 17:35 , Processed in 0.426024 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表