Skip to content

Instantly share code, notes, and snippets.

@CMingTseng
Created October 7, 2016 20:50
Show Gist options
  • Save CMingTseng/c1bcc81234c918e32a534d174f9e8919 to your computer and use it in GitHub Desktop.
Save CMingTseng/c1bcc81234c918e32a534d174f9e8919 to your computer and use it in GitHub Desktop.
public class TimeMethod {
private static final String TAG = "TimeMethod";
public static final SimpleDateFormat sFull = new SimpleDateFormat("yyyy MMMdd hh:mm:ss a", StringToLocale(Locale.getDefault().getLanguage()));
public static final SimpleDateFormat sMedium = new SimpleDateFormat("MMMdd hh:mm, a", StringToLocale(Locale.getDefault().getLanguage()));
public static final SimpleDateFormat sSimple = new SimpleDateFormat("hh:mm, a", StringToLocale(Locale.getDefault().getLanguage()));
public static String ShowLogTime(long logtime) {
long now = System.currentTimeMillis();
final Calendar calendar_now = Calendar.getInstance();
final Calendar calendar_log = Calendar.getInstance();
calendar_now.setTimeInMillis(now);
calendar_log.setTimeInMillis(logtime);
if (calendar_now.get(Calendar.YEAR) != calendar_log.get(Calendar.YEAR)) {
return sFull.format(logtime);
} else if (calendar_now.get(Calendar.MONTH) != calendar_log.get(Calendar.MONTH)) {
return sMedium.format(logtime);
} else {
return sSimple.format(logtime);
}
}
public static Locale StringToLocale(String inputString) {
StringTokenizer tempStringTokenizer = new StringTokenizer(inputString, ",");
String lang = "en", country = "US";
if (tempStringTokenizer.hasMoreTokens()) {
lang = (String) tempStringTokenizer.nextElement();
}
if (tempStringTokenizer.hasMoreTokens()) {
country = (String) tempStringTokenizer.nextElement();
}
return new Locale(lang, country);
}
public static String base64Encode(String string) {
return Base64.encodeToString(string.getBytes(), Base64.NO_WRAP);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment