有时间我们在开发过程中会遇见一些常见的时间转换 接下来我就写出几种常用的吧
/** * @param 此为根据已有的时间戳 来得到 2017年 3 月 3日 这样的时间 * @return */ public static String timeslashData(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); String times = sdr.format(new Date(lcc * 1000L)); return times; }
/** * @param 此为根据已有的时间戳 来得到 星期几 * @return */
private static String getWeek(long timeStamp) { int mydate = 0; String week = null; Calendar cd = Calendar.getInstance(); cd.setTime(new Date(timeStamp)); mydate = cd.get(Calendar.DAY_OF_WEEK); // 获取指定日期转换成星期几 if (mydate == 1) { week = "周日"; } else if (mydate == 2) { week = "周一"; } else if (mydate == 3) { week = "周二"; } else if (mydate == 4) { week = "周三"; } else if (mydate == 5) { week = "周四"; } else if (mydate == 6) { week = "周五"; } else if (mydate == 7) { week = "周六"; } return week; }
/** * 获取上个月的年月 yue为-1 带表前一个月 0 为当前月 1 为下个月 * * @param date * @return */ private static Date getLastDate(Date date, int yue) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, yue); return cal.getTime(); }
/** * 获取昨天 后天 大后天 大大后天 同样 day 为-1则为昨天 0 为今天 1 为明天的日期 * * @param day */ private void getTodayOrYesterday(int day) {//date 是存储的时间戳 Date date = new Date();//取时间 Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.add(calendar.DATE, day);//把日期往后增加一天.整数往后推,负数往前移动 date = calendar.getTime(); //这个时间就是日期往后推一天的结果 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(date); System.out.println(dateString); // Log.i("TAG", "time===" + dateString); }