posts - 247,  comments - 25,  trackbacks - 0

mport java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class DateFormat {
 private static SimpleDateFormat formatter;

 public DateFormat() {
 }

 public static String shortDate(Date aDate) {
  formatter = new SimpleDateFormat("yyyy-MM-dd");
  return formatter.format(aDate);
 }

 public static String mailDate(Date aDate) {
  formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  return formatter.format(aDate);
 }

 public static String longDate(Date aDate) {
  formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  return formatter.format(aDate);
 }

 public static String shortDateGB(Date aDate) {
  formatter = new SimpleDateFormat("yyyy'锟斤拷'MM'锟斤拷'dd'锟斤拷'");
  return formatter.format(aDate);
 }

 public static String longDateGB(Date aDate) {
  formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  return formatter.format(aDate);
 }

 public static String formatDate(Date aDate, String formatStr) {
  formatter = new SimpleDateFormat(formatStr);
  return formatter.format(aDate);

 }

 public static String LDAPDate(Date aDate) {
  return formatDate(aDate, "yyyyMMddHHmm'Z'");
 }

 public static Date getDate(String yyyymmdd) {
  if ((null == yyyymmdd) || (yyyymmdd.trim().length() == 0))
   return null;
  int year = Integer.parseInt(yyyymmdd
    .substring(0, yyyymmdd.indexOf("-")));
  int month = Integer.parseInt(yyyymmdd.substring(
    yyyymmdd.indexOf("-") + 1, yyyymmdd.lastIndexOf("-")));
  int day = Integer.parseInt(yyyymmdd
    .substring(yyyymmdd.lastIndexOf("-") + 1));
  Calendar cal = Calendar.getInstance();
  cal.set(year, month - 1, day);
  return cal.getTime();

 }

 public static Date getDateFull(String yyyymmdd) {
  if ((null == yyyymmdd) || (yyyymmdd.trim().length() == 0))
   return null;
  int year = Integer.parseInt(yyyymmdd
    .substring(0, yyyymmdd.indexOf("-")));
  int month = Integer.parseInt(yyyymmdd.substring(
    yyyymmdd.indexOf("-") + 1, yyyymmdd.lastIndexOf("-")));
  int day = Integer.parseInt(yyyymmdd
    .substring(yyyymmdd.lastIndexOf("-") + 1));
  Calendar cal = Calendar.getInstance();
  cal.set(year, month - 1, day, 24, 60, 60);
  return cal.getTime();

 }

 public static Date parser(String strDate) {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  try {
   return sdf.parse(strDate);
  } catch (Exception e) {
   return null;
  }
 }

 public static Date parser(String strDate, String formatter) {
  SimpleDateFormat sdf = new SimpleDateFormat(formatter);
  try {
   return sdf.parse(strDate);
  } catch (Exception e) {
   return null;
  }
 }

 /**
  * get Date with only date num. baoxy add
  *
  * @param yyyymmdd
  * @return
  */
 public static Date getDateOnly(String yyyymmdd) {
  if ((null == yyyymmdd) || (yyyymmdd.trim().length() == 0))
   return null;
  int year = Integer.parseInt(yyyymmdd
    .substring(0, yyyymmdd.indexOf("-")));
  int month = Integer.parseInt(yyyymmdd.substring(
    yyyymmdd.indexOf("-") + 1, yyyymmdd.lastIndexOf("-")));
  int day = Integer.parseInt(yyyymmdd
    .substring(yyyymmdd.lastIndexOf("-") + 1));
  Calendar cal = Calendar.getInstance();
  cal.set(year, month - 1, day, 0, 0, 0);
  return cal.getTime();
 }

 public static Date addMonth(Date myDate, int amount) {
  GregorianCalendar cal = new GregorianCalendar();
  cal.setTime(myDate);
  boolean isEndDayOfMonth_old = cal
    .getActualMaximum(GregorianCalendar.DAY_OF_MONTH) == cal
    .get(GregorianCalendar.DAY_OF_MONTH);
  cal.add(GregorianCalendar.MONTH, amount);
  boolean isEndDayOfMonth_new = cal
    .getActualMaximum(GregorianCalendar.DAY_OF_MONTH) == cal
    .get(GregorianCalendar.DAY_OF_MONTH);
  if (isEndDayOfMonth_old && !isEndDayOfMonth_new) {
   cal.set(GregorianCalendar.DATE, cal
     .getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
  }
  return cal.getTime();
 }

 public static Date addDay(Date myDate, int amount) {
  Calendar cal = Calendar.getInstance();
  cal.setTime(myDate);
  cal.add(Calendar.DAY_OF_MONTH, amount);
  return cal.getTime();
 }

 // -------------------锟斤拷锟斤拷锟斤拷锟�?-----------------------------------
 public static Date addMinute(Date myDate, int amount) {
  Calendar cal = Calendar.getInstance();
  cal.setTime(myDate);
  int minute = 0;
  amount = -(amount);

  if (amount > 60) {

   int hour = (int) amount / 60;

   if (hour * 60 > amount) {
    minute = hour * 60 - amount;

    cal.add(Calendar.HOUR_OF_DAY, -hour);
    cal.add(Calendar.MINUTE, minute);

   } else if (hour * 60 < amount) {

    minute = amount - hour * 60;
    cal.add(Calendar.HOUR_OF_DAY, -hour);
    cal.add(Calendar.MINUTE, -minute);

   } else {
    cal.add(Calendar.HOUR_OF_DAY, -hour);
   }

  } else {

   cal.add(Calendar.MINUTE, -amount);
  }
  return cal.getTime();
 }

 public static Date addYear(Date myDate, int amount) {
  GregorianCalendar cal = new GregorianCalendar();
  cal.setTime(myDate);
  boolean isEndDayOfMonth_old = cal
    .getActualMaximum(GregorianCalendar.DAY_OF_MONTH) == cal
    .get(GregorianCalendar.DAY_OF_MONTH);
  cal.add(GregorianCalendar.YEAR, amount);
  boolean isEndDayOfMonth_new = cal
    .getActualMaximum(GregorianCalendar.DAY_OF_MONTH) == cal
    .get(GregorianCalendar.DAY_OF_MONTH);
  if (isEndDayOfMonth_old && !isEndDayOfMonth_new) {
   cal.set(GregorianCalendar.DATE, cal
     .getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
  }
  return cal.getTime();
 }

 /*
  * the mapping from jdk is : 1-Sun; 2-Mon;3-Tues; 4-Weds; 5-Thur;6-Fri;
  * 7-Sat;
  */
 public static int getWeekDay(Date myDate) {
  GregorianCalendar cal = new GregorianCalendar();
  cal.setTime(myDate);
  return cal.get(GregorianCalendar.DAY_OF_WEEK);
 }

 /*
  * the mapping from vas2005 is : 1-Mon;2-Tues; 3-Weds; 4-Thur;5-Fri;
  * 6-Sat;7-Sun;
  */
 public static int getConvertWeekDay(Date myDate) {
  int day = getWeekDay(myDate);
  int result = day - 1;
  if (result == 0)
   result = 7;
  return result;
 }

 public static int getTimeFromDate(Date myDate) {
  SimpleDateFormat sdf = new SimpleDateFormat("hhmmss");
  int result = Integer.parseInt(sdf.format(myDate));
  return result;
 }

 /**
  * 取锟斤拷}锟斤拷时锟斤拷之锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
  *
  * @param startDate
  * @param endDate
  * @return
  */
 public static long getDaysBetweenDate(Date startDate, Date endDate) {
  Calendar cal = Calendar.getInstance();
  cal.setTime(startDate);
  cal.set(Calendar.HOUR, 0);
  cal.set(Calendar.MINUTE, 0);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);
  startDate = cal.getTime();
  cal.setTime(endDate);
  cal.set(Calendar.HOUR, 0);
  cal.set(Calendar.MINUTE, 0);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);
  return (cal.getTime().getTime() - startDate.getTime()) / 86400000;

 }
}

posted on 2008-12-15 17:07 林的 阅读(518) 评论(0)  编辑  收藏 所属分类: util

只有注册用户登录后才能发表评论。


网站导航:
 
<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

想回家了,呵呵!

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