joda-time实现Timehelper

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;

public class TimeHelper {
   public final static String DATE_FORMAT_YEAR_MONTH_1 = "yyyy.MM";
   public final static String DATE_FORMAT_YEAR_MONTH_2 = "yyyy-MM";
   public final static String DATE_FORMAT_YEAR_MONTH_3 = "yyyyMM";
    public final static String DATE_FORMAT_YEAR_MONTH_4 = "yyyy年MM月";

   public final static String DATA_FORMAT_YEAR_MONTH_DAY_1 = "yyyy-MM-dd";
   public final static String DATA_FORMAT_YEAR_MONTH_DAY_2 = "yyyy.MM.dd";
   public final static String DATA_FORMAT_YEAR_MONTH_DAY_3 = "yyyy年MM月dd日";
   public final static String DATA_FORMAT_YEAR_MONTH_DAY_4 = "yyyyMMdd";
    public final static String DATA_FORMAT_YEAR_MONTH_DAY_5 = "yyyy年MM月dd日HH时";
   public final static String DATA_FORMAT_YEAR_MONTH_DAY_6 = "yyyyMMddHHmmss";

   public final static String DATE_TIME_FORMAT_DEFAULT_1 = "yyyy.MM.dd HH:mm:ss";
   public final static String DATE_TIME_FORMAT_DEFAULT_2 = "yyyy-MM-dd HH:mm:ss";

   public final static String DATE_TIME_FORMAT_DEFAULT_3 = "HH:mm";
   public final static String DATE_TIME_FORMAT_DEFAULT_4 = "yyyyMMddHHmmss";
   public final static String DATE_TIME_FORMAT_DEFAULT_5 = "yyyy-MM-dd HH:mm";
   public final static String DATE_TIME_FORMAT_DEFAULT_6 = "MM/dd/yyyy HH:mm";
    public final static String DATE_TIME_FORMAT_DEFAULT_7 = "yyyy-MM-dd HH";
   public final static String DATE_TIME_FORMAT_DEFAULT_8 = "MMM dd,yyyy KK:mm:ss aa";

   public final static String[] WEEK_US_ARRAY = new String[] { "", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY" };
   public final static String[] WEEK_CN_ARRAY = new String[] { "", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天" };
   public final static String[] WEEK_TODAY_CN_ARRAY = new String[] { "", "周一", "周二", "周三", "周四", "周五", "周六", "周日" };


   public static Date getDateByYear(int year) {
      DateTime dt = new DateTime(year, 1, 1, 0, 0, 0, 0);
      return dt.toDate();
   }

   public static Date getDateByYear(String year) {
      DateTime dt = new DateTime(Integer.parseInt(year), 1, 1, 0, 0, 0, 0);
      return dt.toDate();
   }

   public static Date addYear(Date date, int addNum) {
      DateTime dt = new DateTime(date.getTime()).plusYears(addNum);
      return dt.toDate();
   }

   public static Date getDateByMonth(int year, int month) {
      DateTime dt = new DateTime(year, month, 1, 0, 0, 0, 0);
      return dt.toDate();
   }

   public static Date getDateByMonth(String year, String month) {
      DateTime dt = new DateTime(Integer.parseInt(year), Integer.parseInt(month), 1, 0, 0, 0, 0);
      return dt.toDate();
   }

   public static Date addMonth(Date date, int addNum) {
      DateTime dt = new DateTime(date.getTime()).plusMonths(addNum);
      return dt.toDate();
   }

   /**
    * 获取指定日期所在天的最后一秒的时间 比如1013-11-14 23:59:59
    */
   public static Date getDateWithLastSecond(Date date) {
      DateTime dt = new DateTime(date.getTime()).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
      return dt.plusDays(1).minusSeconds(1).toDate();
   }

   public static Date getDateByDate(int year, int month, int date) {
      DateTime dt = new DateTime(year, month, date, 0, 0, 0, 0);
      return dt.toDate();
   }

   public static Date getDateByDate(String year, String month, String date) {
      DateTime dt = new DateTime(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(date), 0, 0, 0, 0);
      return dt.toDate();
   }

   public static Date addDate(Date date, int addNum) {
      DateTime dt = new DateTime(date.getTime()).plusDays(addNum);
      return dt.toDate();
   }

   public static Date addMinutes(Date date, int minutes) {
      DateTime dt = new DateTime(date.getTime()).plusMinutes(minutes);
      return dt.toDate();
   }

   public static Date addSeconds(Date date, int seconds) {
      DateTime dt = new DateTime(date.getTime()).plusSeconds(seconds);
      return dt.toDate();
   }

   public static Date firstDateOfMonth(Date date) {
      DateTime dt = new DateTime(date.getTime()).withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
      return dt.toDate();
   }

   public static Date lastDateOfMonth(Date date) {
      DateTime dt = new DateTime(date.getTime()).withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0).plusMonths(1).minusMillis(1);
      return dt.toDate();
   }

   public static Date firstTimeOfDay(Date date) {
      DateTime dt = new DateTime(date.getTime()).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
      return dt.toDate();
   }

   public static Date lastTimeOfDay(Date date) {
      DateTime dt = new DateTime(date.getTime()).withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59).withMillisOfSecond(59);
      return dt.toDate();
   }

