android utils 日期工具

public class TimeUtil {
	@SuppressLint("SimpleDateFormat")
	public static String getCurrentDay(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(new Date());
	}

	/**
	 * 获取当前日期
	 * @param format 日期格式
	 * @return 当前日期
	 */
	public static String getCurrentDay(String format){
		SimpleDateFormat format1 = new SimpleDateFormat(format);
		return format1.format(new Date());
	}

	/**
	 * 根据输入的日期date,由day确定增加day天,或者减少day天
	 * @param date  输入一个日期
	 * @param day 增加或减少的天数
	 * @return
	 */
	public static String dateIncreaseAndDecrease(String date,int day){

		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			d = sdf.parse(date);
		}catch (ParseException e){
			e.printStackTrace();
		}
		if (day==0){
			return sdf.format(new Date(d.getTime()));
		}
		return  sdf.format(new Date(d.getTime()+day*24*60*60*1000));
	}

	/**
	 * 根据yyyy-MM-dd获取毫秒
	 * @param time
	 * @return 0 解析失败
	 */
	public static long getMillisByYmd(String time){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		long millis = 0;
		try {
			Date parse = format.parse(time);
			millis = parse.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			millis = 0;
		}
		return millis;
	}

	/**
	 * 根据date获取毫秒
	 * @param time
	 * @return 0 解析失败
	 */
	public static long getMillisByDate(String time){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long millis = 0;
		try {
			Date parse = format.parse(time);
			millis = parse.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			millis = 0;
		}
		return millis;
	}
	
	/**
	 * 
	 * @param time
	 *            "HH:mm:ss"
	 * @return millis of time
	 */
	public static long getMillis(String time) {
		if (TextUtils.isEmpty(time))
			return 0;
		if(!time.contains(":"))
			return 0;
		String[] times = time.split(":");
		int hour = Integer.parseInt(times[0]);
		int min = Integer.parseInt(times[1]);
		// 02:02:47.00
		String millis = times[2];
		if (millis != null && millis.length()>2) {
			millis = millis.substring(0, 2);
		}
		int sec = Integer.parseInt(millis);
		long length = hour * 3600 + min * 60 + sec;
		return length = length * 1000;
	}
	
	/**
	 * 
	 * @param millis
	 *            second * 1000
	 * @return time "HH:mm:ss"
	 */
	@SuppressLint("DefaultLocale")
	public static String getTime(long millis) {

		long length = millis / 1000;
		long hour = length / 3600;
		long min = (length - hour * 3600) / 60;
		long sec = length - hour * 3600 - min * 60;
		String time = String.format("%02d:%02d:%02d", hour, min, sec);
		return time;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值