时间处理工具
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class TimeUtil {
public static final int SECONDS_IN_DAY = 60 * 60 * 24;
public static final long MILLIS_IN_DAY = 1000L * SECONDS_IN_DAY;
private static final long ONE_MINUTE = 60000L;
private static final long ONE_HOUR = 3600000L;
private static final long ONE_DAY = 86400000L;
private static final long ONE_WEEK = 604800000L;
private static final String ONE_SECOND_AGO = "秒前";
private static final String ONE_MINUTE_AGO = "分钟前";
private static final String ONE_HOUR_AGO = "小时前";
private static final String ONE_DAY_AGO = "天前";
private static final String ONE_MONTH_AGO = "月前";
private static final String ONE_YEAR_AGO = "年前";
private static SimpleDateFormat sdf = new SimpleDateFormat();
public final static String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss.SSS";
public static boolean isSameDayOfMillis(final long ms1, final long ms2) {
final long interval = ms1 - ms2;
return interval < MILLIS_IN_DAY && interval > -1L * MILLIS_IN_DAY && toDay(ms1) == toDay(ms2);
}
private static long toDay(long millis) {
return (millis + TimeZone.getDefault().getOffset(millis)) / MILLIS_IN_DAY;
}
public static String parseTimeFromLong(long lon) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date(lon));
return date;
}
public static long dateToLong(String s) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
return ts;
} catch (Exception e) {
return 0;
}
}
public static String parseDataFromLong(long data) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date(data));
return date;
} catch (Exception e) {
return "暂无";
}
}
public static String parseDataFromLongUTF(long data) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String date = sdf.format(new Date(data));
return date;
}
public static String getCurrentTimeString(String format) {
if (format == null || format.trim().equals("")) {
sdf.applyPattern(FORMAT_DATE_TIME);
} else {
sdf.applyPattern(format);
}
return sdf.format(new Date());
}
public static String getDescriptionTimeFromDateString(String dateString) {
if (TextUtils.isEmpty(dateString)) {
return "";
}
sdf.applyPattern(FORMAT_DATE_TIME);
try {
return getDescriptionTimeFromDate(sdf.parse(formatZhuiShuDateString(dateString)));
} catch (Exception e) {
Log_Ma.e("timeUtils", "getDescriptionTimeFromDateString: " + e);
}
return "";
}
public static String formatZhuiShuDateString(String dateString) {
return dateString.replaceAll("T", " ").replaceAll("Z", "");
}
public static String getDescriptionTimeFromDate(Date date) {
long delta = new Date().getTime() - date.getTime();
if (delta < 1L * ONE_MINUTE) {
long seconds = toSeconds(delta);
return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
}
if (delta < 45L * ONE_MINUTE) {
long minutes = toMinutes(delta);
return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
}
if (delta < 24L * ONE_HOUR) {
long hours = toHours(delta);
return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
}
if (delta < 48L * ONE_HOUR) {
return "昨天";
}
if (delta < 30L * ONE_DAY) {
long days = toDays(delta);
return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
}
if (delta < 12L * 4L * ONE_WEEK) {
long months = toMonths(delta);
return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
} else {
long years = toYears(delta);
return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
}
}
public static long toSeconds(long date) {
return date / 1000L;
}
private static long toMinutes(long date) {
return toSeconds(date) / 60L;
}
private static long toHours(long date) {
return toMinutes(date) / 60L;
}
private static long toDays(long date) {
return toHours(date) / 24L;
}
private static long toMonths(long date) {
return toDays(date) / 30L;
}
private static long toYears(long date) {
return toMonths(date) / 365L;
}
public static int getCurrentDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
if (date != null) {
calendar.setTime(date);
} else {
calendar.setTime(new Date());
}
int i = calendar.get(Calendar.DAY_OF_WEEK);
return i;
}
public static Date getDateForLastDay(Date date, int amount) {
Calendar ca = Calendar.getInstance();
if (date != null) {
ca.setTime(date);
} else {
ca.setTime(new Date());
}
ca.add(Calendar.DATE, -amount);
Date lastDay = ca.getTime();
return lastDay;
}
public static Date getDateForNextDay(Date date, int amount) {
Calendar ca = Calendar.getInstance();
if (date != null) {
ca.setTime(date);
} else {
ca.setTime(new Date());
}
ca.add(Calendar.DATE, +amount);
Date lastDay = ca.getTime();
return lastDay;
}
public static void selectTime(Context context, boolean b, TimeListener timeListener) {
Calendar startDate = Calendar.getInstance();
startDate.set(1920, 0, 1);
Calendar pointDate = Calendar.getInstance();
pointDate.set(2000, 0, 1);
Calendar c = Calendar.getInstance();
Calendar endDate = Calendar.getInstance();
endDate.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
TimePickerView pvTime = new TimePickerBuilder(context, new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {
timeListener.getListener(date);
}
})
.setDate(b ? endDate : pointDate)
.setRangDate(startDate, endDate)
.build();
pvTime.show();
}
public static void selectTime(Context context, TimeListener timeListener) {
selectTime(context, false, timeListener);
}
public static String long2String(long time) {
int sec = (int) time / 1000;
int min = sec / 60;
sec = sec % 60;
if (min < 10) {
if (sec < 10) {
return "0" + min + ":0" + sec;
} else {
return "0" + min + ":" + sec;
}
} else {
if (sec < 10) {
return min + ":0" + sec;
} else {
return min + ":" + sec;
}
}
}
public interface TimeListener {
void getListener(Date date);
}
}