必威体育Betway必威体育官网
当前位置:首页 > IT技术

时间单位换算 与不同格式显示

时间:2019-11-03 05:15:41来源:IT技术作者:seo实验室小编阅读:76次「手机版」
 

时间单位换算

调用这个方法 传入一个时间 让该时间与当前系统时间作对比

/**
 * 设置时间
 *
 * @return
 */
private String getTime(long mTime) {
    oldTime = system.currenttimemillis();
    //DateUtils  时间换算工具类
    String format = DateUtils.format(mTime, "MM月dd日 HH:mm");
    String format1 = DateUtils.format(mTime, "mm 分钟前");
    String format2 = "刚刚";
    if (oldTime - mTime >= 30 * 60 * 1000) {// 如果超过30分钟,显示时间
        mTime = oldTime;
        return format;
    }
    if (oldTime - mTime >= 5 * 60 * 1000) {// 如果超过5分钟,显示几分钟前
        mTime = oldTime;
        return format1;
    }
    if (oldTime - mTime >= 3 * 60 * 1000) {// 如果超过3分钟,显示刚刚
        mTime = oldTime;
        return format2;
    } else {
        return "";
    }
}

** 下面附上 DateUtils 时间换算工具类**

@SuppressLint("simpledateformat")
public final class DateUtils {

public static String format(Date date, String format) {

	if (date == null || TextUtils.isempty(format)) {
		return null;
	}
	SimpleDateFormat sdf = new SimpleDateFormat(format);
	return sdf.format(date);
}

public static String format(long timeMillion, String format){
	if(timeMillion < 31507200000L){
		timeMillion *= 1000;
	}
	return format(new Date(timeMillion), format);
}

/**
 * 
 * @param date
 *            long 20140507
 * @param format
 * @return
 */
public static String format(long date, long time, String format) {
	try {
		Date d = getDateFromNumber(date,time);
		return format(d, format);
	} catch (Exception e) {
		
	}
	return null;
}

/**
 * 处理符合时间格式字符串
 * @param dateStr
 * @param format
 * @return
 */
public static Date parser(String dateStr, String format) {

	if (TextUtils.isEmpty(dateStr) || TextUtils.isEmpty(format)) {
		return null;
	}
	SimpleDateFormat sdf = new SimpleDateFormat(format);
	try {
		return sdf.parse(dateStr);
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		return null;
	}
}


public static String getAmonthAgo(String format) {
	calendar calendar = Calendar.getinstance();
	calendar.setTimeInMillis(System.currentTimeMillis());
	calendar.add(Calendar.MONTH, -1);
	return format(calendar.getTime(), format);
}

public static String getAmonthAgo(Date endDate, String format) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(endDate);
	calendar.add(Calendar.MONTH, -1);
	return format(calendar.getTime(), format);
}

/**
 * 处理非时间格式字符串如:date=20141010 time=163022
 * @param date
 * @param time
 * @return
 */
public static Date getDateFromNumber(long date, long time) {

	long year = date / 10000;
	long month = (date % 10000) / 100;
	long day = date % 100;
	long hour = time / 10000;
	long minute = (time % 10000) / 100;
	long second = time % 100;

	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, (int) year);
	calendar.set(Calendar.MONTH, (int) month - 1);
	calendar.set(Calendar.DAY_OF_MONTH, (int) day);

	calendar.set(Calendar.HOUR_OF_DAY, (int) hour);
	calendar.set(Calendar.MINUTE, (int) minute);
	calendar.set(Calendar.SECOND, (int) second);

	return calendar.getTime();
}

public static String getDateFromNumber(long date, long time, String format) {

	return DateUtils.format(getDateFromNumber(date, time), format);

}

