加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java日期格式化工具类

发布时间:2020-12-14 23:31:13 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 /** * 日期工具类 */public final class DateUtils {/*** 英文简写(默认)如:2010-12-01*/public static String FORMAT_SHORT = "yyyy-MM-dd";/***

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

 
/**
 * 日期工具类 
 */
public final class DateUtils {
/**
* 英文简写(默认)如:2010-12-01
*/
public static String FORMAT_SHORT = "yyyy-MM-dd";
/**
* 英文全称  如:2010-12-01 23:15:06
*/
public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
/**
* 精确到毫秒的完整时间    如:yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
/**
* 中文简写  如:2010年12月01日
*/
public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
/**
* 中文全称  如:2010年12月01日  23时15分06秒
*/
public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";
/**
* 精确到毫秒的完整中文时间
*/
public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";
/**
* 获得默认的 date pattern
*/
public static String getDatePattern() {
return FORMAT_LONG;
}
/**
* 根据预设格式返回当前日期
* @return
*/
public static String getNow() {
return format(new Date());
}
/**
* 根据用户格式返回当前日期
* @param format
* @return
*/
public static String getNow(String format) {
return format(new Date(),format);
}
/**
* 使用预设格式格式化日期
* @param date
* @return
*/
public static String format(Date date) {
return format(date,getDatePattern());
}
/**
* 使用用户格式格式化日期
* @param date 日期
* @param pattern 日期格式
* @return
*/
public static String format(Date date,String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
/**
* 使用预设格式提取字符串日期
* @param strDate 日期字符串
* @return
*/
public static Date parse(String strDate) {
return parse(strDate,getDatePattern());
}
/**
* 使用用户格式提取字符串日期
* @param strDate 日期字符串
* @param pattern 日期格式
* @return
*/
public static Date parse(String strDate,String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 在日期上增加数个整月
* @param date 日期
* @param n 要增加的月数
* @return
*/
public static Date addMonth(Date date,int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH,n);
return cal.getTime();
}
/**
* 在日期上增加天数
* @param date 日期
* @param n 要增加的天数
* @return
*/
public static Date addDay(Date date,int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,n);
return cal.getTime();
}
/**
* 获取时间戳
*/
public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
}
/**
* 获取日期年份
* @param date 日期
* @return
*/
public static String getYear(Date date) {
return format(date).substring(0,4);
}
/**
* 按默认格式的字符串距离今天的天数
* @param date 日期字符串
* @return
*/
public static int countDays (String date) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date));
long t1 = c.getTime().getTime();
return (int)(t/1000 - t1/1000)/3600/24;
}
/**
* 按用户格式字符串距离今天的天数
* @param date 日期字符串
* @param format 日期格式
* @return
*/
public static int countDays (String date,String format) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date,format));
long t1 = c.getTime().getTime();
return (int)(t/1000 - t1/1000)/3600/24;
}
}
 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读