package com.mw.platform.util;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
//时间类(参考了.net 的接口)
//
// demo:: int date = Datetime.Now().addDay(-3).getDate();
//
public class Datetime implements Serializable {
private Date _datetime;
private Calendar _calendar = null;
public Datetime(){
setFulltime(new Date());
}
public Datetime(Date date) {
setFulltime(date);
}
public Datetime(long milliseconds) {
setFulltime(new Date(milliseconds));
}
public Datetime setFulltime(Date date) {
_datetime = date;
_calendar = Calendar.getInstance();
_calendar.setTime(date);
return this;
}
/* public Datetime setFulltime1(Date date){
_datetime = date;
_calendar = Calendar.getInstance();
_calendar.setTime(date);
return this;
}*/
public Date getFullTime() {
return _datetime;
}
// 当前时间
public static Datetime Now() {
return new Datetime(new Date());
}
// 添加年
public Datetime addYear(int year) {
return doAdd(Calendar.YEAR, +year);
}
// 添加月
public Datetime addMonth(int month) {
return doAdd(Calendar.MONTH, +month);
}
// 添加日
public Datetime addDay(int day) {
return doAdd(Calendar.DAY_OF_MONTH, +day);
}
// 添加小时
public Datetime addHour(int hour) {
return doAdd(Calendar.HOUR_OF_DAY, +hour);
}
// 添加分钟
public Datetime addMinute(int minute) {
return doAdd(Calendar.MINUTE, +minute);
}
// 添加秒
public Datetime addSecond(int minute) {
return doAdd(Calendar.SECOND, +minute);
}
private Datetime doAdd(int field, int amount) {
_calendar.add(field, +amount);
_datetime = _calendar.getTime();
return this;
}
// 获取当前年份
public int getYear() {
return _calendar.get(Calendar.YEAR);
}
// 获取当前月份
public int getMonth() {
return _calendar.get(Calendar.MONTH);
}
// 获取当前日份
public int getDays() {
return _calendar.get(Calendar.DAY_OF_MONTH);
}
// 获取当前小时
public int getHours() {
return _calendar.get(Calendar.HOUR_OF_DAY);
}
// 获取当前分钟
public int getMinutes() {
return _calendar.get(Calendar.MINUTE);
}
// 获取当前秒数
public int getSeconds() {
return _calendar.get(Calendar.SECOND);
}
// 获取当前豪秒
public long getMilliseconds() {
return _calendar.get(Calendar.MILLISECOND);
}
// 获取总天数(相对于:1970.01.01 00:00:00 GMT)
public long getAllDays() {
return getAllHours() / 24;
}
// 获取总小时(相对于:1970.01.01 00:00:00 GMT)
public long getAllHours() {
return getAllMinutes() / 60;
}
// 获取总分钟(相对于:1970.01.01 00:00:00 GMT)
public long getAllMinutes() {
return getAllSeconds() / 60;
}
// 获取总秒(相对于:1970.01.01 00:00:00 GMT)
public long getAllSeconds() {
return getTicks() / 1000;
}
// 获取总毫秒(相对于:1970.01.01 00:00:00 GMT)
public long getAllMilliseconds() {
return getTicks();
}
// 获取计时周期数(相对于:1970.01.01 00:00:00 GMT)
public long getTicks() {
return _datetime.getTime();
}
// 获取日期数字(yyyyMMdd)
public int getDate() {
return Integer.parseInt(toString("yyyyMMdd"));
}
// 转成String
public String toString(String format) {
DateFormat df = new SimpleDateFormat(format);
return df.format(_datetime);
}
/*public String toString1(String format){
DateFormat df1= new SimpleDateFormat(format);
return df1.format(-_datetime);
}*/
@Override
public String toString() {
return toString("yyyy-MM-dd HH:mm:ss");
}
/**
* @return 获取指定日期的本月第一天
*/
public Datetime getMonthFirstDay() {
_calendar.add(Calendar.MONTH, 0);
_calendar.set(Calendar.DAY_OF_MONTH, 1);
Datetime datetime = new Datetime(_calendar.getTime());
return datetime;
}
/*public Datetime getMonthFirstDay1(){
_calendar.add(Calendar.MONTH,0);
_calendar.set(Calendar.DAY_OF_MONTH,1);
Datetime datetime = new Datetime(_calendar.getTime());
return datetime;
}*/
/**
* @return 获取指定日期的本月最后一天
*/
public Datetime getMonthLastDay() {
_calendar.set(Calendar.DAY_OF_MONTH,
_calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Datetime datetime = new Datetime(_calendar.getTime());
return datetime;
}
/**
* @return 获取指定日期的周一日期
*/
public Datetime getTimesWeekmorning() {
_calendar.set(_calendar.get(Calendar.YEAR), _calendar.get(Calendar.MONDAY),
_calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
_calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Datetime datetime = new Datetime(_calendar.getTime());
return datetime;
}
/**
* @return 获取指定日期的周日日期
*/
public Datetime getTimesWeeknight() {
_calendar.setTime(getTimesWeekmorning().getFullTime());
_calendar.add(Calendar.DAY_OF_WEEK, 6);
Datetime datetime = new Datetime(_calendar.getTime());
return datetime;
}
// ===================
//
public static Datetime parse(String datetime, String format)
throws ParseException {
DateFormat df = new SimpleDateFormat(format);
Date date = df.parse(datetime);
return new Datetime(date);
}
public static Datetime tryParse(String datetime, String format) {
DateFormat df = new SimpleDateFormat(format);
try {
Date date = df.parse(datetime);
return new Datetime(date);
} catch (Exception ex) {
return null;
}
}
}