Java判斷日期是否是本日,本週,本月
阿新 • • 發佈:2018-12-11
//判斷選擇的日期是否是今天 public static boolean isToday(long time) { return isThisTime(time, "yyyy-MM-dd"); } //判斷選擇的日期是否是本週 public static boolean isThisWeek(long time) { Calendar calendar = Calendar.getInstance(); int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR); calendar.setTime(new Date(time)); int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR); if (paramWeek == currentWeek) { return true; } return false; } //判斷選擇的日期是否是本月 public static boolean isThisMonth(long time) { return isThisTime(time, "yyyy-MM"); } public static boolean isThisTime(long time, String pattern) { Date date = new Date(time); SimpleDateFormat sdf = new SimpleDateFormat(pattern); String param = sdf.format(date);//引數時間 String now = sdf.format(new Date());//當前時間 if (param.equals(now)) { return true; } return false; } @Test public void testTime() { isToday(1416360654000L); isThisMonth(1416360654000L); isThisWeek(1416360654000L); }
public static String getNotifDateStrEnglish(long timeStamp) { String result = ""; try { SimpleDateFormat sf1 = new SimpleDateFormat("EEEE,MMMM dd,yyyy h:m"); result = sf1.format(new Date(timeStamp * 1000L)); } catch (Exception e) { e.printStackTrace(); } return result; } public static String getNotifDateStr(long timeStamp) { String result = ""; try { SimpleDateFormat sf1 = new SimpleDateFormat("dd/MM/yyyy hh:mm a"); result = sf1.format(new Date(timeStamp * 1000L)); } catch (Exception e) { e.printStackTrace(); } return result; } public static String getNotifDateSms(long timeStamp) { String result = ""; try { SimpleDateFormat sf1 = new SimpleDateFormat("hh:mm a"); result = sf1.format(new Date(timeStamp * 1000L)); } catch (Exception e) { e.printStackTrace(); } return result; }