gavinlau 发表于 2013-2-3 11:35:01

java 的日期处理函数

 
 Calendar time = Calendar.getInstance();
   time.clear();
   time.set(Calendar.YEAR, Integer.parseInt(year));
   time.set(Calendar.MONTH, Integer.parseInt(month) - 1 - 1);// 注意,Calendar对象默认一月为0
   int cday = time.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天数
   preDate = year + "-" + (Integer.parseInt(month) - 1) + "-" + cday;
   this.month = Integer.parseInt(month);// 为了页面显示
   this.premonth = this.month - 1;// 为了页面显示
===================================================================
public class DateUtil {
 public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
 public static final String SHORT_DATE_ISO_8601_PATTERN = "yyyy-MM-dd";
 public static final String SHORT_DATE_TIME_ISO_8601_PATTERN = "yyyy-MM-dd HH:mm:ss";
 public static int compareTo(Date date1, Date date2) {
  // Workaround for bug in JDK 1.5.x. This bug is fixed in JDK 1.5.07. See
  // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=6207898 for
  // more information.
  if ((date1 != null) && (date2 == null)) {
   return -1;
  } else if ((date1 == null) && (date2 != null)) {
   return 1;
  } else if ((date1 == null) && (date2 == null)) {
   return 0;
  }
  long time1 = date1.getTime();
  long time2 = date2.getTime();
  if (time1 == time2) {
   return 0;
  } else if (time1 < time2) {
   return -1;
  } else {
   return 1;
  }
 }
 public static boolean equals(Date date1, Date date2) {
  if (compareTo(date1, date2) == 0) {
   return true;
  } else {
   return false;
  }
 }
 public static boolean equals(Date date1, Date date2,
   boolean ignoreMilliseconds) {
  if (!ignoreMilliseconds) {
   return equals(date1, date2);
  }
  long time1 = 0;
  if (date1 != null) {
   time1 = date1.getTime() / Time.SECOND;
  }
  long time2 = 0;
  if (date2 != null) {
   time2 = date2.getTime() / Time.SECOND;
  }
  if (time1 == time2) {
   return true;
  } else {
   return false;
  }
 }
 public static String getCurrentDate(String pattern, Locale locale) {
  return getDate(new Date(), pattern, locale);
 }
 public static String getCurrentDate(String pattern, Locale locale,
   TimeZone timeZone) {
  return getDate(new Date(), pattern, locale, timeZone);
 }
 public static String getDate(Date date, String pattern, Locale locale) {
  DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
  return dateFormat.format(date);
 }
 public static String getDate(Date date, String pattern, Locale locale,
   TimeZone timeZone) {
  DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
  dateFormat.setTimeZone(timeZone);
  return dateFormat.format(date);
 }
 public static int getDaysBetween(Date startDate, Date endDate,
   TimeZone timeZone) {
  int offset = timeZone.getRawOffset();
  Calendar startCal = new GregorianCalendar(timeZone);
  startCal.setTime(startDate);
  startCal.add(Calendar.MILLISECOND, offset);
  Calendar endCal = new GregorianCalendar(timeZone);
  endCal.setTime(endDate);
  endCal.add(Calendar.MILLISECOND, offset);
  int daysBetween = 0;
  while (CalendarUtil.beforeByDay(startCal.getTime(), endCal.getTime())) {
   startCal.add(Calendar.DAY_OF_MONTH, 1);
   daysBetween++;
  }
  return daysBetween;
 }
 public static DateFormat getISOFormat() {
  return getISOFormat(StringPool.BLANK);
 }
 public static DateFormat getISOFormat(String text) {
  String pattern = StringPool.BLANK;
  if (text.length() == 8) {
   pattern = "yyyyMMdd";
  } else if (text.length() == 12) {
   pattern = "yyyyMMddHHmm";
  } else if (text.length() == 13) {
   pattern = "yyyyMMdd'T'HHmm";
  } else if (text.length() == 14) {
   pattern = "yyyyMMddHHmmss";
  } else if (text.length() == 15) {
   pattern = "yyyyMMdd'T'HHmmss";
  } else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
   pattern = "yyyyMMdd'T'HHmmssz";
  } else {
   pattern = "yyyyMMddHHmmssz";
  }
  return new SimpleDateFormat(pattern);
 }
 public static DateFormat getISO8601Format() {
  return new SimpleDateFormat(ISO_8601_PATTERN);
 }
 public static DateFormat getUTCFormat() {
  return getUTCFormat(StringPool.BLANK);
 }
 public static DateFormat getUTCFormat(String text) {
  String pattern = StringPool.BLANK;
  if (text.length() == 8) {
   pattern = "yyyyMMdd";
  } else if (text.length() == 12) {
   pattern = "yyyyMMddHHmm";
  } else if (text.length() == 13) {
   pattern = "yyyyMMdd'T'HHmm";
  } else if (text.length() == 14) {
   pattern = "yyyyMMddHHmmss";
  } else if (text.length() == 15) {
   pattern = "yyyyMMdd'T'HHmmss";
  } else {
   pattern = "yyyyMMdd'T'HHmmssz";
  }
  DateFormat dateFormat = new SimpleDateFormat(pattern);
  dateFormat.setTimeZone(TimeZone.getTimeZone(StringPool.UTC));
  return dateFormat;
 }
 public static Date newDate() {
  return new Date();
 }
 public static Date newDate(long date) {
  return new Date(date);
 }
 //
 public static String getFirstDayOfTheYear(int year) {
  String formateStr = year + "-01-01";
  String str_date = DateUtil
    .getDate(new Date(), formateStr, Locale.CHINA);
  return str_date;
 }
 public static String getLastDayOfTheYear(int year) {
  String formateStr = year + "-12-31";
  String str_date = DateUtil
    .getDate(new Date(), formateStr, Locale.CHINA);
  return str_date;
 }
 
 public static String getFirstDayOfTheMonth(int year, int month) {
  Calendar calendar = Calendar.getInstance();
  calendar.set(year, month, 1);
  calendar.getTime();
  String formateStr = "yyyy-MM-01";
  return DateUtil.getDate(calendar.getTime(), formateStr, Locale.CHINA);
 }
 
 public static String getLastDayOfTheMonth(int year, int month) {
  Calendar calendar = Calendar.getInstance();
  calendar.set(year, month, 1);
  //下个月第一号 减一
  calendar.add(Calendar.MONTH, 1);
  calendar.add(Calendar.DATE, -1);
  String formateStr = "yyyy-MM-dd";
  return DateUtil.getDate(calendar.getTime(), formateStr, Locale.CHINA);
 }
 public static void main(String[] args) {
  System.out.println(DateUtil.getLastDayOfTheMonth(2007,Calendar.FEBRUARY));
 }
}
页: [1]
查看完整版本: java 的日期处理函数