public static int lastDayOfMonth(int year, int month)
{
if (month == 2)
{
if (isLeapYear(year)) {
return 29;
}
return 28;
}
if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) ||
(month == 12)) {
return 31;
}
return 30;
}
public static boolean isLeapYear(int year)
{
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
return true;
}
return false;
}
public static Date dateInstance(int year, int month, int day)
{
Calendar c = Calendar.getInstance();
c.set(year, month - 1, day, 0, 0, 0);
return c.getTime();
}
public static Date dateInstance(int year, int month, int day, int hour, int min, int sec)
{
Calendar c = Calendar.getInstance();
c.set(year, month - 1, day, hour, min, sec);
return c.getTime();
}
public static Calendar calendarInstance(Date d)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
return c;
}
public static int weekDay(Date d)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
return c.get(7);
}
public static int weekCountOfMonth(int year, int month)
{
int lastDay = lastDayOfMonth(year, month);
Calendar c = Calendar.getInstance();
c.set(year, month - 1, lastDay);
return c.get(4);
}
public static int firstDayOfWeekDay(int year, int month, int argWeekDay)
{
Date d = dateInstance(year, month, 1);
int weekday = weekDay(d);
if (weekday == argWeekDay) {
return 1;
}
if (weekday > argWeekDay) {
return 1 + (7 - (weekday - argWeekDay));
}
return 1 + (argWeekDay - weekday);
}
public static int lastDayOfWeekDay(int year, int month, int argWeekDay)
{
int lastDay = lastDayOfMonth(year, month);
int weekday = weekDay(dateInstance(year, month, lastDay));
if (weekday == argWeekDay) {
return lastDay;
}
if (weekday < argWeekDay) {
return lastDay - (7 - (argWeekDay - weekday));
}
return lastDay - (weekday - argWeekDay);
}
public static int[] lastMonth(Date d)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
int year = c.get(1);
int month = c.get(2) + 1;
if (month == 1) {
return new int[] { year - 1, 12 };
}
return new int[] { year, month - 1 };
}
public static int[] lastMonth(int year, int month)
{
return lastMonth(dateInstance(year, month, 1));
}
public static int[] nextMonth(Date d)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
int year = c.get(1);
int month = c.get(2) + 1;
if (month == 12) {
return new int[] { year + 1, 1 };
}
return new int[] { year, month + 1 };
}
public static int[] nextMonth(int year, int month)
{
return nextMonth(dateInstance(year, month, 1));
}
public static long daysBetween(Date startDate, Date endDate)
{
return (endDate.getTime() - startDate.getTime()) / ONE_DAY_MILLIS;
}
public static Date addDays(Date d, int days)
{
if (d == null) {
return null;
}
return new Date(d.getTime() + days * ONE_DAY_MILLIS);
}
public static Date addMonths(Date d, int months)
{
if (months == 0) {
return d;
}
Calendar c = calendarInstance(d);
int day = c.get(5);
int hour = c.get(11);
int min = c.get(12);
int sec = c.get(13);
Date mydate = d;
if (months > 0) {
for (int kk = 0; kk < months; kk++)
{
int[] tmp = nextMonth(mydate);
mydate = dateInstance(tmp[0], tmp[1], 1);
}
} else {
for (int kk = 0; kk > months; kk--)
{
int[] tmp = lastMonth(mydate);
mydate = dateInstance(tmp[0], tmp[1], 1);
}
}
c = calendarInstance(mydate);
int year = c.get(1);
int month = c.get(2) + 1;
int lastDay = lastDayOfMonth(year, month);
if (day > lastDay) {
day = lastDay;
}
return dateInstance(year, month, day, hour, min, sec);
}
public static String toString(Date d, String format)
{
return new SimpleDateFormat(format).format(d);
}
public static Date toDate(String dateString, String format)
{
try
{
return new SimpleDateFormat(format).parse(dateString);
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}
public static String getStrYear()
{
return new SimpleDateFormat("yyyy").format(new Date());
}
public static String getStrMonth()
{
return new SimpleDateFormat("MM").format(new Date());
}
public static String getStrDay()
{
return new SimpleDateFormat("dd").format(new Date());
}
public static String getStrYearMonthDay()
{
return new SimpleDateFormat("yyyyMMdd").format(new Date());
}
public static Date getCurDate()
{
return new Date();
}
public static BigDecimal calDays(Date beginDate, Date endDate)
{
if ((beginDate != null) && (endDate != null))
{
BigDecimal days = new BigDecimal(daysBetween(beginDate, endDate));
return days.setScale(6, 4);
}
return null;
}
public static Date truncDate(Date d, String format)
{
Calendar c = calendarInstance(d);
int year = c.get(1);
int mon = c.get(2) + 1;
int day = c.get(5);
int hour = c.get(11);
int min = c.get(12);
int sec = c.get(13);
if (format.equals("yyyy")) {
return dateInstance(year, 1, 1);
}
if (format.equals("MM")) {
return dateInstance(year, mon, 1);
}
if (format.equals("dd")) {
return dateInstance(year, mon, day);
}
if (format.equals("HH")) {
return dateInstance(year, mon, day, hour, 0, 0);
}
if (format.equals("mm")) {
return dateInstance(year, mon, day, hour, min, 0);
}
if (format.equals("ss")) {
return dateInstance(year, mon, day, hour, min, sec);
}
return d;
}
public static Date padToMax(Date d, String format)
{
Calendar c = calendarInstance(d);
int year = c.get(1);
int mon = c.get(2) + 1;
int day = c.get(5);
int hour = c.get(11);
int min = c.get(12);
int sec = c.get(13);
if (format.equals("yyyy")) {
return dateInstance(year, 12, 31, 23, 59, 59);
}
if (format.equals("MM")) {
return dateInstance(year, mon, lastDayOfMonth(year, mon), 23, 59, 59);
}
if (format.equals("dd")) {
return dateInstance(year, mon, day, 23, 59, 59);
}
if (format.equals("HH")) {
return dateInstance(year, mon, day, hour, 59, 59);
}
if (format.equals("mm")) {
return dateInstance(year, mon, day, hour, min, 59);
}
if (format.equals("ss")) {
return dateInstance(year, mon, day, hour, min, sec);
}
return d;
}
public static Timestamp date2Timestamp(Date d)
{
Calendar sysCal = Calendar.getInstance();
sysCal.setTime(d);
return new Timestamp(sysCal.getTimeInMillis());
}
public static String timestamp2String(Timestamp t)
{
String n = t.toString();
int vv = n.indexOf(".");
String bb = n.substring(0, vv);
return bb;
}
public static Date strToDate(String str, String dateformat)
throws ParseException
{
DateFormat format = new SimpleDateFormat(dateformat);
Date date = format.parse(str);
return date;
}
public static String getCurrentSysTimeNow()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
return sdf.format(date);
}
public static String getCurrentSysTimeNow(String s)
{
Date date = new Date();
s = s.length() < 4 ? "yyyy-MM-dd HH:mm:ss" : s;
SimpleDateFormat sdf = new SimpleDateFormat(s);
return sdf.format(date);
}
/**
* 得到当前月份
* @return
*/
public static String getCurrentMonthInfo(){
java.util.Calendar calendar = java.util.Calendar.getInstance();
int monthI = calendar.get(Calendar.MONTH) + 1;
return monthI + "";
}
/**
* 得到当前年份
* @return
*/
public static String getCurrentYearInfo(){
java.util.Calendar calendar = java.util.Calendar.getInstance();
int year = calendar.get(Calendar.YEAR) ;
return year + "";
}