欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

时间日期类

程序员文章站 2022-05-29 09:17:15
package com.mw.platform.util;import java.io.Serializable;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat ......
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;
}
}

}