日期处理

// 给当前时间加一个时间
	public static String addDate(String day, int x) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
		// SimpleDateFormat format = new
		// SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//12小时制
		Date date = null;
		try {
			date = format.parse(day);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (date == null)
			return "";
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, x);// 24小时制
		// cal.add(Calendar.HOUR, x);12小时制
		date = cal.getTime();
		cal = null;
		return format.format(date);
	}

	// 给日期减掉一个月
	public static String addDays(String day, int x) {
		int intYear = Integer.parseInt(day.substring(0, 4));
		int intMonth = Integer.parseInt(day.substring(5, 7));
		int intDate = Integer.parseInt(day.substring(8, 10));

		Calendar cal = Calendar.getInstance();
		cal.set(intYear, intMonth - (x + 1), intDate);
		cal.add(Calendar.DAY_OF_MONTH, 0);
		Date date = cal.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	// 给日期减掉一天
	public static String addDay(String day, int x) {
		int intYear = Integer.parseInt(day.substring(0, 4));
		int intMonth = Integer.parseInt(day.substring(5, 7));
		int intDate = Integer.parseInt(day.substring(8, 10));

		Calendar cal = Calendar.getInstance();
		cal.set(intYear, intMonth - (1), intDate);
		cal.add(Calendar.DAY_OF_MONTH, -x);
		Date date = cal.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}
  /** * 将时间转换成指定格式的字符串 * * @param time * Date * @param format * 格式描述:例如:yyyy-MM-dd yyyy-MM-dd HH:mm:ss * @return */ public static String formatTime(java.util.Date time, String format) { if (time != null) { java.text.SimpleDateFormat DATE_FORMAT = new java.text.SimpleDateFormat( format); return DATE_FORMAT.format(time); } else { return ""; } }

 

/**
  * 获取当时时间
  *
  * @return 返回int型日期
  */
 public static int getNowDatetime() {
  return (int) (System.currentTimeMillis() / 1000);
 }

/**
  * 获取当前时间 string 格式
  *
  * @return
  */
 public static String GetDate() {
  Calendar calendar = Calendar.getInstance();
  SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
  return simpledateformat.format(calendar.getTime());
 }
/**
     * 两个时间相差距离多少天多少小时多少分多少秒
     * @param str1 时间参数 1 格式:1990-01-01 12:00:00
     * @param str2 时间参数 2 格式:2009-01-01 12:00:00
     * @return long[] 返回值为:{天, 时, 分, 秒}
     */
    public static Long[] getDistanceTimes(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff ;
            if(time2>time1) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            //day = diff / (24 * 60 * 60 * 1000);
            //hour = (diff / (60 * 60 * 1000) - day * 24);
            hour = (diff / (60 * 60 * 1000));
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Long[] times = {day, hour, min, sec};
        return times;
    }

  

/**
	 * 通过yyyy-MM-dd参数获取该月第一天
	 *
	 * @param strdate
	 *            格式yyyy-MM-dd
	 * @return String 日期字符串
	 */
	public static String getFirstDayOfMonth(String strdate) {
		String year;
		String month;
		String day;
		SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		try {
			date = dateFormate.parse(strdate);
		} catch (Exception ex) {
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		year = String.valueOf(cal.get(Calendar.YEAR));
		month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		// day=String.valueOf(FullMonth.getFirstDay(strdate));
		day = "01";
		if (Integer.parseInt(month) < 10) {
			month = "0" + month;
		}
		return year + "-" + month + "-" + day;
	}

	/**
	 * 通过yyyy-MM-dd参数获取该月最后一天
	 *
	 * @param strdate
	 *            格式yyyy-MM-dd
	 * @return String 日期字符串
	 */
	public static String getLastDayOfMonth(String strdate) {
		String year;
		String month;
		String day;
		SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		try {
			date = dateFormate.parse(strdate);
		} catch (Exception ex) {
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		year = String.valueOf(cal.get(Calendar.YEAR));
		month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		day = String.valueOf(DateTime.getLastDay(strdate));
		if (Integer.parseInt(month) < 10) {
			month = "0" + month;
		}
		if (Integer.parseInt(day) < 10) {
			day = "0" + day;
		}
		return year + "-" + month + "-" + day;
	}

 

/**
	 * 将数字转化成日期
	 *
	 * @param x
	 * @return
	 */
	public static String outweek(int x) {
		String[] weeks = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
		if (x > 0 && x < 8) {
			return weeks[x - 1];
		}
		return "未知";
	}

 

/**
	 * 获得某时间段中的天数
	 *
	 * @param sum
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static int getSomeday(Date bdate,Date smdate) {
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
	        try {
				smdate=sdf.parse(sdf.format(smdate));
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}  
	        try {
				bdate=sdf.parse(sdf.format(bdate));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
	        Calendar cal = Calendar.getInstance();    
	        cal.setTime(smdate);    
	        long time1 = cal.getTimeInMillis();                 
	        cal.setTime(bdate);    
	        long time2 = cal.getTimeInMillis();         
	        long between_days=(time2-time1)/(1000*3600*24);  
	            
	       return Integer.parseInt(String.valueOf(between_days));  
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值