   public static boolean isInMonth(Date baseDate, Date compareDate) {
      Date startTime = firstDateOfMonth(baseDate);
      Date endTime = addMonth(startTime, 1);
      return isInTimeZone(startTime, endTime, compareDate);
   }

   public static boolean isInTimeZone(Date startTime, Date endTime, Date compareDate) {
      return startTime.getTime() <= compareDate.getTime() && endTime.getTime() > compareDate.getTime();
   }

   public static SimpleDateFormat getTemplateFormat(String type) {
      return new SimpleDateFormat(type);
   }

   public static String formatDate(Date date, String template) {
      return new DateTime(date).toString(template);
   }

   public static String getDayOfWeekUSString(Date date) {
      DateTime dt = new DateTime(new Date().getTime());
      return WEEK_US_ARRAY[dt.getDayOfWeek()];
   }

   public static String getDayOfWeekCNString(Date date) {
      DateTime dt = new DateTime(new Date().getTime());
      return WEEK_CN_ARRAY[dt.getDayOfWeek()];
   }

   public static Date getDate(String date, String formatStr) {
        if (EmptyChecker.isEmpty(date)) {
            return null;
        }
      try {
         return getTemplateFormat(formatStr).parse(date);
      } catch (ParseException e) {
         throw new RuntimeException(e);
      }
   }

   public static String convert2Today(String en) {
      int k = 0;
      for (String str : WEEK_US_ARRAY) {
         if (str.equalsIgnoreCase(en)) {
            return WEEK_TODAY_CN_ARRAY[k];
         } else {
            k++;
         }
      }
      return null;
   }

   public static Date lastDateOfDay(Date date) {
      DateTime dt = new DateTime(firstTimeOfDay(addDate(date, 1))).minus(1);
      return dt.toDate();
   }

    public static Date getDateFromMillis(Long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    }

   public static void main(String[] args) {
      System.out.println(getDayOfWeekUSString(new Date()));
      System.out.println(getDayOfWeekCNString(new Date()));
      Date date1 = TimeHelper.getDate("2014-09-04 13:00" + ":01", TimeHelper.DATE_TIME_FORMAT_DEFAULT_2);
      System.out.println(date1);

      Date date2 = TimeHelper.getDate("2014-09-04 13:00", TimeHelper.DATE_TIME_FORMAT_DEFAULT_5);
      System.out.println(date2);
      System.out.println(formatDate(new Date(),DATE_FORMAT_YEAR_MONTH_3));
   }

}

你可能感兴趣的:(Joda,DateUtil,timehelper)