package utils; import android.text.TextUtils; import android.util.Log; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; /** * 日期:2017.01.12 *

* 作者:chigan *

* 描述:目期工具类 */ public class DateUtils { private static final String TAG = "DateUtils"; public static String getTimeStart(String timeString) { String timeStamp = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); Date d = new Date(); try { d = sdf.parse(timeString); long l = d.getTime(); if (String.valueOf(l).length() > 10) { l = l / 10; } timeStamp = String.valueOf(l); } catch (Exception e) { } return timeStamp; } public static String getTimeEnd(String timeString) { String timeStamp = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm"); Date d = new Date(); try { d = sdf.parse((timeString + "23:59")); long l = d.getTime(); if (String.valueOf(l).length() > 10) { l = l / 10; } timeStamp = String.valueOf(l); } catch (Exception e) { } return timeStamp; } public static String getDate(String mtime) { return getTimeStrByLong(dataOne(mtime)); } /** * 获取时间串 * * @param longStr 秒 * @return 1月前 1周前 1天前 1小时前 1分钟前 */ public static String getTimeStrByLong(String longStr) { try { Calendar calendar = new GregorianCalendar(); Date date = calendar.getTime(); Long clv = date.getTime(); Long olv = Long.valueOf(longStr); calendar.setTimeInMillis(olv); // 转毫秒 Date date2 = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String curtime = format.format(date2); Long belv = clv - olv; String retStr = ""; // 24 * 60 * 60 * 1000; Long daylong = Long.valueOf("86400000"); Long hourlong = Long.valueOf("3600000"); Long minlong = Long.valueOf("60000"); Long seclong = Long.valueOf("1000"); if (belv >= daylong * 30) {// 月 Long mul = belv / (daylong * 30); retStr = retStr + mul + "月"; belv = belv % (daylong * 30); return retStr + "前"; } if (belv >= daylong * 7) {// 周 Long mul = belv / (daylong * 7); retStr = retStr + mul + "周"; belv = belv % (daylong * 7); return retStr + "前"; } if (belv >= daylong) {// 天 Long mul = belv / daylong; retStr = retStr + mul + "天"; belv = belv % daylong; return retStr + "前"; } if (belv >= hourlong) {// 时 Long mul = belv / hourlong; retStr = retStr + mul + "小时"; belv = belv % hourlong; return retStr + "前"; } if (belv >= minlong) {// 分 Long mul = belv / minlong; retStr = retStr + mul + "分钟"; return retStr + "前"; } if (belv >= seclong) { Long mul = belv / seclong; retStr = retStr + mul + "秒"; return retStr + "前"; } } catch (Exception e) { Log.e("ttterr", longStr); return ""; } return ""; } /** * 返回xxxx年-xx月xx日 xx时:xx分:xx秒 * * @param strMis 毫秒的字符串形式 * @return */ public static String getStringDataMilli(String strMis) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date(Long.parseLong(strMis))); } /** * 返回xx月xx日 xx时:xx分 * * @return */ public static String getStringDataMilliNoy() { SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm"); return sdf.format(new Date(System.currentTimeMillis())); } /** * 返回xxxx年-xx月xx日 xx时:xx分:xx秒 * * @param strSecond 秒的字符串形式所以要*1000 * @return */ public static String getStringDataSecond(String strSecond) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date(Long.parseLong(strSecond) * 1000)); } /** * 返回xxxx年-xx月xx日 * * @param strMis * @return */ public static String getStringYMD(String strMis) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if (TextUtils.isEmpty(strMis)) { return ""; } return sdf.format(new Date(Long.parseLong(strMis) * 1000)); } /** * 返回xxxx年-xx月xx日 * * @param date * @return */ public static String getStringYMD(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(date); } /** * 返回xxxx年-xx月xx日 xx时xx分 * * @param strMis * @return */ public static String getStringYMDHM(String strMis) { // Log.d(TAG, "getStringYMDHM: " + strMis); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return sdf.format(new Date(Long.parseLong(strMis) * 1000)); } /** * 返回xxxx年-xx月xx日 xx时xx分 * * @param date * @return */ public static String getStringYMDHM(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return sdf.format(date); } /** * 获取当前是哪一天 * * @return */ public static int getCurrentDay() { SimpleDateFormat sdf = new SimpleDateFormat("dd"); return Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))); } /** * 获取当前是哪个月 * * @return */ public static int getCurrentMonth() { SimpleDateFormat sdf = new SimpleDateFormat("MM"); return Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) - 1; } /** * 获取当前是哪一年 * * @return */ public static int getCurrentYear() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); return Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))); } /** * 根据一个时间磋,获取是多少天 * * @param time * @return */ public static int getDay(long time) { long day = (time * 1000) / (24 * 60 * 60 * 1000); return (int) day; } /** * 获取时间戳 是多少分钟 * * @param timestamp * @return */ public static int getMinute(long timestamp) { long minute = timestamp * 1000 / (60 * 1000); return (int) minute; } /** * 计算多少天开始 * * @param starTime 时间轴 * @return 天数 */ public static int beginDate(String starTime) { long now = System.currentTimeMillis(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH"); String d = format.format(Long.valueOf(starTime) * 1000); Long start = null; try { start = format.parse(d).getTime(); } catch (ParseException e) { e.printStackTrace(); } long b = start - now; float f = (float) (b * 1.0 / 60 / 60 / 24 / 1000); double days = (b * 1.0 / 60 / 60 / 24 / 1000); return (int) Math.ceil(days); } /** * 返回一个时间轴和现在的相隔时间 如果时间在当前时间之前则返回空 * * @param longStr * @return */ public static String getTimeStrToBegin(String longStr) { Calendar calendar = new GregorianCalendar(); Date date = calendar.getTime(); Long clv = date.getTime(); Long olv = Long.valueOf(longStr); calendar.setTimeInMillis(olv); // 转毫秒 Date date2 = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String curtime = format.format(date2); Long belv = olv * 1000 - clv; String retStr = ""; // 24 * 60 * 60 * 1000; Long daylong = Long.valueOf("86400000"); Long hourlong = Long.valueOf("3600000"); Long minlong = Long.valueOf("60000"); Long seclong = Long.valueOf("1000"); if (belv >= daylong * 30) {// 月 Long mul = belv / (daylong * 30); retStr = retStr + mul + "月"; belv = belv % (daylong * 30); return retStr; } if (belv >= daylong * 7) {// 周 Long mul = belv / (daylong * 7); retStr = retStr + mul + "周"; belv = belv % (daylong * 7); return retStr; } if (belv >= daylong) {// 天 Long mul = belv / daylong; retStr = retStr + mul + "天"; belv = belv % daylong; return retStr; } if (belv >= hourlong) {// 时 Long mul = belv / hourlong; retStr = retStr + mul + "小时"; belv = belv % hourlong; return retStr; } if (belv >= minlong) {// 分 Long mul = belv / minlong; retStr = retStr + mul + "分钟"; return retStr; } if (belv >= seclong) { Long mul = belv / seclong; retStr = retStr + mul + "秒"; return retStr; } return ""; } /** * 调此方法输入所要转换的时间输入例如("2014-06-14-16-09-00")返回时间戳 * * @param time * @return */ public static String dataOne(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); Date date = null; try { date = sdr.parse(time); return date.getTime() + ""; } catch (ParseException e) { e.printStackTrace(); } return ""; } public static Long getUnitDate(String time){ SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA); Date date = null; try { date = sdr.parse(time); return date.getTime() ; } catch (ParseException e) { e.printStackTrace(); } return 0l; } public static String getSpecifiedDayAfter(String specifiedDay) { Calendar c = Calendar.getInstance(); Date date = null; try { date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day + 1); String dayAfter = new SimpleDateFormat("yyyy-MM-dd") .format(c.getTime()); return dayAfter; } /** * 月份+i * * @param i * @return */ public static String getmonthAdd(int i, String specifiedDay) { Calendar c = Calendar.getInstance(); Date date = null; try { date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); int month = c.get(Calendar.MONTH); c.set(Calendar.MONTH, month + i); String dayAfter = new SimpleDateFormat("yyyy-MM-dd") .format(c.getTime()); return dayAfter; } public static Calendar getParkingStart(String info) { Calendar c = Calendar.getInstance(); Date date = null; try { date = new SimpleDateFormat("yy-MM-dd").parse(DateUtils.getSpecifiedDayAfter(info)); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); return c; } public static Calendar getParkingMaxEnd(String info) { Calendar c = Calendar.getInstance(); Date date = null; try { date = new SimpleDateFormat("yy-MM-dd").parse(DateUtils.getSpecifiedDayAfter(info)); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); int month = c.get(Calendar.MONTH); c.set(Calendar.MONTH, month + 12); return c; } public static String getUnit(String info) { if (TextUtils.isEmpty(info)) { return ""; } boolean numericZidai = isNumericZidai(info); if (numericZidai) { return info + "单元"; } else { return info; } } public static boolean isNumericZidai(String str) { for (int i = 0; i < str.length(); i++) { System.out.println(str.charAt(i)); if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } }