package waf.datatype;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
// Data是Long数据类型的封装
// 如果只需要获取整体日期或者时间,用Date对象和SimpleDateFormat对象即可完成
// 如果需要获取日期中的子项,或者需要进行时间运算,就需要使用Calendar类族.
// Date对象主要是数据存储功能,Calendar对象主要是数据计算功能
// 因此,Calendar对象更改之后,必须重新获取到Date对象更新到类成员变量中.
/**
* Author:waf.wang
*/
public class DateTime
{
private java.util.Calendar calendar=java.util.Calendar.getInstance();
private java.util.Date date=new java.util.Date();
private java.text.SimpleDateFormat simpleDateFormat=new java.text.SimpleDateFormat();
public static final long lADay=24L*60L*60L*1000L;
public static final long lASecond=1000L;
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static Calendar startDate = Calendar.getInstance();
private static Calendar endDate = Calendar.getInstance();
private static DateFormat df = DateFormat.getDateInstance();
private static Date earlydate = new Date();
private static Date latedate = new Date();
public static void main(String[] args) throws ParseException
{
DateTime dt11=new DateTime();
String abcc=DateTime.formatDatetime("2010-1-1");
String rrr=addDayForWorkingDay("2013-07-24 11:00:02.000",3);
rrr=addSecondForWorkingDay("2013-07-26 23:59:58",3);
boolean bret=DateTime.isDateTime("202-12-56");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = df.parse("2004-03-26 13:31:40");
Date d2 = df.parse("2004-03-26 13:31:41");
long diff = d1.getTime() - d2.getTime();
long days = diff / (1000 * 60 * 60 * 24);
// waf.datatype.DateTime dateTime=new waf.datatype.DateTime();
// System.out.print( dateTime.getDateTimeString());
// System.out.print(dateTime.diffms("2009-04-02 21:38"));
long lDiff=DateTime.diffDay("2009-04-02 21:38:20", "2009-03-02 21:38:24");
// System.out.print(lDiff);
DateTime dt=new DateTime();
for(int i=0;i<100;i++)
{
System.out.println(System.currentTimeMillis());
waf.lang.Thread.sleep(1000);
}
// System.out.print(dateTime.addMonth("2006-12-32",2));
}
public DateTime()
{
initFormLocal();
}
private void initFormLocal()
{
TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//获取中国的时区
this.simpleDateFormat.setTimeZone(timeZoneChina);//设置系统时区
this.OnDateUpdate(); // 把日期设置到日历对象上去
}
public DateTime(String strDateTime)
{
if(strDateTime==null || strDateTime.length()==0)
{
initFormLocal();
}
else
{
this.setString(strDateTime);
}
}
// since 1970
public DateTime(long unixtime)
{
this.date=new java.util.Date(unixtime);
}
public DateTime(java.util.Date date)
{
this.date=date;
}
public DateTime(java.sql.Timestamp timestamp)
{
this.date=new java.util.Date(timestamp.getTime());
}
public Date getDate()
{
return this.date;
}
public java.sql.Timestamp getTimestamp()
{
return new java.sql.Timestamp(date.getTime());
}
public String getDateTime()
{
return this.getString("yyyy-MM-dd HH:mm:ss");
}
public String getDateTimeString()
{
return this.getString("yyyy-MM-dd HH:mm:ss");
}
public String getTimestampString()
{
return this.getString("yyyyMMddHHmmss");
}
// public String getDate()
// {
// return this.getString("yyyy-MM-dd");
// }
public String getDateString()
{
return this.getString("yyyy-MM-dd");
}
public String getTimeString()
{
return this.getString("HH:mm:ss");
}
public String getString(String strFormatName)
{
String strRet="";
this.simpleDateFormat.applyPattern(strFormatName);
strRet = this.simpleDateFormat.format(this.date);
return strRet;
}
public String getString()
{
String strRet="";
strRet = this.simpleDateFormat.format(this.date);
return strRet;
}
public long getLong()
{
return this.date.getTime();
}
public int getHour()
{
return this.calendar.get(Calendar.HOUR_OF_DAY);
}
public int getDayOfWeek()
{
return this.calendar.get(Calendar.DAY_OF_WEEK);
}
// public String getWeekDayFirst
// {
//
// }
public int getDayOfMonth()
{
return this.calendar.get(Calendar.DAY_OF_MONTH);
}
public int getMonth()
{
return this.calendar.get(Calendar.MONTH);
}
public long diffms(String strDateTime2)
{
long lRet=0;
DateTime dateTime2=new DateTime(strDateTime2);
long lDiff=this.getLong()-dateTime2.getLong();
return lDiff;
}
public long diffDay(String strDateTime2)
{
long lRet=0;
long lDiff=diffms(strDateTime2);
lRet=lDiff/lADay;
return lRet;
}
public static long diffms(String strDateTime1,String strDateTime2)
{
long lRet=0;
DateTime dateTime1=new DateTime(strDateTime1);
return dateTime1.diffms(strDateTime2);
}
public static List<String> getDayList(String beginDate,String endDate)
{
return DateTimeUtil.getDayList(beginDate, endDate);
}
public static List<String> getHourList(String beginDate, String endDate) {
// TODO Auto-generated method stub
return DateTimeUtil.getHourList(beginDate, endDate);
}
public static long diffDay(String strDateTime1,String strDateTime2)
{
long lRet=0;
DateTime dateTime1=new DateTime(strDateTime1);
return dateTime1.diffDay(strDateTime2);
}
//相差月份
public static int monthsBetween(String start, String end) {
try {
startDate.setTime(sdf.parse(start));
endDate.setTime(sdf.parse(end));
} catch (ParseException e) {
e.printStackTrace();
}
int result = yearsBetween(start, end) * 12 + endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH);
return result == 0 ? 1 : Math.abs(result);
}
//相差年份
public static int yearsBetween(String start, String end) {
try {
startDate.setTime(sdf.parse(start));
endDate.setTime(sdf.parse(end));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR));
}
public void setDateTime(java.util.Date newDate)
{
this.date=newDate;
OnDateUpdate();
}
public void setNow()
{
this.setDateTime(new Date());
}
public static boolean isDateTime(String strDateTime)
{
boolean ret=false;
DateTime dt=new DateTime();
ret=dt.setString(strDateTime);
return ret;
}
public boolean setString(String strDateTime)
{
boolean bRet=true;
String strSource=strDateTime;
if(strSource==null)
{
return false;
}
if(strSource.length()==4)
{
strSource+="-01-01 00:00:00";
}
else if(strSource.length()==10)
{
strSource+=" 00:00:00";
}
else if(strSource.length()==16)
{
strSource+=":00";
}
if(strSource.length()==19)
{
this.simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
}
else if(strSource.length()==21)
{
this.simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss.S");
}
else if(strSource.length()==23)
{
this.simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss.SSS");
}
try
{
this.setDateTime(this.simpleDateFormat.parse(strSource));
bRet=true;
}
catch (ParseException e)
{
//e.printStackTrace();
bRet=false;
}
return bRet;
}
public String addMonth(int iMonth)
{
this.calendar.add(Calendar.MONTH, iMonth);
this.OnCalendarUpdate();
return this.getDateTimeString();
}
public static String addMonth(String strDateTime,int iMonth)
{
DateTime dataTime=new DateTime();
dataTime.setString(strDateTime);
return dataTime.addMonth(iMonth);
}
public String addDay(int iDay)
{
this.calendar.add(Calendar.DAY_OF_YEAR, iDay);
this.OnCalendarUpdate();
return this.getDateTimeString();
}
public String addYear(int year)
{
this.calendar.add(Calendar.YEAR, year);
this.OnCalendarUpdate();
return this.getDateTimeString();
}
public static String addDay(String strDateTime,int iDay)
{
return DateTimeUtil.addDay(strDateTime, iDay);
}
public String addSecond(int iSecond)
{
String ret="";
this.calendar.add(Calendar.SECOND, iSecond);
this.OnCalendarUpdate();
return this.getString();
}
public static String addSecond(String strDateTime,int iSecond)
{
DateTime dataTime=new DateTime();
dataTime.setString(strDateTime);
return dataTime.addSecond(iSecond);
}
public String addMinute(int minute)
{
String ret="";
this.calendar.add(Calendar.MINUTE, minute);
this.OnCalendarUpdate();
return this.getString();
}
public static String addMniute(String strDateTime,int minute)
{
DateTime dataTime=new DateTime();
dataTime.setString(strDateTime);
return dataTime.addMinute(minute);
}
public void setCalendar(Calendar c)
{
this.calendar=c;
this.OnCalendarUpdate();
}
// 当Calendar变化了,就需要更新
private void OnCalendarUpdate()
{
this.date=this.calendar.getTime();
}
private void OnDateUpdate()
{
this.calendar.setTime(this.date);
}
public static String formatDatetime(String datetime)
{
String ret="";
String[] temp=datetime.split("/|-| |:");
try
{
if(temp.length>=3)
{
String year=String.format("%04d", Integer.parseInt(temp[0]));
String month=String.format("%02d", Integer.parseInt(temp[1]));
String day=String.format("%02d", Integer.parseInt(temp[2]));
ret=temp[0]+"-"+month+"-"+day;
}
if(temp.length>=6)
{
String hour=String.format("%02d", Integer.parseInt(temp[3]));
String minute=String.format("%02d", Integer.parseInt(temp[4]));
String second=String.format("%02d", Integer.parseInt(temp[5]));
ret+=" "+hour+":"+minute+":"+second;
}
}
catch (Exception e)
{
}
return ret;
}
public static boolean isWorkingday(String day)
{
boolean ret=true;
int dayOfWeek=new DateTime(day).getDayOfWeek();
if(dayOfWeek==Calendar.SATURDAY || dayOfWeek==Calendar.SUNDAY)
{
ret=false;
}
return ret;
}
public static String reCalcEndTimeForWorkingDay(String begin_time,String end_time)
{
String time="";
if(end_time.length()>10)
{
time=end_time.substring(10, end_time.length());
}
List<String> list=DateTime.getDayList(begin_time, end_time);
Map<String,String> map=new LinkedHashMap<String,String>();
for(String day:list)
{
map.put(day, "");
}
int count=0;
List<String> removeList=new ArrayList<String>();
String lastDay="";
for(String day:map.keySet())
{
if(!isWorkingday(day))
{
removeList.add(day);
count++;
}
lastDay=day;
}
for(String remove:removeList)
{
map.remove(remove);
}
String nextDay=lastDay;
for(int i=1;i<=count;)
{
nextDay=DateTime.addDay(nextDay, 1);
if(isWorkingday(nextDay))
{
map.put(nextDay, "");
i++;
}
}
nextDay=nextDay.substring(0, 10)+time;
return nextDay;
}
public static String addSecondForWorkingDay(String begin_time,int second)
{
String end_time=DateTime.addSecond(begin_time, second);
return reCalcEndTimeForWorkingDay(begin_time,end_time);
}
public static String addDayForWorkingDay(String begin_time,int days)
{
String end_time=DateTime.addDay(begin_time, days);
return reCalcEndTimeForWorkingDay(begin_time,end_time);
}
}