日期类型转换

public static final int HOURS = 0;
public static final int MINUTES = 1;
/**
	 * 将字符串转换成日期格式
	 * @param date
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static Date parseDate(String date,String format) throws Exception{
		if(format==null){
		format="yyyy-MM-dd HH:mm:ss.SSS";
		}
		SimpleDateFormat dateToString=new SimpleDateFormat(format);
		return dateToString.parse(date);
	}
/**
	 * 转date为字符串
	 * @param date 要转换的date
	 * @param format 格式,传null表示用"yyyy-MM-dd HH:mm:ss.SSSZ"为默认格式
	 * @return 格式化以后的字符串日期
	 */
	public static String changeDate(Date date,String format){
		if(format==null){
		format="yyyy-MM-dd HH:mm:ss.SSSZ";
		}
		SimpleDateFormat dateToString=new SimpleDateFormat(format);
		return dateToString.format(date);
	}




/**
	 * 改变时间,返回改变后的字符串
	 * @param date 要改变的date
	 * @param time 改多少(正加,负减)
	 * @param type 改什么(小时,分钟)
	 * @param format 返回字符串的格式,传null表示用"yyyy-MM-dd HH:mm:ss.SSSZ"为默认格式
	 * @return 改变后的字符串
	 */
	public static String addTime(Date date, int time, int type, String format) {
		Calendar c = new GregorianCalendar();//创建日期对象
		c.setTime(date);//设置日期
		switch (type) {
		case 0:
			c.add(Calendar.HOUR_OF_DAY, time);
			break;

		case 1:
			c.add(Calendar.MINUTE, time);
			break;
		}
		
		return changeDate(c.getTime(),format);

	}
/**
	 * 把当地格林威治时间字符串转化为GMT-0的时间字符串,用于储存到数据库
	 * @param param 格林威治格式的时间字符串[例:2012-11-27 10:11:45.038+0800/2012-11-27 10:11:45.038+08]
	 * @return 对应的GMT-0的时间字符串
	 * @throws Exception
	 */
	public static String makeTimeZoneToZero(String param) throws Exception{
		return makeTimeZoneToZero(param,"yyyy-MM-dd HH:mm:ss.SSSZ","yyyy-MM-dd HH:mm:ss.SSS");//格式化日期
		
	}
	
	
	/**
	 * 把当地格林威治时间字符串转化为GMT-0的时间字符串,用于储存到数据库
	 * @param param 格林威治格式的时间字符串
	 * @param format 傳入日期的格式
	 * @return 对应的GMT-0的时间字符串
	 * @throws Exception
	 */
	public static String makeTimeZoneToZero(String param,String format,String resultFormat) throws Exception{
		String zone=param.contains("+")?param.substring(param.indexOf("+")+1):param.substring(param.indexOf("-")+1);//截取字符串从+开始截取0800
		param=zone.length()==4?param:param+"00";//将时区长度为2的补上00
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		Date date=sdf.parse(param);
		
		Calendar c=Calendar.getInstance();
		c.setTime(date);
		
		long srcTime=c.getTimeInMillis();//获得时间的long值
		long targetTime=srcTime-TimeZone.getDefault().getRawOffset();//用当前时区减去默认时区
		
		sdf=new SimpleDateFormat(resultFormat);
		Date newDate=new Date(targetTime);
		return sdf.format(newDate);//格式化日期
		
	}
	
	/**
	 * 把数据库中GMT-0的时间转户为用户给的时区的时间字符串
	 * @param param 数据库中的GMT-0时间
	 * @param timeZone 时区[-12...,-8,...,-1,0,1,.....,8,.....12]
	 * @return 相应时区的时间字符串
	 * @throws Exception
	 */
	public static String makeTimeZoneToLocal(String param,int timeZone) throws Exception{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		Date date=sdf.parse(param);
		Calendar c=Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR_OF_DAY, timeZone);
		return sdf.format(c.getTime());
		
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值