/** * 两个时间相差距离多少天多少小时多少分多少秒 * * @param str1 * 时间参数 1 格式:1990-01-01 12:00:00 * @param str2 * 时间参数 2 格式:2009-01-01 12:00:00 * @return String 返回值为:xx天xx小时xx分xx秒 */ public static String getDistanceTime(String str1, String str2) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date one; Date two; long day = 0; long hour = 0; long min = 0; long sec = 0; try { one = df.parse(str1); two = df.parse(str2); long time1 = one.getTime(); long time2 = two.getTime(); long diff; if (time1 < time2) { diff = time2 - time1; } else { diff = time1 - time2; } day = diff / (24 * 60 * 60 * 1000); hour = (diff / (60 * 60 * 1000) - day * 24); min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); } catch (ParseException e) { e.printStackTrace(); } return day + "天" + hour + "小时" + min + "分" + sec + "秒"; }
//得到上个月的时间 public static String getlastmonth(){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calendar.getInstance(); c.setTime(new Date()); c.add(Calendar.MONTH, -1); Date m = c.getTime(); String mon = format.format(m); mon =mon.substring(0, 7); return mon; }
// 获取当前的时间 public static String getapplyTime() { // 将Date类型转成String类型,以String作为表名,保证表名唯一 Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); String tablename = dateFormat.format(now); System.out.println(tablename); return tablename; }
// 获取当前的日期 public static String getapplyDate() { // 将Date类型转成String类型,以String作为表名,保证表名唯一 Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String tablename = dateFormat.format(now); System.out.println(tablename); return tablename; }
// 判断现在是几点 public static int getisSignIn() { Date date = new Date(); SimpleDateFormat df = new SimpleDateFormat("HH"); String str = df.format(date); int a = Integer.parseInt(str); if (a >= 0 && a <= 6) { System.out.println("现在是凌晨" + a + "点"); return 1; } if (a > 6 && a <= 12) { System.out.println("现在是上午" + a + "点"); return 2; } if (a > 12 && a <= 13) { System.out.println("现在是中午" + a + "点"); return 0; } if (a > 13 && a <= 17) { System.out.println("现在是下午" + a + "点"); return 3; } if (a > 17 && a <= 24) { System.out.println("现在是晚上" + a + "点"); return 4; } return 0; }