java 日期工具

package com.papapa.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
 * time 2015-6-19
 * @author 喔哈哈
 * java 日期工具
 */
public class DateTools {
	/*
	 * 获取一个时间,格式为yyyy-MM-dd-hh-mm-ss
	 */
	public String getTime(){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
		 Date d = new Date();
		 String time= dfs.format(d);
		 return time ;
	}
	/*
	 * 获取一个时间,格式为yyyyMMdd
	 */
	public String getTimeYYYYMMdd(){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyyMMdd");
		 Date d = new Date();
		 String time= dfs.format(d);
		 return time ;
	}
	
	/*
	 * 新建一个随机数对象
	 */
	
	private static Random getR = new Random();	
	
	/*
	 * 获取一个随机数
	 */
	
	private String getSuziOne() {
		int getI = getR.nextInt(10) + 48;// 数字48--57=0---9
		String sI = String.valueOf((char) getI);
		return sI;
	}	
	
	/*
	 * 获取一个二十位的数字码
	 */

	public String getcode(){
		String code = "" ;
		for(int i = 0 ; i < 4 ; i++)
			code += getSuziOne() ;
		return code ;
	}
	
	/*
	 * 获取今天的时间,格式为yyyy-MM-dd
	 */
	public String tadayTime(){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
		 Date d = new Date();
		 String time= dfs.format(d);
		 return time ;
	}
	
	/*
	 * 计算两个时间的差值,某天到今天的距离
	 */
	public long chadate(String t2) throws ParseException {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
		 Date d = new Date();
		String t1= dfs.format(d);
		java.util.Date begin = dfs.parse(t1);
		java.util.Date end = dfs.parse(t2);
		long between = (end.getTime() - begin.getTime()) / 1000;

		long day1 = between / (24 * 3600);
		long hour1 = between % (24 * 3600) / 3600;
		long minute1 = between / 600 / 60;
		long second1 = between / 60;		
		return day1;
	}
	
	/*
	 * 计算两个时间的差值
	 */
	public long hedate(String t1,String t2) throws ParseException {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date begin = dfs.parse(t1);
		java.util.Date end = dfs.parse(t2);
		long between = (end.getTime() - begin.getTime()) / 1000;

		long day1 = between / (24 * 3600);
		long hour1 = between % (24 * 3600) / 3600;
		long minute1 = between / 600 / 60;
		long second1 = between / 60;		
		return day1;
	}
	
	/*
	 * 计算一个时间加上天数后的那个日期格式为:yyyy-MM-dd
	 */

	public  String addDate(String date,long day) throws ParseException {
		 SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");
		 java.util.Date date2 = d.parse(date);
     	  long time = date2.getTime();
		  day = day*24*60*60*1000;
		  time+=day;
		  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		  Date newDate= new Date(time);
		   return dateFormat.format(newDate);

		  } 
		 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值