| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- package com.xzjmyk.pm.activity.util;
- import android.content.Context;
- import android.text.TextUtils;
- import android.widget.TextView;
- import com.xzjmyk.pm.activity.R;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- @SuppressWarnings("deprecation")
- public class TimeUtils {
- // ///s 代表Simple日期格式:yyyy-MM-dd
- // ///f 代表Full日期格式:yyyy-MM-dd hh:mm:ss
- public static final SimpleDateFormat ss_format = new SimpleDateFormat("MM-dd");
- public static final SimpleDateFormat day_format = new SimpleDateFormat("yyyy年MM月dd日");
- public static final SimpleDateFormat s_format = new SimpleDateFormat("yyyy-MM-dd");
- public static final SimpleDateFormat f_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- public static final SimpleDateFormat int_format = new SimpleDateFormat("yyyyMMdd");
- public static final SimpleDateFormat ym_format = new SimpleDateFormat("yyyyMM");
- public static String getWeek(String date) { //yyyy-MM-dd
- Calendar c = Calendar.getInstance();
- c.setTimeInMillis(TimeUtils.s_str_2_long(date));
- String src = "";
- int week = c.get(Calendar.DAY_OF_WEEK);
- switch (week) {
- case 1:
- src = "星期天";
- break;
- case 2:
- src = "星期一";
- break;
- case 3:
- src = "星期二";
- break;
- case 4:
- src = "星期三";
- break;
- case 5:
- src = "星期四";
- break;
- case 6:
- src = "星期五";
- break;
- case 7:
- src = "星期六";
- break;
- }
- return src;
- }
- public static long s_str_2_long(String dateString) {
- try {
- Date d = s_format.parse(dateString);
- return d.getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public static String day_long_2_str(long timestamp) {
- return day_format.format(new Date(timestamp));
- }
- public static long int_str_2_long(String date) {
- try {
- Date d = int_format.parse(date);
- return d.getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public static int int_long_2_str(long timestamp) {
- return Integer.parseInt(int_format.format(new Date(timestamp)));
- }
- public static int ym_long_2_str(long timestamp) {
- return Integer.parseInt(ym_format.format(new Date(timestamp)));
- }
- public static long f_str_2_long(String dateString) {
- try {
- Date d = f_format.parse(dateString);
- return d.getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public static String ss_long_2_str(long timestamp) {
- return ss_format.format(new Date(timestamp));
- }
- public static String s_long_2_str(long timestamp) {
- return s_format.format(new Date(timestamp));
- }
- public static String f_long_2_str(long timestamp) {
- return f_format.format(new Date(timestamp));
- }
- /**
- * 获取字符串时间的年份
- *
- * @param dateString 格式为yyyy-MM-ss,或者yyyy-MM-dd HH:mm:ss
- * @return
- */
- public static int getYear(String dateString) {
- try {
- Date d = s_format.parse(dateString);
- return d.getYear() + 1900;// 年份是基于格林威治时间,所以加上1900
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- /**
- * 获取字符串时间的月份
- *
- * @param dateString 格式为yyyy-MM-ss,或者yyyy-MM-dd hh:mm:ss
- * @return
- */
- public static int getMonth(String dateString) {
- try {
- Date d = s_format.parse(dateString);
- return d.getMonth();// 月份从0-11
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return 0;
- }
- /**
- * 获取字符串时间的天
- *
- * @param dateString 格式为yyyy-MM-ss,或者yyyy-MM-dd hh:mm:ss
- * @return
- */
- public static int getDayOfMonth(String dateString) {
- try {
- Date d = s_format.parse(dateString);
- return d.getDate();
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return 0;
- }
- public static int getHours(String timeString) {
- SimpleDateFormat formart = new SimpleDateFormat("HH:mm:ss");
- try {
- Date date = formart.parse(timeString);
- return date.getHours();
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return 0;
- }
- public static int getMinutes(String timeString) {
- SimpleDateFormat formart = new SimpleDateFormat("HH:mm:ss");
- try {
- Date date = formart.parse(timeString);
- return date.getMinutes();
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return 0;
- }
- public static int getSeconds(String timeString) {
- SimpleDateFormat formart = new SimpleDateFormat("HH:mm:ss");
- try {
- Date date = formart.parse(timeString);
- return date.getSeconds();
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return 0;
- }
- public static String getCurrentTime() {
- return f_format.format(new Date(System.currentTimeMillis()));
- }
- /**
- * 在当前时间上加上多少毫秒,返回这个时间
- *
- * @param mask
- * @return
- */
- public static String getCurrentTimeMask(long mask) {
- return f_format.format(new Date(System.currentTimeMillis() + mask));
- }
- // /////////////////////以上是通用的,下面为特殊需求的////////////////////////
- // /**
- // * 时间戳转换日期格式
- // *
- // * @param timestamp
- // * 单位秒
- // * @return
- // */
- // public static String getCurrentTime(long timestamp) {
- // SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- // return f.format(new Date(timestamp * 1000));
- // }
- /**
- * 获取精简的日期
- *
- * @param time
- * @return
- */
- public static String getSimpleDate(String time) {
- SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
- Date date = null;
- try {
- date = f_format.parse(time);
- return formater.format(date);
- } catch (ParseException e) {
- e.printStackTrace();
- return "";
- }
- }
- /**
- * @param time
- * @return
- */
- public static String getSimpleDateTime(String time) {
- SimpleDateFormat formater = new SimpleDateFormat("yy-MM-dd HH:mm");
- Date date = null;
- try {
- date = f_format.parse(time);
- return formater.format(date);
- } catch (ParseException e) {
- e.printStackTrace();
- return "";
- }
- }
- public static String getSimpleTime(String time) {
- SimpleDateFormat formater = new SimpleDateFormat("HH:mm");
- Date date = null;
- try {
- date = f_format.parse(time);
- return formater.format(date);
- } catch (ParseException e) {
- e.printStackTrace();
- return "";
- }
- }
- public static String getChatSimpleDate(String time) {
- SimpleDateFormat formater = new SimpleDateFormat("yy-MM-dd");
- Date date = null;
- try {
- date = f_format.parse(time);
- return formater.format(date);
- } catch (ParseException e) {
- e.printStackTrace();
- return "";
- }
- }
- public static String getTimeHM(String time) {
- SimpleDateFormat formater = new SimpleDateFormat("HH:mm");
- Date date = null;
- try {
- date = f_format.parse(time);
- return formater.format(date);
- } catch (ParseException e) {
- e.printStackTrace();
- return "";
- }
- }
- public static SimpleDateFormat friendly_format1 = new SimpleDateFormat("HH:mm");
- public static SimpleDateFormat friendly_format2 = new SimpleDateFormat("MM-dd HH:mm");
- /**
- * 获取友好的时间显示
- *
- * @param time 秒级别的时间戳
- * @return
- */
- public static String getFriendlyTimeDesc(Context context, int time) {
- String desc = "";
- Date timeDate = new Date(time * 1000L);
- Date nowDate = new Date();
- long delaySeconds = nowDate.getTime() / 1000 - time;// 相差的秒数
- if (delaySeconds < 10) {// 小于10秒,显示刚刚
- desc = context.getString(R.string.friendly_time_just_now);// 显示刚刚
- } else if (delaySeconds <= 60) {// 小于1分钟,显示如“25秒前”
- desc = delaySeconds + context.getString(R.string.friendly_time_before_seconds);
- } else if (delaySeconds < 60 * 30) {// 小于30分钟,显示如“25分钟前”
- desc = (delaySeconds / 60) + context.getString(R.string.friendly_time_before_minute);
- } else if (delaySeconds < 60 * 60 * 24) {// 小于1天之内
- if (nowDate.getDay() - timeDate.getDay() == 0) {// 同一天
- desc = friendly_format1.format(timeDate);
- } else {// 前一天
- desc = context.getString(R.string.friendly_time_yesterday) + " " + friendly_format1.format(timeDate);
- }
- } else if (delaySeconds < 60 * 60 * 24 * 2) {// 小于2天之内
- if (nowDate.getDay() - timeDate.getDay() == 1 || nowDate.getDay() - timeDate.getDay() == -6) {// 昨天
- desc = context.getString(R.string.friendly_time_yesterday) + " " + friendly_format1.format(timeDate);
- } else {// 前天
- desc = context.getString(R.string.friendly_time_before_yesterday) + " " + friendly_format1.format(timeDate);
- }
- } else if (delaySeconds < 60 * 60 * 24 * 3) {// 小于三天
- if (nowDate.getDay() - timeDate.getDay() == 2 || nowDate.getDay() - timeDate.getDay() == -5) {// 前天
- desc = context.getString(R.string.friendly_time_before_yesterday) + " " + friendly_format1.format(timeDate);
- }
- // else 超过前天
- }
- if (TextUtils.isEmpty(desc)) {
- desc = friendly_format2.format(timeDate);
- }
- return desc;
- }
- public static String sk_time_friendly_format2(long time) {
- return friendly_format2.format(new Date(time * 1000));
- }
- public static String sk_time_s_long_2_str(long time) {
- return s_long_2_str(time * 1000);
- }
- public static String sk_time_ss_long_2_str(long time) {
- return ss_long_2_str(time * 1000);
- }
- public static long sk_time_s_str_2_long(String dateString) {
- return s_str_2_long(dateString) / 1000;
- }
- public static int sk_time_current_time() {
- return (int) (System.currentTimeMillis() / 1000);
- }
- private static SimpleDateFormat hm_formater = new SimpleDateFormat("HH:mm");
- public static String sk_time_long_to_hm_str(long time) {
- try {
- return hm_formater.format(new Date(time * 1000));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
- public static String sk_time_long_to_chat_time_str(long time) {
- String date1 = sk_time_s_long_2_str(time);
- String date2 = sk_time_s_long_2_str(System.currentTimeMillis() / 1000);
- if (date1.compareToIgnoreCase(date2) == 0) {// 是同一天
- return sk_time_long_to_hm_str(time);
- } else {
- return long_to_yMdHm_str(time * 1000);
- }
- }
- public static final SimpleDateFormat sk_format_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- // 日期加小时的字符串
- public static String long_to_yMdHm_str(long time) {
- return sk_format_1.format(new Date(time));
- }
- public static long sk_time_yMdHm_str_to_long(String time) {
- try {
- return sk_format_1.parse(time).getTime() / 1000;
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public static int yMdHm_getYear(String dateString) {
- try {
- Date d = sk_format_1.parse(dateString);
- return d.getYear() + 1900;// 年份是基于格林威治时间,所以加上1900
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public static int yMdHm_getMonth(String dateString) {
- try {
- Date d = sk_format_1.parse(dateString);
- return d.getMonth();// 月份从0-11
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public static int yMdHm_getDayOfMonth(String dateString) {
- try {
- Date d = sk_format_1.parse(dateString);
- return d.getDate();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public static int yMdHm_getHours(String timeString) {
- try {
- Date date = sk_format_1.parse(timeString);
- return date.getHours();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public static int yMdHm_getMinutes(String timeString) {
- try {
- Date date = sk_format_1.parse(timeString);
- return date.getMinutes();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return 0;
- }
- /**
- * @param textView
- * @param time 时间戳/1000
- * @return
- */
- public static long getSpecialBeginTime(TextView textView, long time) {
- long currentTime = System.currentTimeMillis() / 1000;
- if (time > currentTime) {
- time = currentTime;
- }
- textView.setText(sk_time_s_long_2_str(time));
- return time;
- }
- /**
- * @param textView
- * @param time 时间戳/1000
- * @return
- */
- public static long getSpecialEndTime(TextView textView, long time) {
- long currentTime = System.currentTimeMillis() / 1000;
- if (time == 0 || time > currentTime - 24 * 60 * 60) {
- textView.setText(R.string.to_this_day);
- return 0;
- }
- textView.setText(sk_time_s_long_2_str(time));
- return time;
- }
- public static int sk_time_age(long birthday) {
- int age = (new Date().getYear()) - (new Date(birthday * 1000).getYear());
- if (age < 0 || age > 100) {
- return 25;
- }
- return age;
- }
- }
|