使用环境:
需要获取到时间转换成固定格式,将毫秒转化成时间,获取当前周几等等
使用方法:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; /** * Created by sensyang on 2017/11/18. * * @Emial:[email protected] */ public class TimeUtil { public static TimeUtil instance; public static TimeUtil getInstance() { if (instance == null) { instance = new TimeUtil(); } return instance; } public static String getFormatDateTime(int dateTime) { Date d = new Date(dateTime); SimpleDateFormat sdf = new SimpleDateFormat("mm:ss"); return sdf.format(d); } public static String timeParse(long duration) { String time = ""; long minute = duration / 60000; long seconds = duration % 60000; long second = Math.round((float) seconds / 1000); if (minute < 10) { time += "0"; } time += minute + ":"; if (second < 10) { time += "0"; } time += second; return "00:" + time; } /** * 将毫秒转化成固定格式的时间 * 时间格式: yyyy-MM-dd HH:mm:ss * * @param millisecond * @return */ public static String getDateTimeFromMillisecond(Long millisecond) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(millisecond); String dateStr = simpleDateFormat.format(date); return dateStr; } /** * 将时间转化成毫秒 * 时间格式: yyyy-MM-dd HH:mm:ss * @param time * @return */ public static Long timeStrToSecond(String time) { try { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Long second = format.parse(time).getTime(); return second; } catch (Exception e) { e.printStackTrace(); } return -1L; } /** * 获取时间间隔 * 因为项目里没用到,所以未曾测试真伪 * @param millisecond * @return */ public static String getSpaceTime(Long millisecond) { Calendar calendar = Calendar.getInstance(); Long currentMillisecond = calendar.getTimeInMillis(); //间隔秒 Long spaceSecond = (currentMillisecond - millisecond) / 1000; //一分钟之内 if (spaceSecond >= 0 && spaceSecond < 60) { return "片刻之前"; } //一小时之内 else if (spaceSecond / 60 > 0 && spaceSecond / 60 < 60) { return spaceSecond / 60 + "分钟之前"; } //一天之内 else if (spaceSecond / (60 * 60) > 0 && spaceSecond / (60 * 60) < 24) { return spaceSecond / (60 * 60) + "小时之前"; } //3天之内 else if (spaceSecond / (60 * 60 * 24) > 0 && spaceSecond / (60 * 60 * 24) < 3) { return spaceSecond / (60 * 60 * 24) + "天之前"; } else { return getDateTimeFromMillisecond(millisecond); } } /** * 获取当前时间 */ public static String getNowTime() { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date curDate = new Date(System.currentTimeMillis()); String str = formatter.format(curDate); return str; } /** * 将指定格式的字符串转换成时间 * * @param str * @return */ public static Long formatDate(String str) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); long milltimes = 0; try { milltimes = sdf.parse(str).getTime(); } catch (ParseException e) { e.printStackTrace(); } return milltimes; } /** * 获取当前时间的毫秒值 */ public static Long getNowTimeLong() { Date dt = new Date(); Long time = dt.getTime(); return time; } // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒 // data Date类型的时间 public static String dateToString(Date data, String formatType) { return new SimpleDateFormat(formatType).format(data); } /** * 获取今天日期, 星期几 */ public static String getDataString() { String mYear; String mMonth; String mDay; String mWay; final Calendar c = Calendar.getInstance(); c.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); mYear = String.valueOf(c.get(Calendar.YEAR)); mMonth = String.valueOf(c.get(Calendar.MONTH) + 1); mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH)); mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK)); if ("1".equals(mWay)) { mWay = "天"; } else if ("2".equals(mWay)) { mWay = "一"; } else if ("3".equals(mWay)) { mWay = "二"; } else if ("4".equals(mWay)) { mWay = "三"; } else if ("5".equals(mWay)) { mWay = "四"; } else if ("6".equals(mWay)) { mWay = "五"; } else if ("7".equals(mWay)) { mWay = "六"; } return mYear + "年" + mMonth + "月" + mDay + "日" + "/星期" + mWay; } }