public static boolean isInOneMonth(String startDateStr, String endDateStr) {

	Date start = parser(startDateStr, "yyyyMMdd");
	if (start == null) {
		return false;
	}
	Date end = parser(endDateStr, "yyyyMMdd");
	if (end == null) {
		return false;
	}

	Calendar calendar = Calendar.getInstance();
	calendar.setTime(end);
	calendar.add(Calendar.MONTH, -1);

	Date monthAgo = calendar.getTime();

	if (start.compareTo(monthAgo) >= 0) {
		return true;
	}
	return false;
}

public static boolean isInOneMonth(Date startDate, Date endDate) {

	if (startDate == null || endDate == null) {
		return false;
	}

	Calendar calendar = Calendar.getInstance();
	calendar.setTime(endDate);
	calendar.add(Calendar.MONTH, -1);

	Date monthAgo = calendar.getTime();

	if (startDate.compareTo(monthAgo) >= 0) {
		return true;
	}
	return false;
}
/**
 * 
 * @return null 时间格式错误
 */
public static String getTimeAgoString(String dateStr,String paramFormat){
	Date date = parser(dateStr, paramFormat);
	if(date == null){
		return dateStr;
	}
	long timeMillion = date.getTime();
	String result = getTimeAgoString(timeMillion,paramFormat);
	if(TextUtils.isEmpty(result)){
		return dateStr;
	}
	return result;
}

/**
 * 
 * @return null 时间格式错误
 */
public static String getTimeAgoString(long timeMillion,String resultFormat){
	
	if(timeMillion < 31507200000L){
		timeMillion *= 1000;
	}
	long currTime = System.currentTimeMillis();
	
	long timeSub = currTime - timeMillion;
	if(timeSub < 0){
		return "刚刚";
	}
	
	if(timeSub < (1 * 60 * 1000)){
		return "刚刚";
	}
	
	if(timeSub < (60 * 60 * 1000)){
		long t = timeSub / (60 * 1000);
		return t+"分钟前";
	}
	
//		if(timeSub < (60 * 60 * 1000)){
//			return "半小时前";
//		}
	
	if(timeSub < (24 * 60 * 60 * 1000)){
		long t = timeSub / (60 * 60 * 1000);
		return t+"小时前";
	}
	
	Calendar yesterday = Calendar.getInstance();
	yesterday.setTimeInMillis(System.currentTimeMillis());
	yesterday.add(Calendar.DAY_OF_MONTH, -1);
	yesterday.set(Calendar.HOUR, 0);
	yesterday.set(Calendar.MINUTE, 0);
	yesterday.set(Calendar.SECOND, 0);
	yesterday.set(Calendar.MILLISECOND, 0);
	
	if(timeMillion > yesterday.getTimeInMillis()){
		
		return "昨天"+format(timeMillion,"HH:mm");
	}
	
	Calendar calendar = Calendar.getInstance();
	calendar.setTimeInMillis(timeMillion);
	int defYear = calendar.get(Calendar.YEAR);
	
	Calendar currCalendar = Calendar.getInstance();
	currCalendar.setTimeInMillis(System.currentTimeMillis());
	int currYear = currCalendar.get(Calendar.YEAR);
	if(defYear == currYear){
		return format(timeMillion,"MM-dd HH:mm");
	}else{
		return format(timeMillion,"yyyy-MM-dd");
	}
	
}


public static Date getDateByMillis(long time){
	if(time<=0){
		return null;
	}
	  Date dat=new Date(time); 
	 return dat;
}
public static String getStringDate(Date date){
	
	if(date==null){
		return "";
	}
	 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	return format.format(date);  
		       

	
}
public static String getStringDateByMillis(long time ){

	return getStringDate(getDateByMillis(time));

	
}

public static boolean isToday(long time){
	Calendar calendar = Calendar.getInstance();
	
	
	return true;
	
}

/**
 * 知我
 */

private static final SimpleDateFormat formatter_INPUT = new SimpleDateFormat(
		"EEE MMM dd HH:mm:ss Z yyyy", locale.US);
private static final SimpleDateFormat FORMATTER_OUTPUT = new SimpleDateFormat(
		"yyyy年MM月dd日 HH:mm");
