Android 時間轉換工具類(初識版本)
我就是一個早上偶爾遲到,然后就挨餓的程序員。在項目中總是會有時間的處理,我根據我們公司的項目來寫的一個時間處理類。如果你們有用到的地方可以參考,并提出自己的想法,我們相互學習。
注意:有時候的我命名不規(guī)范,在看源碼的時候請小聲吐槽。? ??
public class TimeUtil {
方法1:獲取傳入的時間是不是今天(手機時間必須是正確的)
/**
?* @param day 傳入的 時間? "2016-06-28 10:10:30"
? ? *@param type? 你的時間格式
?????* @return true今天 false不是
? ? * @throws ParseException
*/
public boolean IsToday(String day, String type) {
Calendar pre = Calendar.getInstance();
Date predate =new Date(System.currentTimeMillis());
? ? ? ? pre.setTime(predate);
? ? ? ? Calendar cal = Calendar.getInstance();
? ? ? ? Date date =null;
? ? ? ? if (type ==null) {
type ="yyyy-MM-dd HH:mm:ss";
? ? ? ? }
try {
date = getSimpleDateFormat(type).parse(day);
? ? ? ? }catch (ParseException e) {
e.printStackTrace();
? ? ? ? }
cal.setTime(date);
? ? ? ? if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
int diffDay = cal.get(Calendar.DAY_OF_YEAR)
- pre.get(Calendar.DAY_OF_YEAR);
? ? ? ? ? ? return diffDay <=0;
? ? ? ? }
return false;
? ? }
方法1:(重載1)
/**
? ? * @param day? 傳入正確的時間格式? yyyy-MM-dd HH:mm:ss
? ? * @return
? ? */
? ? public boolean IsToday(String day){
????return? IsToday(day,null);
? ? }
方法1:(重載2)
/**
*
? ? * @param time? ? 傳入毫秒
? ? * @return
? ? */
? ? public boolean IsToday(long time){
return? IsToday(getTime(time),null);
? ? }
方法2:獲取傳入的時間是今天? 明天? 或者 周幾
/***
? ? * @param time? 時間
? ? * @return? 返回值 今天? 明天 或者? 周幾
*/
? ? public String checkOption(String time) {
????????SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
? ? ? ? Date predate =new Date(System.currentTimeMillis());
? ? ? ? Calendar cl = Calendar.getInstance();
? ? ? ? String strTime = getTime(time, "yyyy-MM-dd");
? ? ? ? String str ="";
? ? ? ? if (sdf.format(cl.getTime()).equals(strTime)) {
str ="今天";
? ? ? ? }else {
cl.setTime(predate);
? ? ? ? ? ? cl.add(Calendar.DAY_OF_YEAR, 1);
? ? ? ? ? ? if (sdf.format(cl.getTime()).equals(strTime)) {
str ="明天";
? ? ? ? ? ? }else {
str = getWeek(time);
? ? ? ? ? ? }
}
return str;
? ? }
方法2:(重載1)
public String checkOption(long time){
return checkOption(getTime(time));
? ? }
獲取SimpleDateFormat
@NonNull
? ? private SimpleDateFormat getSimpleDateFormat(String s) {
return new SimpleDateFormat(s);
? ? }
方法2:(重載2)
public String getTime(String time, String mFormat) {
return getTime(time, mFormat, null, 0);
? ? }
方法3:獲取你需要的時間格式 比如:2018年10月10日 12點12分12秒? ?2018-10-10 12:12:12 等等。
/**
? ? * @param time? ? 你傳入的時間? 格式要求? ?"yyyy-MM-dd HH:mm:ss"? 或者?mYTypeTime 給出你的時間格式。
? ? * @param mFormat 返回的時間格式
? ? * @param? mYTypeTime? ? ? 你自己的時間格式 和Time參數相對應
? ? *@param? mTime? ? 機器時間? 毫秒為單位? ? System.currentTimeMillis()? ?我這里測試用的機器時間。
? ? * @return
? ? */
? ? public String getTime(String time, String mFormat, String mYTypeTime, long mTime) {
int strTime =0;
? ? ? ? long currentTime;
? ? ? ? if (!TextUtils.isEmpty(time)&&mTime == strTime) {
currentTime = strTime;
? ? ? ? ? ? Date date;
? ? ? ? ? ? try {
if (TextUtils.isEmpty(mYTypeTime)) {
date = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
? ? ? ? ? ? ? ? }else {
date = getSimpleDateFormat(mYTypeTime).parse(time);
? ? ? ? ? ? ? ? }
currentTime = date.getTime();
? ? ? ? ? ? }catch (ParseException e) {
e.printStackTrace();
? ? ? ? ? ? }
}else {
currentTime = mTime;
? ? ? ? }
if (mFormat==null){
mFormat="yyyy-MM-dd HH:mm:ss";
? ? ? ? }
if (mTime==0&&TextUtils.isEmpty(time)){
currentTime=System.currentTimeMillis();
? ? ? ? }
return? getSimpleDateFormat(mFormat).format(currentTime);
? ? }
方法3:(重載1)
/**
*
? ? * @param time
? ? * @param mYTypeTime
? ? * @param mFormat
? ? * @return
? ? */
? ? public String getTime(String time,String mYTypeTime, String mFormat) {
return getTime(time, mFormat, mYTypeTime, 0);
? ? }
方法3:(重載2)
/**
*
? ? * @param mTime? ? ? 機器時間? 毫秒為單位? ? System.currentTimeMillis()
? ? * @param mFormat? ? 你需要轉換的時間格式。? 默認值? "yyyy-MM-dd HH:mm:ss"
? ? * @return? ? 你需要的時間格式
*/
? ? public String getTime(long mTime, String mFormat) {
return getTime(null, mFormat, null, mTime);
? ? }
方法3:(重載3)
/**
*
? ? * @param mTime? ? 機器時間? 毫秒為單位? ? System.currentTimeMillis()
? ? * @return? yyyy-MM-dd HH:mm:ss
*/
? ? public String getTime(long mTime) {
return getTime(null, null, null, mTime);
? ? }
方法3:(重載4)
/**
*? 默認獲取當前時間
? ? * @return? yyyy-MM-dd HH:mm:ss
*/
? ? public String getTime( ) {
return getTime(null, null, null, 0);
? ? }
方法3:(重載5)
/**
????*默認獲取當前時間
? ? * @param mFormat? ? 時間格式 你希望返回的時間格式
? ? * @return
? ? */
? ? public String getTime(String mFormat){
return getTime(null, mFormat, null, 0);
? ? }
方法4:傳入的時間和當前時間做對比。
/**
* 時間戳? ? 格式? yyyy-MM-dd HH:mm:ss
?* @param time
?* @return? ? ? 返回結果時間差
*/
? ? public String getTimeDifference(String time) {
????????String str ="";
? ? ? ? Calendar c = Calendar.getInstance();
? ? ? ? long mowTime = System.currentTimeMillis();
? ? ? ? long pastTimes =0;
? ? ? ? try {
c.setTime(getSimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));
? ? ? ? ? ? pastTimes = c.getTimeInMillis();
? ? ? ? }catch (ParseException e) {
e.printStackTrace();
? ? ? ? ? ? return str = time.substring(5, time.length() -3);
? ? ? ? }
long shijiancha = mowTime - pastTimes;
? ? ? ? long day = shijiancha / (3600000 *24);
? ? ? ? if (day <1) {
long mtime = shijiancha / (3600000);
? ? ? ? ? ? str = mtime +"小時前";
? ? ? ? ? ? if (mtime <1) {
long branch = shijiancha %3600000 /60000;
? ? ? ? ? ? ? ? str = branch +"分鐘前";
? ? ? ? ? ? ? ? if (branch <1) {
long second = shijiancha %60000 /1000;
? ? ? ? ? ? ? ? ? ? str ="剛剛";
? ? ? ? ? ? ? ? }
}
}else {
str = day +"天前";
//? ? ? ? ? ? }
? ? ? ? }
return str;
? ? }
方法4:(重載1)
/**
? ?* @param time 機器時間? 毫秒為單位? ? System.currentTimeMillis()
? ?* @return
? ?*/
? ? public String getTimeDifference(long time){
return getTimeDifference(getTime(time));
? ? }
方法5:?判斷當前日期是星期幾
/**
* 判斷當前日期是星期幾
* @param pTime 設置的需要判斷的時間? //格式如yyyy-MM-dd HH:mm:ss
* @return dayForWeek 判斷結果
* @Exception 發(fā)生異常
*/
? ? public String getWeek(String pTime) {
StringWeek="周";
? ? ? ? SimpleDateFormat format = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
? ? ? ? Calendar c = Calendar.getInstance();
? ? ? ? try {
c.setTime(format.parse(pTime));
? ? ? ? }catch (ParseException e) {
// TODO Auto-generated catch block
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
if (c.get(Calendar.DAY_OF_WEEK) ==1) {
Week +="日";
? ? ? ? }
if (c.get(Calendar.DAY_OF_WEEK) ==2) {
Week +="一";
? ? ? ? }
if (c.get(Calendar.DAY_OF_WEEK) ==3) {
Week +="二";
? ? ? ? }
if (c.get(Calendar.DAY_OF_WEEK) ==4) {
Week +="三";
? ? ? ? }
if (c.get(Calendar.DAY_OF_WEEK) ==5) {
Week +="四";
? ? ? ? }
if (c.get(Calendar.DAY_OF_WEEK) ==6) {
Week +="五";
? ? ? ? }
if (c.get(Calendar.DAY_OF_WEEK) ==7) {
Week +="六";
? ? ? ? }
return Week;
? ? }
public String getWeek(long time){
return getWeek(getTime(time));
? ? }
}
最后放一張測試圖:希望對你所有幫助,也希望提出你們的想法和意見。

//毫秒轉換 默認 "yyyy-MM-dd HH:mm:ss"
TimeChange.getInstanceSafe().getTimeStr(System.currentTimeMillis());
//毫秒轉換 你需要的 比如 :"MM-dd"
TimeChange.getInstanceSafe().getTimeStr(System.currentTimeMillis(), "MM-dd");
//字符串轉換 你需要的 比如 :"MM-dd" TimeChange.getInstanceSafe().getTimeStr("2020-12-12 00:00:00", "MM-dd");
//字符串轉換 后臺給的格式 如2020/12/12 00:00:00 、 2020年12月12日 00時00分00秒TimeChange.getInstanceSafe().getTimeStr("2020/12/12 00:00:00", "yyyy/MM/dd HH:mm:ss", "MM-dd");
TimeChange.getInstanceSafe().getTimeStr("2020-12-12 00:00:00", "yyyy年MM月dd日 HH時mm分ss秒", "MM-dd");
//剛剛 10秒前 1小時 等等
TimeChange.getInstanceSafe().getTimeLossStr(TimeChange.getInstanceSafe().getTimeStr(System.currentTimeMillis() - 10), null);
//轉毫秒
TimeChange.getInstanceSafe().getTimemIllisecond("2020/12/12 00:00:00");