📄 dateutil.java
字号:
package com.yondor.oa.common.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
/**
* getDateStr get a string with format YYYY-MM-DD from a Date object
* @param date date
* @return String
*/
static public String getDateStr(Date date) {
if (date == null) {
return "";
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
static public String getDateStrC(Date date) {
if (date == null) {
return "";
}
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
return format.format(date);
}
/**
* getDateStr get a string with format YYYY-MM-DD HH:mm:ss from a Date object
* @param date date
* @return String
*/
static public String getDateTimeStr(Date date) {
if (date == null) {
return "";
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
static public String getDateTimeStrC(Date date) {
if (date == null) {
return "";
}
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
return format.format(date);
}
/**
* Parses text in 'YYYY-MM-DD' format to produce a date.
* @param s the text
* @return Date
* @throws ParseException
*/
static public Date parseDate(String s) throws ParseException {
if (s == "" || s == null) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
{
return format.parse(s);
}
}
static public Date parseDateC(String s) throws ParseException {
if (s == "" || s == null) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
return format.parse(s);
}
/**
* Parses text in 'YYYY-MM-DD' format to produce a date.
* @param s the text
* @return Date
* @throws ParseException
*/
static public Date parseDateTime(String s) throws ParseException {
if (s == "" || s == null) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(s);
}
static public Date parseDateTimeC(String s) throws ParseException {
if (s == "" || s == null) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
return format.parse(s);
}
static public String format(Date date) throws ParseException {
String datestr = "";
if (getDateTimeStr(date).equals("")) {
datestr = "";
}
else {
datestr = getDateTimeStr(date).substring(0, 10);
}
return datestr;
}
/**
* 计算两个日期相差的天数
* @param beginDate
* @param endDate
* @return
*/
static public Long compare(Date beginDate,Date endDate){
return new Long(endDate.getTime()-beginDate.getTime())/1000/60/60/24;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -