今天无聊写了个时间操作类,用于以后开发方便直接调用。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* 时间操作类
* Created by Alan on 2016/05/16 0016.
*/
public class DateUtility {
public static final String DEFAULT = "yyyy-MM-dd HH:mm:ss";
public static final String LONG = "yyyy年MM月dd日 HH时mm分ss秒";
public static final String SHORT = "yyyy-MM-dd HH:mm";
public static final String DATE = "yyyy-MM-dd";
public static final String TIME = "HH:mm:ss";
public static final String FULL = "yyyy-MM-dd HH:mm:ss EEEE";
public static final String WEEK = "EEEE";
private static SimpleDateFormat sdf;
public DateUtility(){
}
/**
* 取时间戳 dateUtility.getTime()
* @return
*/
public long getTime(){
return new Date().getTime();
}
/**
* 时间戳到日期时间 dateUtility.strToDateTime(dateUtility.getTime(), "yyyy-MM-dd hh:mm:ss")
* @param time
* @param template 默认:yyyy-MM-dd HH:mm:ss
* @return
*/
public String strToDateTime(long time, String template){
if(template.isEmpty() || template.equals("")){
template = DEFAULT;
}
sdf = new SimpleDateFormat(template, Locale.CHINA);
return sdf.format(new Date(time));
}
/**
* 取星期 dateUtility.getWeek(null)
* @param date
* @return
*/
public String getWeek(Date date){
if(date == null)
date = new Date();
sdf = new SimpleDateFormat(WEEK);
return sdf.format(date);
}
/**
* 取当前时间 dateUtility.getDateTime(new Date(), null)
* @param date 为Null则取当前时间
* @param template 默认:yyyy-MM-dd HH:mm:ss
* @return
*/
public String getDateTime(Date date,String template) {
if(date == null)
date = new Date();
if(template == null || template.equals("")){
template = DEFAULT;
}
sdf = new SimpleDateFormat(template, Locale.CHINA);
return sdf.format(date);
}
/**
* 取月份 dateUtility.getMonth(null)
* @param date
* @return
*/
public String getMonth(Date date) {
if(date == null){
date = new Date();
}
sdf = new SimpleDateFormat("MM");
return sdf.format(date);
}
/**
* 取年份 dateUtility.getYear(null)
* @param date
* @return
*/
public String getYear(Date date) {
if(date == null){
date = new Date();
}
sdf = new SimpleDateFormat("yyyy");
return sdf.format(date);
}
/**
* 取当前日 dateUtility.getDate(null)
* @param date
* @return
*/
public String getDate(Date date) {
if(date == null){
date = new Date();
}
sdf = new SimpleDateFormat("dd");
return sdf.format(date);
}
/**
* 格式化日期时间 dateUtility.formatDateToDateTimeWeek(null)
* @param date
* @return 返回时间星期
*/
public String formatDateToDateTimeWeek(Date date) {
if(date == null)
date = new Date();
String _week = getWeek(null);
String _time = getDateTime(date,"");
return _time + " " + _week;
}
/**
* 格式化日期时间 dateUtility.formatDateToDate(null)
* @param date
* @return 返回日期 yyyy-MM-dd
*/
public String formatDateToDate(Date date) {
if(date == null)
date = new Date();
return getDateTime(date,DATE);
}
/**
* 格式化日期时间 dateUtility.formatDateToTime(null)
* @param date
* @return 返回时间 HH:mm:ss
*/
public String formatDateToTime(Date date) {
if(date == null)
date = new Date();
return getDateTime(date, TIME);
}
/**
* 当前日期到时间
* @param calendar
* @return
*/
public Date calendarToDate(Calendar calendar) {
return calendar.getTime();
}
/**
* 取时间间隔(第二个时间减去第一个时间) dateUtility.getDaysBetweenTwoDate(new Date(), dateUtility.strToDate("2016-05-22 00:12:15",null)))
* @param firstDate 第一个时间
* @param secondDate 第二个时间
* @return 返回天数
*/
public int getDaysBetweenTwoDate(Date firstDate, Date secondDate) {
int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
return nDay;
}
/**
* 指定时间加、减指定的时间单位
*
* @param calendarFinal
* Calendar类中代表时间的字段常量
* Calendar.YEAR 年
* Calendar.MONTH 月(月份从0开始)
* Calendar.DATE 日
* Calendar.HOUR 时
* Calendar.MINUTE 分
* Calendar.SECOND 秒
* @param date
* 指定时间
* @param times
* 相加或减的时间,相加传正数,相减传负数
* @return 相加或相减后的时间
*/
public Date addOrMinusTimes(int calendarFinal, Date date, int times) {
if(date == null)
date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(calendarFinal, times);
return calendar.getTime();
}
/**
* 天数增加或者减少 dateUtility.addDays(null, 4), null)
* @param date 如果为null则取当前时间
* @param days 要增加或者减少的天数
* @return
*/
public Date addDays(Date date, int days) {
if(date == null)
date = new Date();
return addOrMinusTimes(Calendar.DATE, date, days);
}
/**
* 时间增加或减少月份 dateUtility.addMonths(null, 2), null)
* @param date 如果为null则取当前时间
* @param months 要增加或者减少的月份
* @return
*/
public Date addMonths(Date date, int months) {
if(date == null)
date = new Date();
return addOrMinusTimes(Calendar.MONTH, date, months);
}
/**
* 日期时间格式验证 dateUtility.validateDateFormat("2016-06-16","yyyy-MM-dd")
* @param str 如:2010-01-01 00:00
* @param tmpl 如:yyyy-MM-dd HH:mm
* @return 返回 false 验证失败
*/
public boolean validateDateFormat(String str, String tmpl) {
sdf = new SimpleDateFormat(tmpl);
sdf.setLenient(false);
Date date = null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
return false;
}
return str.equals(sdf.format(date));
}
/**
* 字符串转换成时间 dateUtility.strToDate("2016-05-22 00:12:15",null)
* @param time 如:2016-01-01 00:00:00
* @param tmpl 如:yyyy-MM-dd HH:mm:ss
* @return 返回Date时间对象
*/
public Date strToDate(String time,String tmpl){
if(tmpl == null || tmpl.equals(""))
tmpl = DEFAULT;
sdf = new SimpleDateFormat(tmpl);
Date date = null;
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}

