关闭

java日期工具类

标签: java日期工具类
2323人阅读 评论(0) 收藏 举报
分类:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.httpclient.util.DateUtil;

import com.zgxcw.framework.util.Assert;



/**
 * @Title: DateUtils
 * @Description: 日期相关工具类

 */
public final class DateUtils {

  // 短日期格式
  public static String DATE_FORMAT = "yyyy-MM-dd";

  // 长日期格式
  public static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

  /**
   * 将数字转成日期字符串
   * 
   * @param num
   * @return
   */
  public static String num2Date(Long num) {
    if (null == num || num == 0) {
      return "";
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    String dateStr = df.format(new Date(num));
    return dateStr;
  }

  /**
   * 获取当天0点的毫秒数
   * 
   * @return
   */
  public static long todayStartMillis() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return (cal.getTimeInMillis());
  }

  /**
   * 获取当天24点的毫秒数
   * 
   * @return
   */
  public static long todayEndMillis() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 24);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return (cal.getTimeInMillis());
  }

  /**
   * 获取当月0点的毫秒数
   * 
   * @return
   */
  public static long monthStartMillis() {
    Calendar cal = Calendar.getInstance();
    cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,
        0);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
    return (cal.getTimeInMillis());
  }

  /**
   * 获取当月24点的毫秒数
   * 
   * @return
   */
  public static long monthEndMillis() {
    Calendar cal = Calendar.getInstance();
    cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,
        0);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    cal.set(Calendar.HOUR_OF_DAY, 24);
    return (cal.getTimeInMillis());
  }

  /**
   * 30天前0点毫秒数
   * 
   * @return
   */
  public static long before30DayStartMillis() {
    return DateUtils.todayEndMillis() - (86400000l * 30);
  }

  /**
   * 获取时间的毫秒数
   * 
   * @param date
   * @param format
   * @return
   */
  public static long convert2long(String date, String format) {
    try {
      if (!Assert.isNull(date)) {
        if (Assert.isNull(format)) {
          format = DateUtils.TIME_FORMAT;
        }
        SimpleDateFormat sf = new SimpleDateFormat(format);
        return sf.parse(date).getTime();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return 0l;
  }

  /**
   * 返回两个日期相差的天数
   * 
   * @param startDate 开始日期
   * @param endDate 结束日期
   * @return 相差天数
   */
  public static int getDateSpace(Long startDate, Long endDate) {
    Calendar calst = Calendar.getInstance();;
    Calendar caled = Calendar.getInstance();

    calst.setTimeInMillis(startDate);
    caled.setTimeInMillis(endDate);

    // 设置时间为0时
    calst.set(Calendar.HOUR_OF_DAY, 0);
    calst.set(Calendar.MINUTE, 0);
    calst.set(Calendar.SECOND, 0);
    caled.set(Calendar.HOUR_OF_DAY, 0);
    caled.set(Calendar.MINUTE, 0);
    caled.set(Calendar.SECOND, 0);
    // 得到两个日期相差的天数
    int days =
        ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;

    return days;
  }

  /**
   * 将毫秒数转成 几天几小时几分几秒
   * 
   * @param mss
   * @return
   */
  public static String formatDuring(long mss) {
    long days = mss / (1000 * 60 * 60 * 24);
    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
    long seconds = (mss % (1000 * 60)) / 1000;
    return days + " 天 " + hours + " 小时 " + minutes + " 分 " + seconds + " 秒 ";
  }

 
  
  /**
   * @Description:日期格式转换Long型
   * @return:Long
   * @param:String
   * @Date:2015-10-23 上午10:31:55
   */
  public static Long strToLong(String source) {
	  if (!Assert.isNull(source)) {
		  if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
			  return DateUtils.convert2long(source, DateUtils.DATE_FORMAT);
		  }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
			  return DateUtils.convert2long(source, DateUtils.TIME_FORMAT);
		  }
	  }
	  return null;
  }
  
   /**
    * 获取时间指定格式的时间字符串
    * 
   * @param format 需要显示字符串的指定格式
   * @param date 时间
   * @return
   */
  public static String getDateStrByFormat(String format,Date date){
	  SimpleDateFormat df = new SimpleDateFormat(format);
	  String dateStr = df.format(date);
	  return dateStr;
  }
  
  
   /**
    * 获取yyyy-MM-dd HH:mm:ss格式的时间字符串
    * 
   * @param date
   * @return
   */
  public static String getFullAppointDateStr(Date date){
	  SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT);
	  String dateStr = df.format(date);
	  return dateStr;
  }
  /**
   * 时间 加 某月
   * @param dateTime
   * @param month
   * @return
   */
  public static Long addMonth(Long dateTime,int month){
	  GregorianCalendar gc=new GregorianCalendar(); 
	  gc.setTimeInMillis(dateTime);
	  gc.add(2,+3);
	  return gc.getTimeInMillis();
  }
  

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场