一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java日期工具類實例分享

java日期工具類實例分享

2019-10-29 16:06java技術網 JAVA教程

本文介紹一個java日期工具類,功能有英文簡寫、英文全稱、精確到毫秒的完整時間、中文簡寫、中文全稱等方法

代碼如下:


/**
 * 日期工具類
 * 默認使用 "yyyy-MM-dd HH:mm:ss" 格式化日期
 */
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;
}
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99久久免费国内精品 | 亚洲AV无码国产精品色在线看 | 9966久久精品免费看国产 | 十六以下岁女子毛片免费 | www免费看 | 农村脱精光一级 | 99精品视频在线观看免费 | 日韩理论片在线看免费观看 | 激情涩涩 | 午夜亚洲WWW湿好爽 午夜想想爱午夜剧场 | 亚洲成在人网站天堂一区二区 | 黑人性xxxⅹxxbbbbb | 亚洲国产区男人本色在线观看欧美 | 日韩成人av在线 | 国产性片在线观看 | 久久久久久久国产精品视频 | 成人二区 | 免费观看韩剧网站在线观看 | 性姿势女人嗷嗷叫图片 | tube69中国露脸 | 亚洲精品久久久久69影院 | 俄罗斯处女摘花 | 性柔术18性13处交 | h版欧美大片免费观看 | 性做久久久久久 | 熟睡中的麻麻大白屁股小说 | 美女用手扒开粉嫩的屁股 | 久久视频这有精品63在线国产 | 国产美女亚洲精品久久久久久 | 日韩久久网 | 楚乔传第二部全60集免费观看 | 国产午夜精品久久久久小说 | free性欧洲 | 欧美在线视频免费播放 | 91av免费在线观看 | 小辣椒精品福利视频导航 | 国产成人精品一区二区阿娇陈冠希 | 亚洲图片综合区 | 亚洲欧美久久一区二区 | 精品视频在线播放 | 日本无卡无吗中文免费 |