整理一个时间的操作类(android)

本文介绍了一个实用的时间操作工具类,提供了多种方法来处理日期和时间的比较、格式化及差值计算等功能。包括时间差计算、时间格式转换、获取当前时间戳等常见需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 package com.ejoy.android.utils;

import android.annotation.SuppressLint;
import android.content.Context;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@SuppressLint("SimpleDateFormat")
public class DateOperation {

	/**
	 * 
	 * @param begin
	 *            开始时间
	 * @param end
	 *            当前时间
	 * @param dateFormate
	 *            时间格式
	 * @return 两个时间点之间的差
	 */
	public static long compareTime(String end, SimpleDateFormat dateFormate) {
		long min = 0;
		try {
			String dfdate1 = dateFormate.format(new Date());
			Date date1 = dateFormate.parse(dfdate1);

			Date date2 = (Date) dateFormate.parseObject(end.replace('/', '-'));
			long diff = date2.getTime() - date1.getTime();
			min = diff / (1000 * 60); // 分钟
			if (min < 0) {
				// 错误
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return min;
	}

	/**
	 * @param begin
	 *            开始时间
	 * @param end
	 *            当前时间
	 * @param dateFormate
	 *            时间格式
	 * @return 两个时间点之间的差,单位分钟 返回正数,负数返回绝对值
	 */
	public static long compareTime2(String end, SimpleDateFormat dateFormate) {
		return Math.abs(compareTime(end, dateFormate));
	}

	public static String GetDateFormatTodays(long min) {

		try {
			if (min > 60) {
				if (min > 60 * 24) {
					long a1 = 0;
					long a2 = 0;
					long a6 = 0;
					long a3 = 0;
					a1 = min / (60 * 24);
					a6 = min % (60 * 24);
					if (a6 > 60) {
						a2 = a6 / 60;
						a3 = a6 % 60;
					} else {
						a3 = a6;
					}
					return a1 + "天" + a2 + "小时" + a3 + "分钟";
				} else {
					long a4 = 0;
					long a5 = 0;
					a4 = min / 60;
					a5 = min % 60;
					return a4 + "小时" + a5 + "分钟";
				}
			} else {
				return min + "分钟";
			}
		} catch (Exception er) {
			er.printStackTrace();
			return "";
		}
	}

	public static String getTimeDiff(Date date) {
		Calendar cal = Calendar.getInstance();
		long diff = 0;
		Date dnow = cal.getTime();
		String str = "";
		diff = dnow.getTime() - date.getTime();
		if (diff > 24 * 60 * 60 * 1000) {
			str = "1天前";
		} else if (diff > 5 * 60 * 60 * 1000) {
			str = "2小时前";
		} else if (diff > 1 * 60 * 60 * 1000) {
			str = "1小时前";
		} else if (diff > 30 * 60 * 1000) {
			str = "30分钟前";
		} else if (diff > 15 * 60 * 1000) {
			str = "15分钟前";
		} else if (diff > 5 * 60 * 1000) {
			str = "5分钟前";
		} else if (diff > 1 * 60 * 1000) {
			str = "1分钟前";
		} else {
			str = "刚刚";
		}
		return str;
	}

	public static String getTimeDiff(String date) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		long diff = 0;
		Date dnow = cal.getTime();
		String str = "";
		Date date2 = df.parse(date);
		diff = dnow.getTime() - date2.getTime();
		if (diff > 7 * 24 * 60 * 60 * 1000) {
			str = "7天前 " + date2.getHours() + ":" + date2.getMinutes();
		} else if (diff > 6 * 24 * 60 * 60 * 1000) {
			str = "6天前 " + date2.getHours() + ":" + date2.getMinutes();
		} else if (diff > 5 * 24 * 60 * 60 * 1000) {
			str = "5天前 " + date2.getHours() + ":" + date2.getMinutes();
		} else if (diff > 4 * 24 * 60 * 60 * 1000) {
			str = "4天前 " + date2.getHours() + ":" + date2.getMinutes();
		} else if (diff > 3 * 24 * 60 * 60 * 1000) {
			str = "3天前 " + date2.getHours() + ":" + date2.getMinutes();
		} else if (diff > 2 * 24 * 60 * 60 * 1000) {
			str = "前天 " + date2.getHours() + ":" + date2.getMinutes();
		} else if (diff > 24 * 60 * 60 * 1000) {
			str = "昨天 " + date2.getHours() + ":" + date2.getMinutes();
		} else if (diff > 5 * 60 * 60 * 1000) {
			str = "2小时前";
		} else if (diff > 1 * 60 * 60 * 1000) {
			str = "1小时前";
		} else if (diff > 30 * 60 * 1000) {
			str = "30分钟前";
		} else if (diff > 15 * 60 * 1000) {
			str = "15分钟前";
		} else if (diff > 5 * 60 * 1000) {
			str = "5分钟前";
		} else if (diff > 1 * 60 * 1000) {
			str = "1分钟前";
		} else {
			str = "刚刚";
		}
		return str;
	}

	public static String getTimeDiffDay(String date) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();

		Date dnow = cal.getTime();
		String str = "";
		Date date2 = df.parse(date);

		int diffyear = dnow.getYear() - date2.getYear();
		if (diffyear == 0) {
			int diffmonth = dnow.getMonth() - date2.getMonth();
			if (diffmonth == 0) {
				int diffday = dnow.getDay() - date2.getDay();
				if (diffday == 0) {
					str = "今天" + date2.getHours() + ":" + date2.getMinutes();
				} else if (diffday == 1) {
					str = "昨天" + date2.getHours() + ":" + date2.getMinutes();
				} else if (diffday == 2) {
					str = "前天" + date2.getHours() + ":" + date2.getMinutes();
				} else {
					str = diffday + "天以前" + date2.getHours() + ":"
							+ date2.getMinutes();
				}
			} else {
				str = diffmonth + "月以前";
			}
		} else {
			str = diffyear + "年以前";
		}
		return str;
	}

