public class FileUtis {
public static final File externalStorageDirectory = Environment.getExternalStorageDirectory();
public static String packageFilesDirectory = null;
public static String storagePath = null;
private static String mDefaultFolder = "libCGE";
public FileUtis() {
}
public static void setDefaultFolder(String defaultFolder) {
mDefaultFolder = defaultFolder;
}
public static String getPath() {
return getPath((Context)null);
}
public static String getPath(Context context) {
if (storagePath == null) {
storagePath = externalStorageDirectory.getAbsolutePath() + "/" + mDefaultFolder;
File file = new File(storagePath);
if (!file.exists() && !file.mkdirs()) {
storagePath = getPathInPackage(context, true);
}
}
return storagePath;
}
public static String getPathInPackage(Context context, boolean grantPermissions) {
if (context != null && packageFilesDirectory == null) {
String path = context.getFilesDir() + "/" + mDefaultFolder;
File file = new File(path);
if (!file.exists()) {
if (!file.mkdirs()) {
Log.e("libCGE_java", "在pakage目录创建CGE临时目录失败!");
return null;
}
if (grantPermissions) {
if (file.setExecutable(true, false)) {
Log.i("libCGE_java", "Package folder is executable");
}
if (file.setReadable(true, false)) {
Log.i("libCGE_java", "Package folder is readable");
}
if (file.setWritable(true, false)) {
Log.i("libCGE_java", "Package folder is writable");
}
}
}
packageFilesDirectory = path;
return packageFilesDirectory;
} else {
return packageFilesDirectory;
}
}
//将内容保存在某个文件里面
public static void saveTextContent(String text, String filename) {
Log.i("libCGE_java", "Saving text : " + filename);
try {
FileOutputStream fileout = new FileOutputStream(filename);
fileout.write(text.getBytes());
fileout.flush();
fileout.close();
} catch (Exception var3) {
Log.e("libCGE_java", "Error: " + var3.getMessage());
}
}
//获得某个问价里面的内容
public static String getTextContent(String filename) {
Log.i("libCGE_java", "Reading text : " + filename);
if (filename == null) {
return null;
} else {
String content = "";
byte[] buffer = new byte[256];
try {
FileInputStream filein = new FileInputStream(filename);
while(true) {
int len = filein.read(buffer);
if (len <= 0) {
return content;
}
content = content + new String(buffer, 0, len);
}
} catch (Exception var5) {
Log.e("libCGE_java", "Error: " + var5.getMessage());
return null;
}
}
}
//将Assets目录下的文件拷贝到指定目录
public static boolean copyAssetsFile(AssetManager assetManager, String src, String dst) {
if ((new File(dst)).exists()) {
return false;
} else {
try {
FileOutputStream outputStream = new FileOutputStream(dst);
InputStream inputStream = assetManager.open(src);
byte[] buffer = new byte[1024];
int count;
while((count = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, count);
}
inputStream.close();
outputStream.close();
return true;
} catch (FileNotFoundException var7) {
var7.printStackTrace();
Log.d("camera", "copy assets file failed:" + var7.getMessage());
return false;
} catch (IOException var8) {
Log.d("camera", "copy assets file failed:" + var8.getMessage());
var8.printStackTrace();
return false;
}
}
}
//会在该应用下创建某个目录 /storage/emulated/0/Android/data/com.example.myapplication(应用)/file
public static String rootPath(Context context) {
File file = context.getExternalFilesDir((String)null);
if (file == null) {
file = context.getFilesDir(); 通过修改这个参数可以改变创建目录的名称
}
return file.getAbsolutePath();
}
//把数据把存到指定目录
public static void saveDataToFile(String path, byte[] data) {
try {
try {
File file = new File(path);
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
if (file.exists()) {
file.delete();
}
if (!file.exists()) {
boolean isok = file.createNewFile();
if (isok) {
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(data);
if (bos != null) {
bos.flush();
bos.close();
}
}
}
} catch (Exception var10) {
var10.printStackTrace();
}
} finally {
;
}
}
//读取某个文件里面的内容
public static byte[] readDataFromFile(String path) {
File file = new File(path);
BufferedInputStream bis = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
byte[] bytes = new byte[(int)file.length()];
bis.read(bytes);
if (bis != null) {
bis.close();
}
return bytes;
} catch (Exception var5) {
var5.printStackTrace();
return null;
}
}
//删除某个目录
public static void deleteDir(String pPath) {
File dir = new File(pPath);
if (dir.exists() && dir.isFile()) {
dir.delete();
}
deleteDirWihtFile(dir);
}
//删除某个目录的下的文件
public static void deleteDirWihtFile(File dir) {
if (dir != null && dir.exists() && dir.isDirectory()) {
File[] var1 = dir.listFiles();
int var2 = var1.length;
for(int var3 = 0; var3 < var2; ++var3) {
File file = var1[var3];
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
deleteDirWihtFile(file);
}
}
dir.delete();
}
}
}
//对时间的计算和转化
public class DateUtils {
public static final float SECOND_DECIMAL_IN_MILLIS = 1000f;
public static final int HOURS_IN_ONE_DAY = 24;
public static final long UNIX_TIME_FACTOR = 1000L;
public static final int MINUTE = 60;
public static final long ONE_DAY = 24 * 60 * 60 * 1000;
public static final long ONE_HOUR = 60 * 60 * 1000;
public static final long ONE_MIN = 60 * 1000;
public static final long ONE_WEEK = 7 * ONE_DAY;
public static final String DEFAULT_SEPARATOR = "/";
public static final String LINE_SEPARATOR = "-";
private static final SimpleDateFormat DATE_FORMAT =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
private static final SimpleDateFormat DATE_FORMAT_DISPLAY_12_EN =
new SimpleDateFormat("yyyy/MM/dd a h:mm", Locale.US);
private static final SimpleDateFormat DATE_FORMAT_DISPLAY_24_EN =
new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.US);
private static final SimpleDateFormat DATE_FORMAT_DISPLAY_NOYEAR_12_EN =
new SimpleDateFormat("MM/dd a h:mm", Locale.US);
private static final SimpleDateFormat DATE_FORMAT_DISPLAY_NOYEAR_24_EN =
new SimpleDateFormat("MM/dd HH:mm", Locale.US);
private static final SimpleDateFormat DATE_FORMAT_HM_12 = new SimpleDateFormat("h:mm");
public static final SimpleDateFormat DATE_FORMAT_HM_24 = new SimpleDateFormat("HH:mm");
private static final SimpleDateFormat DATE_FORMAT_MS = new SimpleDateFormat("mm:ss");
private static final SimpleDateFormat DATE_FORMAT_MS_SINGLE = new SimpleDateFormat("m:ss");
private static final SimpleDateFormat DATE_FORMAT_HM_24_WITH_SPACES =
new SimpleDateFormat(" HH:mm");
private static final SimpleDateFormat DATE_FORMAT_WEEK_TIME =
new SimpleDateFormat("EEEE");
private static final SimpleDateFormat DATE_FORMAT_WEEK_TIME_EN =
new SimpleDateFormat("EEEE a h:mm", Locale.US);
private static final SimpleDateFormat DATE_FORMAT_WEEK = new SimpleDateFormat("EEEE");
private static SimpleDateFormat DATE_FORMAT_YMD;
private static final SimpleDateFormat DATE_FORMAT_YMD_EN =
new SimpleDateFormat("yyyy/MM/dd", Locale.US);
private static final SimpleDateFormat DATE_FORMAT_YMD_POINT =
new SimpleDateFormat("yyyy.MM.dd", Locale.US);
private static final SimpleDateFormat DATE_FORMAT_Y =
new SimpleDateFormat("yyyy ", Locale.US);
private static SimpleDateFormat DATE_FORMAT_YM;
private static SimpleDateFormat DATE_FORMAT_MD;
private static final SimpleDateFormat DATE_FORMAT_MD_EN =
new SimpleDateFormat("MM/dd", Locale.US);
private static final SimpleDateFormat DATE_FORMAT_HMS =
new SimpleDateFormat("HH:mm:ss", Locale.US);
private static final SimpleDateFormat DATE_FORMAT_MMM =
new SimpleDateFormat("MMM", Locale.US);
private static final SimpleDateFormat SAT_DATE = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
private static final long MONTH_IN_MILLIS = android.text.format.DateUtils.DAY_IN_MILLIS * 31;
private static final String MONTH_FORMAT = "%d月";
private static NumberFormat sDecimalSecondFormat;
public static long parseTime(String date) {
synchronized (DATE_FORMAT) {
try {
return DATE_FORMAT.parse(date).getTime();
} catch (ParseException e) {
return System.currentTimeMillis() - 1000L;
}
}
}
public static String formatTime(long date) {
synchronized (DATE_FORMAT) {
return DATE_FORMAT.format(new Date(date));
}
}
private static String formatTimeDisplayNoYear12(long timestamp) {
synchronized (DATE_FORMAT_DISPLAY_NOYEAR_12_EN) {
if (Utils.isZhLanguage()) {
Date date = new Date(timestamp);
return getMDDataFormat().format(date) + " " + getAMPM(timestamp) + " "
+ DATE_FORMAT_HM_12.format(date);
} else {
return DATE_FORMAT_DISPLAY_NOYEAR_12_EN.format(new Date(timestamp));
}
}
}
private static String formatTimeDisplayNoYear24(long timestamp) {
synchronized (DATE_FORMAT_DISPLAY_NOYEAR_24_EN) {
if (Utils.isZhLanguage()) {
Date date = new Date(timestamp);
return getMDDataFormat().format(date) + DATE_FORMAT_HM_24.format(date);
} else {
return DATE_FORMAT_DISPLAY_NOYEAR_24_EN.format(new Date(timestamp));
}
}
}
private static String formatTimeDisplay12(long timestamp) {
synchronized (DATE_FORMAT_DISPLAY_12_EN) {
if (Utils.isZhLanguage()) {
Date date = new Date(timestamp);
return getYMDDataFormat().format(date) + " " + getAMPM(timestamp) + " "
+ DATE_FORMAT_HM_12.format(date);
} else {
return DATE_FORMAT_DISPLAY_12_EN.format(new Date(timestamp));
}
}
}
public static String formatTimeDisplay24(long timestamp) {
synchronized (DATE_FORMAT_DISPLAY_24_EN) {
if (Utils.isZhLanguage()) {
Date date = new Date(timestamp);
return getYMDDataFormat().format(date) + " " + DATE_FORMAT_HM_24.format(date);
} else {
return DATE_FORMAT_DISPLAY_24_EN.format(new Date(timestamp));
}
}
}
public static String formatTimeDisplay24En(long timestamp, @Nullable String separator) {
synchronized (DATE_FORMAT_DISPLAY_24_EN) {
String time = DATE_FORMAT_DISPLAY_24_EN.format(new Date(timestamp));
if (separator != null) {
time = time.replaceAll(DEFAULT_SEPARATOR, separator);
}
return time;
}
}
public static String formatTimeToday12(long timestamp) {
synchronized (DATE_FORMAT_HM_12) {
return DATE_FORMAT_HM_12.format(new Date(timestamp));
}
}
public static String formatTimeToday24(long timestamp) {
synchronized (DATE_FORMAT_HM_24) {
return DATE_FORMAT_HM_24.format(new Date(timestamp));
}
}
private static String formatTimeWeek(long timestamp) {
synchronized (DATE_FORMAT_WEEK_TIME_EN) {
if (Utils.isZhLanguage()) {
Date date = new Date(timestamp);
return DATE_FORMAT_WEEK_TIME.format(date) + " " + getAMPM(timestamp) + " "
+ DATE_FORMAT_HM_12.format(date);
} else {
return DATE_FORMAT_WEEK_TIME_EN.format(new Date(timestamp));
}
}
}
public static String formatTimeYear(long timestamp) {
synchronized (DATE_FORMAT_Y) {
return DATE_FORMAT_Y.format(new Date(timestamp));
}
}
public static String formatTimeYearMonthDay(long timestamp, @Nullable String separator) {
synchronized (DATE_FORMAT_YMD_EN) {
String time = DATE_FORMAT_YMD_EN.format(new Date(timestamp));
if (separator != null) {
time = time.replaceAll(DEFAULT_SEPARATOR, separator);
}
return time;
}
}
public static String formatTimeMonthDay(long timestamp) {
synchronized (DATE_FORMAT_MD_EN) {
if (Utils.isZhLanguage()) {
return getMDDataFormat().format(new Date(timestamp));
} else {
return DATE_FORMAT_MD_EN.format(new Date(timestamp));
}
}
}
public static String formatTimeMonthDayEn(long timestamp, @Nullable String separator) {
synchronized (DATE_FORMAT_MD_EN) {
String time = DATE_FORMAT_MD_EN.format(new Date(timestamp));
if (separator != null) {
time = time.replaceAll(DEFAULT_SEPARATOR, separator);
}
return time;
}
}
private static SimpleDateFormat getYMDDataFormat() {
synchronized (DATE_FORMAT_YMD_EN) {
if (null == DATE_FORMAT_YMD) {
try {
DATE_FORMAT_YMD = new SimpleDateFormat("yyyy"
+ AppEnv.get().getAppContext().getResources().getString(R.string.time_year) + "MM"
+ AppEnv.get().getAppContext().getResources().getString(R.string.time_month)
+ "dd"
+ AppEnv.get().getAppContext().getResources().getString(R.string.time_day));
} catch (Exception e) {
DATE_FORMAT_YMD = DATE_FORMAT_YMD_EN;
}
}
}
return DATE_FORMAT_YMD;
}
private static SimpleDateFormat getMDDataFormat() {
synchronized (DATE_FORMAT_MD_EN) {
if (null == DATE_FORMAT_MD) {
try {
DATE_FORMAT_MD = new SimpleDateFormat("MM"
+ AppEnv.get().getAppContext().getResources().getString(R.string.time_month) + "dd"
+ AppEnv.get().getAppContext().getResources().getString(R.string.time_day));
} catch (Exception e) {
DATE_FORMAT_MD = DATE_FORMAT_MD_EN;
}
}
}
return DATE_FORMAT_MD;
}
public static String getTimeDuration(Context context, long timeDuration) {
String durationText;
Resources r = context.getResources();
if (timeDuration < 60000L) {
int value = (int) (timeDuration / android.text.format.DateUtils.SECOND_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_second : R.string.num_seconds, value);
} else if (timeDuration < android.text.format.DateUtils.HOUR_IN_MILLIS) {
int value = (int) (timeDuration / android.text.format.DateUtils.MINUTE_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_minute : R.string.num_minutes, value);
} else if (timeDuration < android.text.format.DateUtils.DAY_IN_MILLIS) {
int value = (int) (timeDuration / android.text.format.DateUtils.HOUR_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_hour : R.string.num_hours, value);
} else if (timeDuration < MONTH_IN_MILLIS) {
int value = (int) (timeDuration / android.text.format.DateUtils.DAY_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_day : R.string.num_days, value);
} else if (timeDuration < android.text.format.DateUtils.YEAR_IN_MILLIS) {
int value = (int) (timeDuration / MONTH_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_month : R.string.num_months, value);
} else {
int value = (int) (timeDuration / android.text.format.DateUtils.YEAR_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_year : R.string.num_years, value);
}
return durationText;
}
/**
* 1分钟以内:刚刚
* 1分钟 - 59分钟:MM分钟前
* 1小时 - 24小时:HH小时前
* 1天-2天:昨天 HH:MM
* 2天 - 30天:MM月DD日 HH:MM
* 30天 - 12个月:MM月DD日 HH:MM
* 1年以上: YYYY年MM月DD日 HH:MM
*
* @param context
* @param timestamp
* @return
*/
public static String getTimeDurationInMoment(Context context, long timestamp) {
String durationText;
Resources r = context.getResources();
long now = System.currentTimeMillis();
long duration = Math.abs(now - timestamp);
if (duration < 60000L) {
durationText = r.getString(R.string.just_now);
} else if (duration < android.text.format.DateUtils.HOUR_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.MINUTE_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_minute_with_suffix : R.string.num_minutes_with_suffix, value);
} else if (duration < android.text.format.DateUtils.DAY_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.HOUR_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_hour_with_suffix : R.string.num_hour_with_suffix, value);
} else {
long today = now - (now + Calendar.getInstance().getTimeZone().getRawOffset()) % ONE_DAY;
if (isInSameYear(now, timestamp)) {
long yestoday = today - ONE_DAY;
if (timestamp > yestoday) {
return r.getString(R.string.yestoday) + " " + formatTimeToday24(timestamp);
} else {
return formatTimeDisplayNoYear24(timestamp);
}
} else {
return formatTimeDisplay24(timestamp);
}
}
return durationText;
}
/**
* 1分钟以内:刚刚
* 1分钟 - 59分钟:MM分钟前
* 1小时 - 24小时:HH小时前
* 其余:HH:MM
*
* @param context
* @param timestamp
* @return
*/
public static String getTimeDurationInMomentForHM(Context context, long timestamp) {
String durationText;
Resources r = context.getResources();
long now = System.currentTimeMillis();
long duration = Math.abs(now - timestamp);
if (duration < 60000L) {
durationText = r.getString(R.string.just_now);
} else if (duration < android.text.format.DateUtils.HOUR_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.MINUTE_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_minute_with_suffix : R.string.num_minutes_with_suffix, value);
} else if (duration < android.text.format.DateUtils.DAY_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.HOUR_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_hour_with_suffix : R.string.num_hour_with_suffix, value);
} else {
return formatTimeToday24(timestamp);
}
return durationText;
}
/**
* 1分钟以内:刚刚
* 1分钟 - 59分钟:MM分钟前
* 1小时 - 24小时:HH小时前
* 其余:x月x日
*
* @param context
* @param timestamp
* @return
*/
public static String getTimeDurationForStory(Context context, long timestamp) {
long now = System.currentTimeMillis();
long duration = Math.abs(now - timestamp);
if (duration < android.text.format.DateUtils.DAY_IN_MILLIS) {
return getTimeDurationInMomentForHM(context, timestamp);
} else {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
Resources r = context.getResources();
return r.getString(R.string.moment_month_day, String.valueOf(month), String.valueOf(day));
}
}
public static String getPastTimeDuration(Context context, long timestamp) {
String durationText;
Resources r = context.getResources();
long now = System.currentTimeMillis();
long duration = Math.abs(now - timestamp);
if (now - timestamp < 60000L) {
durationText = r.getString(R.string.just_now);
} else if (duration < android.text.format.DateUtils.HOUR_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.MINUTE_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_minute : R.string.num_minutes, value);
} else if (duration < android.text.format.DateUtils.DAY_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.HOUR_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_hour : R.string.num_hours, value);
} else if (duration < MONTH_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.DAY_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_day : R.string.num_days, value);
} else if (duration < android.text.format.DateUtils.YEAR_IN_MILLIS) {
int value = (int) (duration / MONTH_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_month : R.string.num_months, value);
} else {
int value = (int) (duration / android.text.format.DateUtils.YEAR_IN_MILLIS);
durationText = r.getString(value == 1 ? R.string.num_year : R.string.num_years, value);
}
return durationText;
}
public static String getPastTimeDurationWithSuffix(Context context, long timestamp) {
if (timestamp < 0) {
return "";
}
String durationText;
Resources r = context.getResources();
long now = System.currentTimeMillis();
long duration = Math.abs(now - timestamp);
if (now - timestamp < 60000L) {
return r.getString(R.string.just_now);
} else if (duration < android.text.format.DateUtils.HOUR_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.MINUTE_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_minute_with_suffix : R.string.num_minutes_with_suffix, value);
} else if (duration < android.text.format.DateUtils.DAY_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.HOUR_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_hour_with_suffix : R.string.num_hours_with_suffix, value);
} else if (duration < MONTH_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.DAY_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_day_with_suffix : R.string.num_days_with_suffix, value);
} else if (duration < android.text.format.DateUtils.YEAR_IN_MILLIS) {
int value = (int) (duration / MONTH_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_month_with_suffix : R.string.num_months_with_suffix, value);
} else {
int value = (int) (duration / android.text.format.DateUtils.YEAR_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_year_with_suffix : R.string.num_years_with_suffix, value);
}
return durationText;
}
public static String getPastTimeDurationWithSuffixAndSpace(Context context, long timestamp) {
if (timestamp < 0) {
return "";
}
String durationText;
Resources r = context.getResources();
long now = System.currentTimeMillis();
long duration = Math.abs(now - timestamp);
if (now - timestamp < 60000L) {
return r.getString(R.string.just_now);
} else if (duration < android.text.format.DateUtils.HOUR_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.MINUTE_IN_MILLIS);
durationText = r.getString(R.string.slide_play_num_minute_with_suffix, value);
} else if (duration < android.text.format.DateUtils.DAY_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.HOUR_IN_MILLIS);
durationText = r.getString(R.string.slide_play_num_hour_with_suffix, value);
} else if (duration < MONTH_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.DAY_IN_MILLIS);
durationText = r.getString(R.string.slide_play_num_day_with_suffix, value);
} else if (duration < android.text.format.DateUtils.YEAR_IN_MILLIS) {
int value = (int) (duration / MONTH_IN_MILLIS);
durationText = r.getString(R.string.slide_play_num_month_with_suffix, value);
} else {
int value = (int) (duration / android.text.format.DateUtils.YEAR_IN_MILLIS);
durationText = r.getString(R.string.slide_play_num_year_with_suffix, value);
}
return durationText;
}
/**
* 小于1分钟:刚刚
* 1 - 59分钟:xx分钟前
* 1 - 24小时:xx小时前
* 作品发布24小时 - 以当天自然日凌晨0点为基准:昨天
* 自然日0:00之后 - 当年自然年12月31日23:59:月/日
* 超过当年自然年12月31日23:59:年/月/日
*/
public static String getPastTimeDurationWithSuffixV2(Context context, long timestamp,
@Nullable String separator) {
if (timestamp < 0) {
return "";
}
String durationText;
Resources r = context.getResources();
long now = System.currentTimeMillis();
long duration = Math.abs(now - timestamp);
long today = now - (now + Calendar.getInstance().getTimeZone().getRawOffset()) % ONE_DAY;
long yesterday = today - ONE_DAY;
if (duration < android.text.format.DateUtils.MINUTE_IN_MILLIS) {
return r.getString(R.string.just_now);
} else if (duration < android.text.format.DateUtils.HOUR_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.MINUTE_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_minute_with_suffix : R.string.num_minutes_with_suffix, value);
} else if (duration < android.text.format.DateUtils.DAY_IN_MILLIS) {
int value = (int) (duration / android.text.format.DateUtils.HOUR_IN_MILLIS);
durationText = r.getString(
value == 1 ? R.string.num_hour_with_suffix : R.string.num_hours_with_suffix, value);
} else if (timestamp > yesterday) {
durationText = r.getString(R.string.yestoday);
} else if (isInSameYear(now, timestamp)) {
durationText = formatTimeMonthDayEn(timestamp, separator);
} else {
durationText = formatTimeYearMonthDay(timestamp, separator);
}
return durationText;
}
public static boolean isInSameYear(long timestamp) {
return isInSameYear(timestamp, System.currentTimeMillis());
}
public static boolean isInSameYear(long time1, long time2) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time1);
int year1 = calendar.get(Calendar.YEAR);
calendar.setTimeInMillis(time2);
int year2 = calendar.get(Calendar.YEAR);
return year1 == year2;
}
public static boolean isInSameMonth(long time1, long time2) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time1);
int month1 = calendar.get(Calendar.MONTH);
int year1 = calendar.get(Calendar.YEAR);
calendar.setTimeInMillis(time2);
int month2 = calendar.get(Calendar.MONTH);
int year2 = calendar.get(Calendar.YEAR);
return year1 == year2 && month1 == month2;
}
public static String getAMPM(long time) {
Resources r = AppEnv.get().getAppContext().getResources();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
int ampm = calendar.get(Calendar.AM_PM);
String text;
int hour = calendar.get(Calendar.HOUR);
if (ampm == 0) {
if (hour < 6) { // 凌晨
text = r.getString(R.string.before_dawn);
} else { // 上午
text = r.getString(R.string.forenoon);
}
} else { // 下午
text = r.getString(R.string.afternoon);
}
return text;
}
public static String getPastTimeDurationInMessage(Context context, long timestamp) {
if (timestamp < 0) {
return "";
}
long now = System.currentTimeMillis();
long today = now - (now + Calendar.getInstance().getTimeZone().getRawOffset()) % ONE_DAY;
if (isInSameYear(now, timestamp)) {
Resources r = context.getResources();
long tomorrow = today + ONE_DAY;
long yestoday = today - ONE_DAY;
long weekday = tomorrow - ONE_WEEK;
if (timestamp > tomorrow) {
return formatTimeDisplayNoYear12(timestamp);
} else if (timestamp > today) {
return getAMPM(timestamp) + " " + formatTimeToday12(timestamp);
} else if (timestamp > yestoday) {
return r.getString(R.string.yestoday) + " " + getAMPM(timestamp) + " "
+ formatTimeToday12(timestamp);
} else if (timestamp > weekday) {
return formatTimeWeek(timestamp);
} else {
return formatTimeDisplayNoYear12(timestamp);
}
} else {
return formatTimeDisplay12(timestamp);
}
}
public static String getCurrentYearMonthDay() {
synchronized (DATE_FORMAT_YMD_POINT) {
return DATE_FORMAT_YMD_POINT.format(new Date(System.currentTimeMillis()));
}
}
/**
* 传入毫秒数,返回秒数,精确到小数点后一位.
*
* <p>
* 输入:11000, 返回 "1.1s";
* 输入:10000, 返回 "1.0s".
* </p>
*
* <p>
* 非线程安全.
* </p>
*/
public static String getDecimalSeconds(long durationInMills) {
if (sDecimalSecondFormat == null) {
sDecimalSecondFormat = LocaleUSUtil.newDecimalFormat("0.0");
}
return sDecimalSecondFormat.format(durationInMills / SECOND_DECIMAL_IN_MILLIS) + "s";
}
// 判断当前时间和输入时间是否为同一天
public static boolean isSameDay(long time) {
return DATE_FORMAT_YMD_POINT.format(new Date(time)).equals(getCurrentYearMonthDay());
}
// 判断当前时间和输入时间是否为同一天
public static boolean isSameDay(long targetTime, long time) {
return DATE_FORMAT_YMD_POINT.format(new Date(targetTime))
.equals(DATE_FORMAT_YMD_POINT.format(new Date(time)));
}
/** MM月DD日 HH:MM */
public static String getTimeDisplayWithTwoSpaces(long timestamp) {
synchronized (DATE_FORMAT_DISPLAY_NOYEAR_24_EN) {
if (Utils.isZhLanguage()) {
Date date = new Date(timestamp);
return getMDDataFormat().format(date) + DATE_FORMAT_HM_24_WITH_SPACES.format(date);
} else {
return DATE_FORMAT_DISPLAY_NOYEAR_24_EN.format(new Date(timestamp));
}
}
}
/** MM/DD HH:MM */
public static String getEngTimeDisplayWithTwoSpaces(long timestamp) {
return DATE_FORMAT_DISPLAY_NOYEAR_24_EN.format(new Date(timestamp));
}
/**
* 计算两个时间相差的天数
*
* @param time1
* @param time2
* @return
*/
public static int getDetalDayCount(long time1, long time2) {
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(time1);
Calendar cal2 = Calendar.getInstance();
cal2.setTimeInMillis(time2);
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
if (year1 != year2) {
int timeDistance = 0;
for (int i = year1; i < year2; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
timeDistance += 366;
} else {
timeDistance += 365;
}
}
return timeDistance + (day2 - day1);
} else {
return day2 - day1;
}
}
/**
* 计算两个小时数(24小时制)之间的时间差值
*
* @param start 起始时间小时数,24小时制
* @param end 结束时间小时数,24小时制
* @return 两个时间之间的差值,单位为毫秒, ex: start=22,end=6,return=8 * 60 * 60 * 1000;
*/
public static long getHourTimeDiff(int start, int end) {
if (start <= end) {
return (end - start) * ONE_HOUR;
} else {
// 跨天case
return (end - start + HOURS_IN_ONE_DAY) * ONE_HOUR;
}
}
/**
* 传入毫秒数,返回分:秒形式的string
*/
public static String getDuration(long durationInMills) {
if (durationInMills >= ONE_HOUR) {
DATE_FORMAT_HMS.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
return DATE_FORMAT_HMS.format(new Date(durationInMills));
} else {
return DATE_FORMAT_MS_SINGLE.format(new Date(durationInMills));
}
}
/**
* 传入毫秒数,返回分:秒形式的string
* 例子:01:42(分秒)
*/
public static String getMSDuration(long durationInMills) {
if (durationInMills >= ONE_HOUR) {
DATE_FORMAT_HMS.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
return DATE_FORMAT_HMS.format(new Date(durationInMills));
} else {
return DATE_FORMAT_MS.format(new Date(durationInMills));
}
}
/**
* 获取当天结束时间.
*
* @return 当天23:59:59:999的时间戳,单位ms
*/
public static long getCurrentDayEndTime() {
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
return todayEnd.getTime().getTime();
}
/**
* 获取当天某时刻
*/
public static long getCurrentDayAtTime(int hour, int minute, int second, int millisecond) {
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.HOUR_OF_DAY, hour);
todayEnd.set(Calendar.MINUTE, minute);
todayEnd.set(Calendar.SECOND, second);
todayEnd.set(Calendar.MILLISECOND, millisecond);
return todayEnd.getTime().getTime();
}
/**
* 获取传入时间的多语言月份字符串
*
* @return 比如 英文环境去前三位缩写(首字母大写):Jun;中文:6月
*/
public static String getMultiLanguageMonthSummary(long timestamp) {
if (Utils.isZhLanguage()) {
Calendar instance = Calendar.getInstance();
instance.setTimeInMillis(timestamp);
return String.format(Locale.CHINA, MONTH_FORMAT, instance.get(Calendar.MONTH) + 1);
} else {
return DATE_FORMAT_MMM.format(new Date(timestamp));
}
}
public static String getSatDate(long time) {
return SAT_DATE.format(new Date(time));
}
/**
* XX年X月X日
*/
public static String formatTimeDisplayYMD(long timestamp) {
synchronized (DATE_FORMAT_YMD_EN) {
if (Utils.isZhLanguage()) {
Date date = new Date(timestamp);
return getYMDDataFormat().format(date);
} else {
return DATE_FORMAT_YMD_EN.format(new Date(timestamp));
}
}
}
}