[Java] Java获取系统当前时间的方法并指定格式 →→→→→进入此内容的聊天室

来自 , 2020-06-14, 写在 Java, 查看 169 次.
URL http://www.code666.cn/view/f228bda6
  1. /**
  2.          * 返回当前日期时间字符串<br>
  3.          * 默认格式:yyyy-mm-dd hh:mm:ss
  4.          *
  5.          * @return String 返回当前字符串型日期时间
  6.          */
  7.         public static String getCurrentTime() {
  8.                 String returnStr = null;
  9.                 SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  10.                 Date date = new Date();
  11.                 returnStr = f.format(date);
  12.                 return returnStr;
  13.         }
  14.        
  15.         /**
  16.          * 返回当前日期时间字符串<br>
  17.          * 默认格式:yyyymmddhhmmss
  18.          *
  19.          * @return String 返回当前字符串型日期时间
  20.          */
  21.         public static BigDecimal getCurrentTimeAsNumber() {
  22.                 String returnStr = null;
  23.                 SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
  24.                 Date date = new Date();
  25.                 returnStr = f.format(date);
  26.                 return new BigDecimal(returnStr);
  27.         }
  28.  
  29.  
  30.         /**
  31.          * 返回自定义格式的当前日期时间字符串
  32.          *
  33.          * @param format
  34.          *            格式规则
  35.          * @return String 返回当前字符串型日期时间
  36.          */
  37.         public static String getCurrentTime(String format) {
  38.                 String returnStr = null;
  39.                 SimpleDateFormat f = new SimpleDateFormat(format);
  40.                 Date date = new Date();
  41.                 returnStr = f.format(date);
  42.                 return returnStr;
  43.         }
  44.  
  45.         /**
  46.          * 返回当前字符串型日期
  47.          *
  48.          * @return String 返回的字符串型日期
  49.          */
  50.         public static String getCurDate() {
  51.                 Calendar calendar = Calendar.getInstance();
  52.                 SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
  53.                 String strDate = simpledateformat.format(calendar.getTime());
  54.                 return strDate;
  55.         }
  56.  
  57.         /**
  58.          * 返回指定格式的字符型日期
  59.          * @param date
  60.          * @param formatString
  61.          * @return
  62.          */
  63.         public static String Date2String(Date date, String formatString) {
  64.                 if (G4Utils.isEmpty(date)) {
  65.                         return null;
  66.                 }
  67.                 SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);
  68.                 String strDate = simpledateformat.format(date);
  69.                 return strDate;
  70.         }
  71.        
  72.         /**
  73.          * 返回当前字符串型日期
  74.          *
  75.          * @param format
  76.          *            格式规则
  77.          *
  78.          * @return String 返回的字符串型日期
  79.          */
  80.         public static String getCurDate(String format) {
  81.                 Calendar calendar = Calendar.getInstance();
  82.                 SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
  83.                 String strDate = simpledateformat.format(calendar.getTime());
  84.                 return strDate;
  85.         }
  86.  
  87.         /**
  88.          * 返回TimeStamp对象
  89.          *
  90.          * @return
  91.          */
  92.         public static Timestamp getCurrentTimestamp() {
  93.                 Object obj = TypeCaseHelper.convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");
  94.                 if (obj != null)
  95.                         return (Timestamp) obj;
  96.                 else
  97.                         return null;
  98.         }
  99.  
  100.         /**
  101.          * 将字符串型日期转换为日期型
  102.          *
  103.          * @param strDate
  104.          *            字符串型日期
  105.          * @param srcDateFormat
  106.          *            源日期格式
  107.          * @param dstDateFormat
  108.          *            目标日期格式
  109.          * @return Date 返回的util.Date型日期
  110.          */
  111.         public static Date stringToDate(String strDate, String srcDateFormat, String dstDateFormat) {
  112.                 Date rtDate = null;
  113.                 Date tmpDate = (new SimpleDateFormat(srcDateFormat)).parse(strDate, new ParsePosition(0));
  114.                 String tmpString = null;
  115.                 if (tmpDate != null)
  116.                         tmpString = (new SimpleDateFormat(dstDateFormat)).format(tmpDate);
  117.                 if (tmpString != null)
  118.                         rtDate = (new SimpleDateFormat(dstDateFormat)).parse(tmpString, new ParsePosition(0));
  119.                 return rtDate;
  120.         }
  121. //java/5982

回复 "Java获取系统当前时间的方法并指定格式"

这儿你可以回复上面这条便签

captcha