	public static String DateFormatProcess(String DateTimeString,
			SimpleDateFormat dateFormate) {
		SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date1 = dateFormate.parse(DateTimeString);
			if (date1.getHours() == 0) {
				return dateformat1.format((dateFormate.parse(DateTimeString)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DateTimeString;
	}

	/***
	 * 获取时间的年月日
	 * 
	 * @param DateTimeString
	 * @return
	 */
	public static String DateFormatProcess(String DateTimeString) {
		SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return dateformat1.format((dateformat1.parse(DateTimeString)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DateTimeString;
	}

	public static String GetSyscurrentTimeMillis() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		Date curDate = new Date(System.currentTimeMillis());
		return formatter.format(curDate);
	}

	public static String GetSyscurrentTimeMillis(int iMin) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		Date curDate = new Date(System.currentTimeMillis() + iMin * 60 * 1000);
		return formatter.format(curDate);
	}

	public static String GetSysCurrentTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}

	/**
	 * @param iMin
	 *            分钟 需要增加的分钟数
	 * @return 获取当前的时间
	 */
	public static String GetSysCurrentTime(int iMin) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date curDate = new Date(System.currentTimeMillis() + iMin * 60 * 1000);// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}

	public static int GetCurrentDay(String datetimeString) {
		Calendar cal = new GregorianCalendar();
		SimpleDateFormat oSdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			cal.setTime(oSdf.parse(datetimeString));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	public static int GetCurrentMonth(String datetimeString) {
		Calendar cal = new GregorianCalendar();
		SimpleDateFormat oSdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			cal.setTime(oSdf.parse(datetimeString));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return cal.get(Calendar.MONTH);
	}

	public static int GetCurrentYear(String datetimeString) {
		Calendar cal = new GregorianCalendar();
		SimpleDateFormat oSdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			cal.setTime(oSdf.parse(datetimeString));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return cal.get(Calendar.YEAR);
	}

	public static int GetCurrentHour(String datetimeString, Context mContext) {
		Calendar cal = new GregorianCalendar();
		SimpleDateFormat oSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			cal.setTime(oSdf.parse(datetimeString));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		boolean a = android.text.format.DateFormat.is24HourFormat(mContext);
		boolean b = false;
		if (!a) {
			// 12小时
			int am = cal.get(Calendar.AM_PM);
			if (am == 1) {
				// pm
				b = true;
			}
		}
		return b == true ? cal.get(Calendar.HOUR) + 12 : cal.get(Calendar.HOUR);
	}

	public static int GetCurrentMinute(String datetimeString) {
		Calendar cal = new GregorianCalendar();
		SimpleDateFormat oSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			cal.setTime(oSdf.parse(datetimeString));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return cal.get(Calendar.MINUTE);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值