-
-
Save allenatwork/71dd33aad301ba1329744a79a006fba1 to your computer and use it in GitHub Desktop.
Time Utils
This file contains hidden or 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 jp.co.neo.DCChat.common.utils; | |
import android.content.Context; | |
import android.support.annotation.StringRes; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
import java.util.LinkedHashMap; | |
import java.util.Locale; | |
import java.util.TimeZone; | |
import jp.co.neo.DCChat.R; | |
import jp.co.neo.DCChat.common.controller.App; | |
import jp.co.neo.DCChat.common.model.AppEnv; | |
public class TimeUtils { | |
public static Locale getServiceLocale() { | |
return Locale.getDefault(); | |
} | |
public static TimeZone getServiceTimeZone() { | |
return TimeZone.getDefault(); | |
} | |
public static SimpleDateFormat getServiceSimpleFormat(String pattern) { | |
return getServiceSimpleFormat(pattern, null, null); | |
} | |
public static SimpleDateFormat getServiceSimpleFormat(String pattern, Locale locale, TimeZone tz) { | |
if (locale == null) { | |
locale = getServiceLocale(); | |
} | |
if (tz == null) { | |
tz = getServiceTimeZone(); | |
} | |
SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale); | |
sdf.setTimeZone(tz); | |
return sdf; | |
} | |
/** | |
* @return 2015/05/26 | |
*/ | |
public static String getCurrentDate(String pattern) { | |
String date = null; | |
SimpleDateFormat sdf = getServiceSimpleFormat(pattern); | |
if (sdf != null) { | |
date = sdf.format(new Date()); | |
} | |
return date; | |
} | |
public static String getFormattedDate(Date date, String pattern) { | |
String result = null; | |
SimpleDateFormat sdf = getServiceSimpleFormat(pattern); | |
if (sdf != null) { | |
result = sdf.format(date); | |
} | |
return result; | |
} | |
public static long dateToLong(String date, String pattern) { | |
SimpleDateFormat sdf = getServiceSimpleFormat(pattern); | |
Date d = null; | |
try { | |
d = sdf.parse(date); | |
} catch (Exception e) { | |
LogUtils.printStackTrace(e); | |
} | |
Calendar c = Calendar.getInstance(getServiceTimeZone()); | |
c.setTime(d); | |
return c.getTimeInMillis(); | |
} | |
public static Date dateFromString(String date, String pattern) { | |
return new Date(dateToLong(date, pattern)); | |
} | |
/** | |
* @param date the date from. | |
* @param fromPattern sample yyyy-mm-dd. | |
* @param toPattern to date format. | |
* @return format date string. | |
*/ | |
public static String convertDateToDate(final String date, | |
final String fromPattern, final String toPattern) { | |
String result = date; | |
if (date == null) { | |
result = ""; | |
} else { | |
SimpleDateFormat dfFrom = getServiceSimpleFormat(fromPattern); | |
SimpleDateFormat dfTo = getServiceSimpleFormat(toPattern); | |
try { | |
result = dfTo.format(dfFrom.parse(date)); | |
} catch (ParseException e) { | |
result = date; | |
LogUtils.printStackTrace(e); | |
} | |
} | |
return result; | |
} | |
// 一覧用当年対応版 | |
public static String convertDateToDate(final String date, | |
final String fromPattern, String toPattern, boolean isYearShort) { | |
// 年調整フラグあり | |
if (isYearShort) { | |
// 当年ではない | |
if (!checkCurrentYear(date, fromPattern)) { | |
// MM/dd HH:mm | |
if ((Utils.getResourceString(R.string.date_format_3)).equalsIgnoreCase(toPattern)) { | |
toPattern = Utils.getResourceString(R.string.date_format_22); | |
} | |
// MM/dd | |
else if ((Utils.getResourceString(R.string.date_format_14)).equalsIgnoreCase(toPattern)) { | |
toPattern = Utils.getResourceString(R.string.date_format_17); | |
} | |
} | |
} | |
return convertDateToDate(date, fromPattern, toPattern); | |
} | |
// todo ↓ 以下のconvertDateToDateを可能なら整理する | |
public static String convertDateToDate(Context context, String date, @StringRes int fromPattern, @StringRes int toParttern, Locale locale) { | |
String result = ""; | |
if (date == null) { | |
result = ""; | |
} else { | |
String fromPatternString = context.getResources().getString(fromPattern); | |
String toPatternString = context.getResources().getString(toParttern); | |
final SimpleDateFormat dfFrom = new SimpleDateFormat(fromPatternString); | |
final SimpleDateFormat dfTo = new SimpleDateFormat(toPatternString, locale); | |
try { | |
result = dfTo.format(dfFrom.parse(date)); | |
} catch (ParseException e) { | |
LogUtils.printStackTrace(e); | |
result = ""; | |
} | |
} | |
return result; | |
} | |
public static String convertDateToDate(Context context, String date, @StringRes int fromPattern, @StringRes int toParttern) { | |
return convertDateToDate(context, date, fromPattern, toParttern, Locale.getDefault()); | |
} | |
// todo ↑ | |
public static boolean checkCurrentYear(String data, String formatFrom) { | |
if (data == null) { | |
return false; | |
} | |
String currentYear = TimeUtils.getCurrentDate(Constants.DATE_FORMAT_18); | |
String year = convertDateToDate(data, formatFrom, Constants.DATE_FORMAT_18); | |
return year.equalsIgnoreCase(currentYear); | |
} | |
public static String getMinDateOfMonth(String format, String type) { | |
String date = ""; | |
int dateMin = 0; | |
try { | |
SimpleDateFormat dateFormat = getServiceSimpleFormat(format); | |
Calendar calendar = Calendar.getInstance(); | |
if (type.equalsIgnoreCase(Constants.MIN)) { | |
dateMin = calendar.getActualMinimum(Calendar.DATE); | |
} else if (type.equalsIgnoreCase(Constants.MAX)) { | |
dateMin = calendar.getActualMaximum(Calendar.DATE); | |
} | |
calendar.set(Calendar.DATE, dateMin); | |
date = dateFormat.format(calendar.getTime()); | |
} catch (Exception e) { | |
LogUtils.printStackTrace(e); | |
} | |
return date; | |
} | |
/** | |
* 経過時間を取得する(n秒前、n分前、 ..., n年前) | |
* | |
* aBaseDateにnullを指定した場合は(携帯の)現在日時までの経過時間を返却する | |
* @param aDate | |
* @param aBaseDate | |
* @return | |
*/ | |
public static String getPassedTimePhrase(Date aDate, Date aBaseDate) | |
{ | |
/// 経過時間を取得する (n秒前, n分前, ..., n年前) | |
Context context = App.getInstance(); | |
LinkedHashMap<String, Integer> _unitForSec = new LinkedHashMap<>(); | |
_unitForSec.put("second", 60); | |
_unitForSec.put("minute", 60); | |
_unitForSec.put("hour", 24); | |
_unitForSec.put("day", 31); | |
LinkedHashMap<String, String> _passedTime = new LinkedHashMap<>(); | |
_passedTime.put("second", context.getString(R.string.passed_time_second)); | |
_passedTime.put("minute", context.getString(R.string.passed_time_minute)); | |
_passedTime.put("hour", context.getString(R.string.passed_time_hour)); | |
_passedTime.put("day", context.getString(R.string.passed_time_day)); | |
_passedTime.put("month", context.getString(R.string.passed_time_month)); | |
_passedTime.put("year", context.getString(R.string.passed_time_year)); | |
// 引数の省略を補完 | |
Date _date = aDate; | |
Date _base = aBaseDate != null ? aBaseDate : new Date(); | |
// 日付同時の差分(経過秒)を取得 | |
long gap = (_base.getTime() - _date.getTime()) / 1000; | |
if (gap > 0) { | |
// 秒~日(秒数基準) | |
for (String unit : _unitForSec.keySet()){ | |
if (gap < _unitForSec.get(unit)) { | |
return String.format(_passedTime.get(unit), (int)Math.floor(gap)); | |
} | |
gap /= _unitForSec.get(unit); | |
} | |
// 月~(月数基準) | |
GregorianCalendar cal = new GregorianCalendar(); | |
cal.setTime(_date); | |
int dateYear = cal.get(Calendar.YEAR); | |
int dateMonth= cal.get(Calendar.MONTH); | |
cal.setTime(_base); | |
int baseYear = cal.get(Calendar.YEAR); | |
int baseMonth= cal.get(Calendar.MONTH); | |
gap = (baseMonth + baseYear * 12) - (dateMonth + dateYear * 12); | |
if (gap < 12) { | |
return String.format(_passedTime.get("month"), (int)Math.floor(gap)); | |
} | |
return String.format(_passedTime.get("year"), (int)Math.floor(gap/12)); | |
} | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment