Files
android-fuplanner/app/src/main/java/de/sebse/fuplanner/tools/DateUtils.java
2018-07-23 00:32:05 +02:00

71 lines
2.2 KiB
Java

package de.sebse.fuplanner.tools;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.format.DateFormat;
import com.google.android.gms.common.logging.Logger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateUtils {
private static Logger log = new Logger("DateUtils");
@Deprecated
public static String getModifiedDateTime(long modified) {
return getModifiedDateTime(null, modified);
}
public static String getModifiedDateTime(Context context, long modified) {
return getModifiedDate(context, modified, "dd.MM.yy hh:mm");
}
@Deprecated
public static String getModifiedTime(long modified) {
return getModifiedTime(null, modified);
}
public static String getModifiedTime(Context context, long modified) {
return getModifiedDate(context, modified, "hh:mm");
}
public static String getModifiedDate(long modified) {
return getModifiedDate(null, modified, "dd.MM.yy");
}
public static String getModifiedDate(@Nullable Context context, long modified, String skeleton) {
return getModifiedDate(context, Locale.getDefault(), modified, skeleton);
}
@SuppressLint("SimpleDateFormat")
private static String getModifiedDate(@Nullable Context context, Locale locale, long modified, String skeleton) {
SimpleDateFormat dateFormat;
if (context != null && DateFormat.is24HourFormat(context))
skeleton = skeleton.replaceAll("h", "H");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
dateFormat = new SimpleDateFormat(getDateFormat(locale, skeleton));
} else {
dateFormat = new SimpleDateFormat(skeleton, locale);
}
return dateFormat.format(new Date(modified));
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static String getDateFormat(Locale locale, String skeleton) {
return DateFormat.getBestDateTimePattern(locale, skeleton);
}
public static boolean dateEquals(long a, long b) {
return a / 86400000 == b / 86400000;
}
}