我想从Java Date中获得Year,Month,Day等,以便与Java中的Gregorian Calendardate进行比较。 这可能吗?

我有Java中的date对象存储为Java的datetypes。

我也有一个公历日历创builddate。 公历date没有参数,因此是今天的date(和时间?)的一个实例。

使用javadate,我希望能够从javadatetypes中获得年,月,日,小时,分钟和秒数,并比较gregoriancalendardate。

我看到,目前Javadate被存储为一个很长的时间,唯一可用的方法似乎只是将长写作格式化的datestring。 有没有办法访问年,月,日等?

我看到, Date类的getYear()getMonth()等方法已被弃用。 我想知道在GregorianCalendardate中使用Java Date实例的最佳做法是什么。

我的最终目标是做一个date计算,以便我可以检查Javadate是在今天的date和时间的许多小时,分钟等。

我仍然是Java的新手,并对此感到有点困惑。

使用类似于:

  Date date; // your date Calendar cal = Calendar.getInstance(); cal.setTime(date); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); // etc. 

请注意,月份从0开始,而不是1。

编辑 :从Java 8开始,最好使用java.time.LocalDate而不是java.util.Calendar 。 看到这个答案如何做到这一点。

使用Java 8和更高版本,可以将Date对象转换为LocalDate对象,然后轻松获取年,月和日。

 Date date = new Date(); LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); int year = localDate.getYear(); int month = localDate.getMonthValue(); int day = localDate.getDayOfMonth(); 

请注意, getMonthValue()返回从1到12的整数值。

或者你可以做这样的事情。 它将解释“Date”类如何工作。

  String currentDateString = "02/27/2012 17:00:00"; SimpleDateFormat sd = new SimpleDateFormat("mm/dd/yyyy HH:mm:ss"); Date currentDate = sd.parse(currentDateString); String yourDateString = "02/28/2012 15:00:00"; SimpleDateFormat yourDateFormat = new SimpleDateFormat( "mm/dd/yyyy HH:mm:ss"); Date yourDate = yourDateFormat.parse(yourDateString); if (yourDate.after(currentDate)) { System.out.println("After"); } else if(yourDate.equals(currentDate){ System.out.println("Same"); }else{ System.out.println("Before"); } 
  Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE"); System.out.println("DAY "+simpleDateFormat.format(date).toUpperCase()); simpleDateFormat = new SimpleDateFormat("MMMM"); System.out.println("MONTH "+simpleDateFormat.format(date).toUpperCase()); simpleDateFormat = new SimpleDateFormat("YYYY"); System.out.println("YEAR "+simpleDateFormat.format(date).toUpperCase()); 
 private boolean isSameDay(Date date1, Date date2) { Calendar calendar1 = Calendar.getInstance(); calendar1.setTime(date1); Calendar calendar2 = Calendar.getInstance(); calendar2.setTime(date2); boolean sameYear = calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); boolean sameMonth = calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH); boolean sameDay = calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH); return (sameDay && sameMonth && sameYear); } 
 @Test public void testDate() throws ParseException { long start = System.currentTimeMillis(); long round = 100000l; for (int i = 0; i < round; i++) { StringUtil.getYearMonthDay(new Date()); } long mid = System.currentTimeMillis(); for (int i = 0; i < round; i++) { StringUtil.getYearMonthDay2(new Date()); } long end = System.currentTimeMillis(); System.out.println(mid - start); System.out.println(end - mid); } public static Date getYearMonthDay(Date date) throws ParseException { SimpleDateFormat f = new SimpleDateFormat("yyyyyMMdd"); String dateStr = f.format(date); return f.parse(dateStr); } public static Date getYearMonthDay2(Date date) throws ParseException { Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.SECOND, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.HOUR_OF_DAY, 0); return c.getTime(); } public static int compare(Date today, Date future, Date past) { Date today1 = StringUtil.getYearMonthDay2(today); Date future1 = StringUtil.getYearMonthDay2(future); Date past1 = StringUtil.getYearMonthDay2(past); return today.compare // or today.after or today.before } 

getYearMonthDay2(日历解决scheme)速度快十倍。 现在你有yyyy MM dd 00 00 00,然后使用date.compare进行比较

仔细安装从0开始不是1.而且日历使用像上午一样,所以使用小时和分钟时要非常小心。

 Date your_date; Calendar cal = Calendar.getInstance(); cal.setTime(your_date); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MINUTE);