private static final SimpleDateFormat FORMATTER_INPUT2 = new SimpleDateFormat(
		"yyyyMMddHHmmss");
/**
 * 格式化日期
 * @param date
 * @return
 */
public static String paserDate(String date) {
	try {
		Date myDate = FORMATTER_INPUT2.parse(date);
		Date curDate = new Date();
		long dif = curDate.getTime() - myDate.getTime();

		long temp = 0l;
		if (isToday(myDate)) {
			if (dif < 0) {
				return "1秒前";
			}

			temp = dif / (60L * 60L * 1000L);
			if (temp > 0) {

				return temp + "小时前";
			}
			temp = dif / (60 * 1000L);
			if (temp > 0) {

				return temp + "分钟前";
			}
			temp = dif / (1000L);
			if (temp > 0) {
				return temp + "秒前";
			}
		}

		return FORMATTER_OUTPUT.format(myDate);

	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printstacktrace();
	}
	return null;
}

/**
 * 判断传入的日期是否是今天
 * @param mDate
 * @return
 */
public static boolean isToday(Date mDate) {
	Date date = new Date();
	SimpleDateFormat format = new SimpleDateFormat("dd");
	String todayStr = format.format(date);
	String mStr = format.format(mDate);

	SimpleDateFormat mm = new SimpleDateFormat("MM");
	SimpleDateFormat yy = new SimpleDateFormat("yyyy");
	if (!(yy.format(date).equals(yy.format(mDate)))) {
		return false;
	} else {

		if (!(mm.format(date).equals(mm.format(mDate)))) {
			return false;
		} else {
			if (todayStr.equals(mStr)) {
				return true;
			} else {
				return false;
			}

		}
	}
}

/**
 * 获取当前时间
 * @return
 */
public static String getCurrentTime() {
	Date date = new Date();
	return paserDate(FORMATTER_INPUT2.format(date));
}

/**
 * 由手机时间判断是否是周末
 * */
public static boolean isWeekEnd() {

	int week = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
	// Log.d("Time", "week="+week);
	if (week == Calendar.SATURDAY || week == Calendar.SUNDAY)
		return true;
	else
		return false;

}
/**
 * 判断当前日期是星期几
 *
 * @param
 *
 * @return dayForWeek 判断结果
 * @Exception 发生异常
 */
public static String dayForWeek(Calendar c) {
	int dayForWeek = 0;
	if (c.get(Calendar.DAY_OF_WEEK) == 1) {
		dayForWeek = 7;
	} else {
		dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
	}
	switch (dayForWeek) {
		case 1:
			return "周一";
		case 2:
			return "周二";
		case 3:
			return "周三";
		case 4:
			return "周四";
		case 5:
			return "周五";
		case 6:
			return "周六";
		case 7:
			return "周日";
		default:
			return "";
	}
}

文章最后发布于: 2019-01-10 11:27:06

相关阅读

C# DateTime.Compare时间大小对比

转载于:https://www.cnblogs.com/lbonet/p/10215723.html

localtime时间使用

在linux环境,应用需要记录KPI的数据统计,代码中获取时间函数,localtime(),asctime查看,获取事件为东八区的时间,与当前系统时间一致,而

京东2017国庆节促销时间表介绍

2017年国庆节京东商城肯定是有活动的,那么京东2017国庆节促销时间表大家知道吗?如果大家知道这个时间表,就可以按照时间去参加促销

天猫美妆付邮试用更新时间是什么?怎么领取?

提起免费试用,很多朋友都会毫不犹豫的参加这项活动中,尤其是美妆行业下面的这个活动时最为常见,也是最为受欢迎的,今天小编就给大家分

2018京东春节放假时间,京东快递过年发货吗?

很快就要迎来2018年春节了,电商平台这个要考虑的就是春节期间发货的问题了。京东集团的董事长刘强东之前在中法企业家委员会成立大

分享到:

栏目导航

推荐阅读

热门阅读