UTC时间转本地时间、友好时间格式

This commit is contained in:
pppscn 2021-03-03 09:32:31 +08:00
parent 13a6a79644
commit c7d1858800
3 changed files with 44 additions and 18 deletions

View File

@ -27,6 +27,7 @@ import com.idormy.sms.forwarder.BroadCastReceiver.SmsForwarderBroadcastReceiver;
import com.idormy.sms.forwarder.adapter.LogAdapter; import com.idormy.sms.forwarder.adapter.LogAdapter;
import com.idormy.sms.forwarder.model.vo.LogVo; import com.idormy.sms.forwarder.model.vo.LogVo;
import com.idormy.sms.forwarder.utils.LogUtil; import com.idormy.sms.forwarder.utils.LogUtil;
import com.idormy.sms.forwarder.utils.aUtil;
import com.umeng.analytics.MobclickAgent; import com.umeng.analytics.MobclickAgent;
import java.util.ArrayList; import java.util.ArrayList;
@ -130,9 +131,9 @@ public class MainActivity extends AppCompatActivity implements ReFlashListView.I
builder.setTitle("详情"); builder.setTitle("详情");
String simInfo = logVo.getSimInfo(); String simInfo = logVo.getSimInfo();
if (simInfo != null) { if (simInfo != null) {
builder.setMessage(logVo.getFrom() + "\n\n" + logVo.getContent() + "\n\n" + logVo.getSimInfo() + "\n\n" + logVo.getRule() + "\n\n" + logVo.getTime()); builder.setMessage(logVo.getFrom() + "\n\n" + logVo.getContent() + "\n\n" + logVo.getSimInfo() + "\n\n" + logVo.getRule() + "\n\n" + aUtil.utc2Local(logVo.getTime()));
} else { } else {
builder.setMessage(logVo.getFrom() + "\n\n" + logVo.getContent() + "\n\n" + logVo.getRule() + "\n\n" + logVo.getTime()); builder.setMessage(logVo.getFrom() + "\n\n" + logVo.getContent() + "\n\n" + logVo.getRule() + "\n\n" + aUtil.utc2Local(logVo.getTime()));
} }
builder.show(); builder.show();
} }

View File

@ -4,7 +4,6 @@ import android.content.Context;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Environment; import android.os.Environment;
import android.util.Log;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -13,6 +12,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Properties; import java.util.Properties;
import java.util.TimeZone;
public class aUtil { public class aUtil {
private static String TAG = "aUtil"; private static String TAG = "aUtil";
@ -59,19 +59,19 @@ public class aUtil {
} }
//友好时间显示 //友好时间显示
public static String friendlyTime(String sdate) { public static String friendlyTime(String utcTime) {
SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));//时区定义并进行时间获取
Date time = null; Date utcDate = null;
try { try {
time = sf.parse(sdate); utcDate = utcFormater.parse(utcTime);
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
return utcTime;
} }
Log.d(TAG, time.getTime() + "");
//获取time距离当前的秒数 //获取utcDate距离当前的秒数
int ct = (int) ((System.currentTimeMillis() - time.getTime()) / 1000); int ct = (int) ((System.currentTimeMillis() - utcDate.getTime()) / 1000);
if (ct == 0) { if (ct == 0) {
return "刚刚"; return "刚刚";
@ -97,4 +97,31 @@ public class aUtil {
return ct / 31104000 + "年前"; return ct / 31104000 + "年前";
} }
/**
* 函数功能描述:UTC时间转本地时间格式
*
* @param utcTime UTC时间
* @return 本地时间格式的时间
*/
public static String utc2Local(String utcTime) {
String utcTimePatten = "yyyy-MM-dd HH:mm:ss";
String localTimePatten = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat utcFormater = new SimpleDateFormat(utcTimePatten);
utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));//时区定义并进行时间获取
Date utcDate = null;
try {
utcDate = utcFormater.parse(utcTime);
} catch (ParseException e) {
e.printStackTrace();
return utcTime;
}
SimpleDateFormat localFormater = new SimpleDateFormat(localTimePatten);
localFormater.setTimeZone(TimeZone.getDefault());
String localTime = localFormater.format(utcDate.getTime());
return localTime;
}
} }

View File

@ -8,27 +8,26 @@
android:layout_width="32dp" android:layout_width="32dp"
android:layout_height="32dp" android:layout_height="32dp"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_marginLeft="10dp" android:layout_marginLeft="5dp"
android:layout_marginTop="10dp" /> android:layout_marginTop="10dp" />
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="10dp" android:layout_marginLeft="8dp"
android:orientation="vertical"> android:orientation="vertical">
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="10dip" android:paddingTop="5dp"
android:paddingBottom="10dip"> android:paddingBottom="5dp">
<TextView <TextView
android:id="@+id/tlog_from" android:id="@+id/tlog_from"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentStart="true" android:layout_alignParentStart="true" />
android:layout_marginLeft="10dp" />
<TextView <TextView
android:id="@+id/tlog_rule" android:id="@+id/tlog_rule"
@ -56,7 +55,6 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp" /> android:layout_marginBottom="10dp" />
</LinearLayout> </LinearLayout>