/** * Created by DateUtils on 2019/10/14. */ public class DateUtils { private static final String TAG = "DataUtils"; public static String getTodayDateTime() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); return format.format(new Date()); } public static String getTodayDateTime(String dateType) { SimpleDateFormat format = new SimpleDateFormat(dateType, Locale.getDefault()); return format.format(new Date()); } /** * 时间加减 * * @return */ public static long date2TimeStamp(String start, String end) throws ParseException { DateFormat df = new SimpleDateFormat("HH:mm:ss"); Date d1 = df.parse(start); Date d2 = df.parse(end); long diff = d1.getTime() - d2.getTime(); return diff; } /** * 掉此方法输入所要转换的时间输入例如("2014年06月14日16时09分00秒")返回时间戳 * * @param time * @return */ public static String data(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒", Locale.CHINA); Date date; String times = null; try { date = sdr.parse(time); long l = date.getTime(); String stf = String.valueOf(l); times = stf.substring(0, 10); } catch (Exception e) { e.printStackTrace(); } return times; } /** * 调此方法输入所要转换的时间输入例如("2014-06-14-16-09-00")返回时间戳 * * @param time * @return */ public static long dataTwo(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); Date date; String times = null; try { date = sdr.parse(time); long l = date.getTime(); return l; } catch (Exception e) { e.printStackTrace(); } return 0; } public static String time(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } /** * 根据传递的类型格式化时间 * * @param str * @param type 例如:yy-MM-dd * @return */ public static String getDateTimeByMillisecond(String str, String type) { Date date = new Date(Long.valueOf(str)); SimpleDateFormat format = new SimpleDateFormat(type); String time = format.format(date); return time; } /** * 返回时间戳 * * @return */ public static String getTime() { long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳 String str = String.valueOf(time); return str; } }
DateUtils 获取时间工具类
最新推荐文章于 2024-05-15 17:39:40 发布