淘先锋技术网

首页 1 2 3 4 5 6 7

1获取当前系统时间

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

2按时间格式返回

	public static String getCurrentTimeMillis(String formatType) {
		SimpleDateFormat format = new SimpleDateFormat(formatType,
				Locale.getDefault());
		return format.format(System.currentTimeMillis());
	}

3获取系统时间返回时间类型

	public static Date getSystemDate() {
		Calendar calendar = GregorianCalendar.getInstance();
		return calendar.getTime();
	}

4两个时间间隔天数

	public static int getDateNumber(String begTime,String endTime) throws ParseException{
		SimpleDateFormat  df=new  SimpleDateFormat( "yyyy-MM-dd");
		 Date d1,d2;
		 int HM;
		  d1= df.parse(begTime);   
		  d2= df.parse(endTime);
		  if(d2.getTime()>d1.getTime())
		  {
		  HM = (int) (d2.getTime() - d1.getTime()); 
		  }
		  else
		  {
		  HM=(int)(d1.getTime()-d2.getTime());
		  }
		  HM= (int)(HM / (1000 * 60 * 60 *24) );
		 return HM;
	}

5在日期上增加数个整月

	public static Date addMonth(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, n);
		return cal.getTime();
	}

6 在日期上增加数个整天

	public static Date addDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}

7在日期上增加数个小时

	public static Date addHour(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, n);
		return cal.getTime();
	}

8在日期上增加数个分钟

	public static Date addMinute(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, n);
		return cal.getTime();
	}

9得到两个日期时间的差额

	public static long betDate(Date date, Date otherDate) {
		return date.getTime() - otherDate.getTime();
	}

10字符串格式化为日期对象

	public static Date DateFormatTime(String datetime) {
		SimpleDateFormat sdf = null;
		Date d = null;
		try {
			sdf = new SimpleDateFormat(FM_DATE_AND_TIME1, Locale.CHINA);
			d = sdf.parse(datetime);
		} catch (Exception e) {
			d = null;
		}
		return d;
	}

11时间格式化处理:20180808,20180808135036

public static Date parseDataStr(String str,String split){
		if(str!=null&&str.length()>0){
			if(str.length()==14){
				String yyyy = str.substring(0,4);
				String mm = str.substring(4,6);
				String dd = str.substring(6,8);
				String hh = str.substring(8,10);
				String ff = str.substring(10,12);
				String ss = str.substring(12,14);
				StringBuffer result = new StringBuffer("");
				result.append(yyyy).append(split);
				result.append(mm).append(split);
				result.append(dd).append(" ");
				result.append(hh).append(":");
				result.append(ff).append(":");
				result.append(ss).append("");
				if(split.equals("/")){
					return DateFormatTime2(result.toString());
				}else if(split.equals("-")){
					return DateFormatTime1(result.toString()); 
				}else{
					return null;
				}
			}else if(str.length()==8){
				String yyyy = str.substring(0,4);
				String mm = str.substring(4,6);
				String dd = str.substring(6,8);
				StringBuffer result = new StringBuffer("");
				result.append(yyyy).append(split);
				result.append(mm).append(split);
				result.append(dd).append("");
				if(split.equals("/")){
					return DateFormatDay1(result.toString());
				}else if(split.equals("-")){
					return DateFormatDay2(result.toString()); 
				}else{
					return null;
				}
			}
			
			
		}
		return null;
	}

12统计两个日期之间包含的天数

    public static int getDayDiff(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new InvalidParameterException("date1 and date2 cannot be null!");
        }
        long millSecondsInOneDay = 24 * 60 * 60 * 1000;
        return (int) ((date1.getTime() - date2.getTime()) / millSecondsInOneDay);
    }