1. 程式人生 > >Android時間工具類 本地轉UTC,UTC轉本地

Android時間工具類 本地轉UTC,UTC轉本地

  package com.peopleapp.en.util;

import android.content.Context;
import android.text.TextUtils;
import android.text.format.DateFormat;

import com.peopleapp.en.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class TimeUtil { private static final int seconds_of_1minute = 60; private static final int seconds_of_1hour = 60 * 60; private static final int seconds_of_2hour = 2 * 60 * 60; private static final int seconds_of_3hour = 3 * 60 * 60; private static final String YMDHMS_FORMAT =
"yyyy-MM-dd HH:mm:ss"; private static final String search_DateFormat = "MM/dd/yyyy HH:mm:ss"; private static final String TIME_ZERO = "00:00"; private static final String TIME_MAX = "23:59:59"; public static Date stringConvertDate(String time) { SimpleDateFormat sdf = new SimpleDateFormat
("yyyy-MM-dd HH:mm", Locale.CHINA); Date data = null; try { data = sdf.parse(time); } catch (ParseException e) { e.printStackTrace(); } return data; } public static String timeAgo(Context context, long createdTime) { return timeAgo(context, new Date(createdTime)); } public static String timeAgo(Context context, 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 context.getString(R.string.just_now); } else if (agoTimeInMin <= 60) { return agoTimeInMin + context.getString(R.string.mins_ago); } else if (agoTimeInMin <= 60 * 24) { return agoTimeInMin / 60 + context.getString(R.string.hours_ago); } else if (agoTimeInMin <= 60 * 24 * 2) { return agoTimeInMin / (60 * 24) + context.getString(R.string.days_ago); } else { return format.format(createdTime); } } else { return format.format(new Date(0)); } } public static String getDateTimeAgo(Context context, long timeStamp) { return timeAgo(context, new Date(timeStamp)); } public static String getUSDateTimeFormat(long timeStamp) { SimpleDateFormat usSdf = new SimpleDateFormat("HH:mm, MMMM dd, yyyy", Locale.US); return usSdf.format(new Date(timeStamp)); } public static String getCurrentTimeStamp() { return String.valueOf(System.currentTimeMillis() / 1000); } /** * local ---> UTC * * @return */ public static String Local2UTC() { SimpleDateFormat sdf = new SimpleDateFormat(YMDHMS_FORMAT); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String gmtTime = sdf.format(new Date()); return gmtTime; } /** * UTC --->local * * @param utcTime UTC * @return */ public static String utc2Local(String utcTime) { try { if (TextUtils.isEmpty(utcTime)) { return ""; } SimpleDateFormat utcFormater = new SimpleDateFormat(YMDHMS_FORMAT); utcFormater.setTimeZone(TimeZone.getTimeZone("UTC")); Date gpsUTCDate = null; try { gpsUTCDate = utcFormater.parse(utcTime); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat localFormater = new SimpleDateFormat(YMDHMS_FORMAT); localFormater.setTimeZone(TimeZone.getDefault()); String localTime = localFormater.format(gpsUTCDate.getTime()); return localTime; } catch (Exception e) { e.printStackTrace(); } return ""; } /** * @param tTime * @return */ public static String getTimeRange(Context context, String tTime) { String mTime = ""; try { mTime = utc2Local(tTime); if (TextUtils.isEmpty(mTime)) { return ""; } SimpleDateFormat sdf = new SimpleDateFormat(YMDHMS_FORMAT); sdf.setTimeZone(TimeZone.getDefault()); Date curDate = new Date(System.currentTimeMillis()); String dataStrNew = sdf.format(curDate); Date startTime = null; try { curDate = sdf.parse(dataStrNew); startTime = sdf.parse(mTime); } catch (ParseException e) { e.printStackTrace(); } long between = (curDate.getTime() - startTime.getTime()) / 1000; int elapsedTime = (int) (between); if (elapsedTime < 0) { return context.getResources().getString(R.string.timeutils_default_oneminageo); } if (elapsedTime < seconds_of_1minute) { return context.getResources().getString(R.string.timeutils_default_oneminageo); } if (elapsedTime < seconds_of_1hour) { return elapsedTime / seconds_of_1minute + " " + context.getResources().getString(R.string.timeutils_default_moreminsageo); } if (elapsedTime < seconds_of_2hour) { return context.getResources().getString(R.string.timeutils_default_onehourageo); } if (elapsedTime < seconds_of_3hour) { return elapsedTime / seconds_of_1hour + " " + context.getResources().getString(R.string.timeutils_default_morehoursageo); } return ""; } catch (Exception e) { e.printStackTrace(); } return context.getResources().getString(R.string.timeutils_default_threehoursageo); } public static String getTimeRange(Context context, long time) { long between = (System.currentTimeMillis() - time) / 1000; int elapsedTime = (int) (between); if (elapsedTime < 0) { return context.getResources().getString(R.string.timeutils_default_oneminageo); } if (elapsedTime < seconds_of_1minute) { return context.getResources().getString(R.string.timeutils_default_oneminageo); } if (elapsedTime < seconds_of_1hour) { return elapsedTime / seconds_of_1minute + " " + context.getResources().getString(R.string.timeutils_default_moreminsageo); } if (elapsedTime < seconds_of_2hour) { return context.getResources().getString(R.string.timeutils_default_onehourageo); } if (elapsedTime < seconds_of_3hour) { return elapsedTime / seconds_of_1hour + " " + context.getResources().getString(R.string.timeutils_default_morehoursageo); } return ""; } /** * 時間戳轉換成日期格式字串 * * @return */ public static String timeStamp2Date(long seconds, String format) { if (format == null || format.isEmpty()) { format = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new Date(seconds)); } public static String longToString(long longNum, String dateFormat) { if (TextUtils.isEmpty(dateFormat)) { dateFormat = YMDHMS_FORMAT; } SimpleDateFormat format = new SimpleDateFormat(dateFormat); Date date = new Date(longNum); return format.format(date); } public static String secondsToTime(int time) { String timeStr = null; int hour = 0; int minute = 0; int second = 0; if (time <= 0) return TIME_ZERO; else { minute = time / 60; if (minute < 60) { second = time % 60; timeStr = unitFormat(minute) + ":" + unitFormat(second); } else { hour = minute / 60; if (hour > 23) return TIME_MAX; minute = minute % 60; second = time - hour * 3600 - minute * 60; timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second); } } return timeStr; } public static String unitFormat(int i) { String retStr = null; try { if (i >= 0 && i < 10) retStr = "0" + Integer.toString(i); else retStr = "" + i; } catch (Exception e) { e.printStackTrace(); } return retStr; } public static long searchTimeToLong(String time) { if (TextUtils.isEmpty(time)) { return 0L; } try { String[] split = time.split(" "); String tempTime = split[0] + " " + split[1]; int diff = 0; if ("pm".equals(split[2])) { diff = 1000 * 12 * 60 * 60; } SimpleDateFormat sdf = new SimpleDateFormat(search_DateFormat); sdf.setTimeZone(TimeZone.getDefault()); Date startTime = null; startTime = sdf.parse(tempTime); return (startTime.getTime() + diff); } catch (ParseException e) { e.printStackTrace(); } return 0L; } public static String searchTimeFormat(String time) { if (TextUtils.isEmpty(time)) { return ""; } try { String date = (String) DateFormat.format("yyyy-MM-dd HH:mm:ss", searchTimeToLong(time)); return date; } catch (Exception e) { e.printStackTrace(); } return ""; } }