Java各种获取系统当前时间方法 和 格式
2014-12-11 19:31
253 查看
/**
* 返回当前日期时间字符串<br>
* 默认格式:yyyy-mm-dd hh:mm:ss
*
* @return String 返回当前字符串型日期时间
*/
public static String getCurrentTime() {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
returnStr = f.format(date);
return returnStr;
}
/**
* 返回当前日期时间字符串<br>
* 默认格式:yyyymmddhhmmss
*
* @return String 返回当前字符串型日期时间
*/
public static BigDecimal getCurrentTimeAsNumber() {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
returnStr = f.format(date);
return new BigDecimal(returnStr);
}
/**
* 返回自定义格式的当前日期时间字符串
*
* @param format
* 格式规则
* @return String 返回当前字符串型日期时间
*/
public static String getCurrentTime(String format) {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat(format);
Date date = new Date();
returnStr = f.format(date);
return returnStr;
}
/**
* 返回当前字符串型日期
*
* @return String 返回的字符串型日期
*/
public static String getCurDate() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = simpledateformat.format(calendar.getTime());
return strDate;
}
/**
* 返回指定格式的字符型日期
* @param date
* @param formatString
* @return
*/
public static String Date2String(Date date, String formatString) {
if (G4Utils.isEmpty(date)) {
return null;
}
SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);
String strDate = simpledateformat.format(date);
return strDate;
}
/**
* 返回当前字符串型日期
*
* @param format
* 格式规则
*
* @return String 返回的字符串型日期
*/
public static String getCurDate(String format) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
String strDate = simpledateformat.format(calendar.getTime());
return strDate;
}
/**
* 返回TimeStamp对象
*
* @return
*/
public static Timestamp getCurrentTimestamp() {
Object obj = TypeCaseHelper.convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");
if (obj != null)
return (Timestamp) obj;
else
return null;
}
/**
* 将字符串型日期转换为日期型
*
* @param strDate
* 字符串型日期
* @param srcDateFormat
* 源日期格式
* @param dstDateFormat
* 目标日期格式
* @return Date 返回的util.Date型日期
*/
public static Date stringToDate(String strDate, String srcDateFormat, String dstDateFormat) {
Date rtDate = null;
Date tmpDate = (new SimpleDateFormat(srcDateFormat)).parse(strDate, new ParsePosition(0));
String tmpString = null;
if (tmpDate != null)
tmpString = (new SimpleDateFormat(dstDateFormat)).format(tmpDate);
if (tmpString != null)
rtDate = (new SimpleDateFormat(dstDateFormat)).parse(tmpString, new ParsePosition(0));
return rtDate;
}
* 返回当前日期时间字符串<br>
* 默认格式:yyyy-mm-dd hh:mm:ss
*
* @return String 返回当前字符串型日期时间
*/
public static String getCurrentTime() {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
returnStr = f.format(date);
return returnStr;
}
/**
* 返回当前日期时间字符串<br>
* 默认格式:yyyymmddhhmmss
*
* @return String 返回当前字符串型日期时间
*/
public static BigDecimal getCurrentTimeAsNumber() {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
returnStr = f.format(date);
return new BigDecimal(returnStr);
}
/**
* 返回自定义格式的当前日期时间字符串
*
* @param format
* 格式规则
* @return String 返回当前字符串型日期时间
*/
public static String getCurrentTime(String format) {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat(format);
Date date = new Date();
returnStr = f.format(date);
return returnStr;
}
/**
* 返回当前字符串型日期
*
* @return String 返回的字符串型日期
*/
public static String getCurDate() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = simpledateformat.format(calendar.getTime());
return strDate;
}
/**
* 返回指定格式的字符型日期
* @param date
* @param formatString
* @return
*/
public static String Date2String(Date date, String formatString) {
if (G4Utils.isEmpty(date)) {
return null;
}
SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);
String strDate = simpledateformat.format(date);
return strDate;
}
/**
* 返回当前字符串型日期
*
* @param format
* 格式规则
*
* @return String 返回的字符串型日期
*/
public static String getCurDate(String format) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
String strDate = simpledateformat.format(calendar.getTime());
return strDate;
}
/**
* 返回TimeStamp对象
*
* @return
*/
public static Timestamp getCurrentTimestamp() {
Object obj = TypeCaseHelper.convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");
if (obj != null)
return (Timestamp) obj;
else
return null;
}
/**
* 将字符串型日期转换为日期型
*
* @param strDate
* 字符串型日期
* @param srcDateFormat
* 源日期格式
* @param dstDateFormat
* 目标日期格式
* @return Date 返回的util.Date型日期
*/
public static Date stringToDate(String strDate, String srcDateFormat, String dstDateFormat) {
Date rtDate = null;
Date tmpDate = (new SimpleDateFormat(srcDateFormat)).parse(strDate, new ParsePosition(0));
String tmpString = null;
if (tmpDate != null)
tmpString = (new SimpleDateFormat(dstDateFormat)).format(tmpDate);
if (tmpString != null)
rtDate = (new SimpleDateFormat(dstDateFormat)).parse(tmpString, new ParsePosition(0));
return rtDate;
}
相关文章推荐
- Java各种获取系统当前时间方法和格式
- Java各种获取系统当前时间方法 和 格式
- Java 获取当前系统时间 格式:yyyy-MM-dd HH:mm:ss
- Java获取当前时间,及转化格式的方法
- Java 获取系统当前时间,格式(2015-05-06 21:26:39.82)
- JAVA中获取当前系统时间的两种方法
- Java中获取当前系统的时间的方法
- java 获取系统当前时间的方法
- java中获取当前系统时间的方法
- Java 以指定日期时间格式获取当前时间、以及每隔一秒刷新一次的方法------附带实例
- JAVA,jsp,oracle获取系统当前时间以及日期格式转换!
- Java获取各种常用时间方法
- (转)Java获取当前系统时间
- java中获取不同时间格式的方法
- Java获取当前系统时间
- Java,.NET中当前时间获取、格式化方法比较
- Java获取各种常用时间方法
- 主题:Java获取各种常用时间方法
- java另一种获取当前系统的时间与日期
- Java获取各种常用时间方法