Created
January 1, 2020 12:18
-
-
Save mdeora/13bab9acaa64303f12052930b0e463c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.utils; | |
import android.annotation.SuppressLint; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Locale; | |
/** | |
* 有关时间的工具方法 | |
* Created by 陈健宇 at 2018/12/6 | |
*/ | |
public class TimeUtils { | |
/** | |
* 获取当前时间 | |
*/ | |
public static String getNowTime(){ | |
@SuppressLint("SimpleDateFormat") | |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
Date date = new Date(System.currentTimeMillis()); | |
return simpleDateFormat.format(date); | |
} | |
/** | |
* 返回发布时间距离当前的时间 | |
*/ | |
public static String getTimeAgo(Date createdTime) { | |
SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA); | |
if (createdTime != null) { | |
long agoTimeInMin = (new Date(System.currentTimeMillis()).getTime() - createdTime.getTime()) / 1000 / 60; | |
// 如果在当前时间以前一分钟内 | |
if (agoTimeInMin <= 1) { | |
return "刚刚"; | |
} else if (agoTimeInMin <= 60) { | |
// 如果传入的参数时间在当前时间以前10分钟之内 | |
return agoTimeInMin + "分钟前"; | |
} else if (agoTimeInMin <= 60 * 24) { | |
return agoTimeInMin / 60 + "小时前"; | |
} else if (agoTimeInMin <= 60 * 24 * 2) { | |
return agoTimeInMin / (60 * 24) + "天前"; | |
} else { | |
return format.format(createdTime); | |
} | |
} else { | |
return format.format(new Date(0)); | |
} | |
} | |
/** | |
* 根据时间戳 返回发布时间距离当前的时间 | |
*/ | |
public static String getTimeStampAgo(String timeStamp) { | |
Long time = Long.valueOf(timeStamp); | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); | |
String string = sdf.format(time * 1000L); | |
Date date = null; | |
try { | |
date = sdf.parse(string); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return getTimeAgo(date); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment