目的:辅助使用c#习惯的小伙伴使用DateTime来处理时间问题;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTime
{
static private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private Date date;
Calendar calendar;
public DateTime()
{
date = new Date();
calendar = Calendar.getInstance();
calendar.setTime(date);
}
public DateTime(String time)
{
try {
date = new Date();
date = dateFormat.parse(time);
calendar = Calendar.getInstance();
calendar.setTime(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
public DateTime(long allMillisec)
{
date = new Date(allMillisec);
calendar.setTime(date);
}
public DateTime(int year, int month, int day)
{
date = new Date(year, month, day);
calendar = Calendar.getInstance();
calendar.set(year, month, day);
}
public DateTime(int year, int month, int day, int hour, int minute, int second)
{
date = new Date(year, month, day, hour, minute, second);
calendar = Calendar.getInstance();
calendar.set(year, month, day, hour, minute, second);
}
public long getAllMillisec()
{
return date.getTime();
}
public static DateTime Now()
{
return new DateTime();
}
public int getYear()
{
return calendar.get(Calendar.YEAR);
}
public int getMonth()
{
return calendar.get(Calendar.MONTH);
}
public int getDay()
{
return calendar.get(Calendar.DATE);
}
public int getHour()
{
return calendar.get(Calendar.HOUR);
}
public int getMinute()
{
return calendar.get(Calendar.MINUTE);
}
public int getSecond()
{
return calendar.get(Calendar.SECOND);
}
public String getWeek()
{
String[] week={"", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
return week[Calendar.DAY_OF_WEEK];
}
public Calendar getCalendar()
{
return calendar;
}
public void setDate(Date dt)
{
this.date = dt;
this.calendar.setTime(dt);
}
public DateTime AddSeconds(int seconds)
{
DateTime time = new DateTime(this.toString("yyyy-MM-dd HH:mm:ss"));
time.getCalendar().add(Calendar.SECOND, seconds);
time.setDate(time.getCalendar().getTime());
return time;
}
public DateTime AddMinutes(int minutes)
{
DateTime time = new DateTime(this.toString("yyyy-MM-dd HH:mm:ss"));
time.getCalendar().add(Calendar.MINUTE, minutes);
time.setDate(time.getCalendar().getTime());
return time;
}
public DateTime AddDays(int days)
{
DateTime time = new DateTime(this.toString("yyyy-MM-dd HH:mm:ss"));
time.getCalendar().add(Calendar.DATE, days);
time.setDate(time.getCalendar().getTime());
return time;
}
public DateTime AddMonths(int months)
{
DateTime time = new DateTime(this.toString("yyyy-MM-dd HH:mm:ss"));
time.getCalendar().add(Calendar.MONTH, months);
time.setDate(time.getCalendar().getTime());
return time;
}
public DateTime AddYears(int years)
{
DateTime time = new DateTime(this.toString("yyyy-MM-dd HH:mm:ss"));
time.getCalendar().add(Calendar.YEAR, years);
time.setDate(time.getCalendar().getTime());
return time;
}
public String toString(String format)
{
DateFormat dtDateFormat = new SimpleDateFormat(format);
return dtDateFormat.format(date);
}
}