自定义事件格式--常用的 Java编写的各种时间格式类
2011-11-07 15:31
393 查看
public class TimeStaticMethod { /** * @see 得到当前时刻的时间字符串 * @return String para的标准时间格式的串,例如:返回'2003-08-09 16:00:00' */ public static String getLocalString() { java.util.Date currentDate = new java.util.Date(); java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); String date = dateFormat.format(currentDate); return date; } public static String getLocalString(java.util.Date currentDate) { java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); String date = dateFormat.format(currentDate); return date; } // 的到默认的时间格式为("yyyy-MM-dd HH:mm:ss")的当前时间 public static String getCurrentDateTime() { return getCurrentDateTime("yyyy-MM-dd HH:mm:ss"); } /* 根据输入的格式(String _dtFormat)得到当前时间格式 */ public static String getCurrentDateTime(String _dtFormat) { String currentdatetime = ""; try { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dtFormat = new SimpleDateFormat(_dtFormat); currentdatetime = dtFormat.format(date); } catch (Exception e) { System.out.println("时间格式不正确"); e.printStackTrace(); } return currentdatetime; } public static Timestamp getTimestamp(String str) { Timestamp ret = null; try { SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); Date date = dateFormat.parse(str); long datelong = date.getTime(); ret = new Timestamp(datelong); } catch (Exception e) { } return ret; } public static Timestamp getTimestamp(String str,String _dtFormat) { Timestamp ret = null; try { SimpleDateFormat dateFormat = new SimpleDateFormat( _dtFormat); Date date = dateFormat.parse(str); long datelong = date.getTime(); ret = new Timestamp(datelong); } catch (Exception e) { } return ret; } public static Timestamp getTimestamp() { return getTimestamp(0); } public static String getTimestampString(Timestamp sta) { String ret= ""; try{ String str = sta.toString(); ret = str.substring(0, str.lastIndexOf('.')); } catch(Exception e){ ret = ""; } return ret; } /** length 推荐直接用 StaticVariable中的 YearToMonth 和 YearToDay 做参数. * YearToMonth: 2007-01 * YearToDay: 2007-01-30 * @param sta * @param length * @return */ public static String getTimestampFormat(Timestamp sta, int length) { String ret= ""; try{ String str = sta.toString(); ret = str.substring(0, length); } catch(Exception e){ ret = ""; } return ret; } public static Timestamp getTimestamp(int disday) { Calendar c; c = Calendar.getInstance(); long realtime = c.getTimeInMillis(); realtime += 86400000 * disday; return new Timestamp(realtime); } /** * @see 得到时间字符串 * @see 例如:StaticMethod.getDateString(-1),可以返回昨天的时间字符串 * @param disday int 和当前距离的天数 * @return String para的标准时间格式的串,例如:返回'2003-8-10 00:00:00' */ public static String getTimeString(int disday) { String ls_display = ""; Calendar c; c = Calendar.getInstance(); long realtime = c.getTimeInMillis(); realtime += 86400000 * disday; c.setTimeInMillis(realtime); String _yystr = "", _mmstr = "", _ddstr = ""; int _yy = c.get(Calendar.YEAR); _yystr = _yy + ""; int _mm = c.get(Calendar.MONTH) + 1; _mmstr = _mm + ""; if (_mm < 10) { _mmstr = "0" + _mm; } int _dd = c.get(Calendar.DATE); _ddstr = _dd + ""; if (_dd < 10) { _ddstr = "0" + _dd; } ls_display = "'" + _yy + "-" + _mm + "-" + _dd + " 00:00:00'"; return ls_display; } public static Date getCurrentDateTime(String strDate, String _dtFormat) { Date tDate = null; SimpleDateFormat smpDateFormat = new SimpleDateFormat(_dtFormat); ParsePosition pos = new ParsePosition(0); tDate = smpDateFormat.parse(strDate, pos); //标准格式的date类型时间 return tDate; }
相关文章推荐
- Java获取各种常用时间方法
- Java获取各种常用时间方法
- Java各种获取系统当前时间方法 和 格式
- java获取各种格式的时间,获取昨天明天日期,获取一天的开始结束时间
- Java时间类型和String类型间各种格式的转化
- Java获取各种常用时间方法
- Java各种获取系统当前时间方法 和 格式
- Java获取各种常用时间方法
- java中各种时间格式的转化
- Java获取各种常用时间方法
- Java各种常用时间转换
- java 常用的各种时间方法。
- java中各种时间格式的转化
- java中各种时间格式的转化
- java中各种时间格式的转化
- IT忍者神龟之Java获取各种常用时间方法(工具类)
- Java获取各种常用时间方法
- [记录]Java获取各种常用时间方法
- Java获取各种常用时间
- Java获取各种常用时间方法2