java日期工具类实例分享
时间:2023-05-17 17:50:40
/**
* 日期工具类
* 默认使用 "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;
}
}
标签:java日期工具类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解读@RequestBody与post请求的关系
2022-10-07 02:02:51
![](https://img.aspxhome.com/file/2023/7/95037_0s.png)
Spring注解Autowired的底层实现原理详解
2022-10-19 11:49:44
![](https://img.aspxhome.com/file/2023/7/132227_0s.jpg)
android效果TapBarMenu绘制底部导航栏的使用方式示例
2023-07-29 20:53:36
![](https://img.aspxhome.com/file/2023/2/84722_0s.gif)
深入学习C#多线程
2021-06-12 12:35:04
![](https://img.aspxhome.com/file/2023/2/111102_0s.png)
AndroidManifest.xml <uses-feature>和<uses-permisstion>分析及比较
2023-11-05 12:17:12
如何从dump文件中提取出C#源代码
2022-09-13 19:54:54
![](https://img.aspxhome.com/file/2023/4/91284_0s.png)
详解Android获取系统内核版本的方法与实现代码
2023-11-25 14:51:59
C# 设计模式系列教程-状态模式
2022-11-07 13:31:55
![](https://img.aspxhome.com/file/2023/9/111239_0s.png)
Java设计模式之接口隔离原则精解
2022-05-05 16:42:05
![](https://img.aspxhome.com/file/2023/5/80935_0s.png)
C#实现复杂XML的序列化与反序列化
2023-12-19 05:09:45
解决@Cacheable在同一个类中方法调用不起作用的问题
2022-02-19 01:46:54
![](https://img.aspxhome.com/file/2023/1/62031_0s.png)
Java字符串split使用方法代码实例
2023-02-06 18:55:31
![](https://img.aspxhome.com/file/2023/2/68002_0s.png)
原来Java中有两个ArrayList
2023-06-27 11:49:40
Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法
2022-11-10 15:07:26
![](https://img.aspxhome.com/file/2023/7/126707_0s.jpg)
Android build文件的删除的方法
2022-12-28 12:40:17
java如何连续执行多条cmd命令
2023-07-13 13:10:41
详解IDEA中SpringBoot整合Servlet三大组件的过程
2023-05-06 15:23:13
struts2简介_动力节点Java学院整理
2023-06-24 02:04:07
![](https://img.aspxhome.com/file/2023/3/72323_0s.jpg)
java实现切割wav音频文件的方法详解【附外部jar包下载】
2021-07-08 22:29:56
springboot乱码问题解决方案
2022-03-22 21:32:38