java编写时间类,自定义事件格式--常用的 Java编写的各种时间格式类

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值