I want to get Year, Month, Day, etc from Java Date to compare with Gregorian Calendar date in Java. Is this possible?
Java中的日期对象存储为Java的日期类型。
我也有公历创建日期。公历日期没有参数,因此是今天日期(和时间?).
使用Java日期,我希望能够从Java日期类型获得年、月、日、小时、分钟和秒,并比较GRGGORICANALDEAR日期。
我看到目前Java数据存储的时间很长,唯一可用的方法似乎是把长时间写为格式化日期字符串。有没有方法访问年、月、日等?
我看到
我的最终目标是做一个日期计算,这样我就可以检查Java日期在今天的日期和时间这么长的时间,分钟等等。
我仍然是Java新手,对此我有点困惑。
使用如下内容:
1 2 3 4 5 6 7 8 | Date date; // your date // Choose time zone in which you want to interpret your Date Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris")); 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,最好使用JavaTime.LoalDealt,而不是JavaUTL.Calase.查看此答案了解如何执行此操作。
使用Java 8和以后,可以将日期对象转换为LoalDead对象,然后轻松获取年、月和日。
1 2 3 4 5 |
注意,
您可以这样做,它将解释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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"); } |
1 2 3 4 5 6 7 8 9 10 11 | 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()); |
编辑:
1 2 3 | DAY FRIDAY MONTH JUNE YEAR 2018 |
1 2 3 4 5 6 7 8 9 10 | 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); } |
可能会更容易些
1 2 3 4 5 6 7 8 9 10 11 | Date date1 = new Date("31-May-2017"); OR java.sql.Date date1 = new java.sql.Date((new Date()).getTime()); SimpleDateFormat formatNowDay = new SimpleDateFormat("dd"); SimpleDateFormat formatNowMonth = new SimpleDateFormat("MM"); SimpleDateFormat formatNowYear = new SimpleDateFormat("YYYY"); String currentDay = formatNowDay.format(date1); String currentMonth = formatNowMonth.format(date1); String currentYear = formatNowYear.format(date1); |
1 2 3 4 5 6 7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | @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(日历解决方案)速度快十倍。现在您有了yyyy-mm-dd 00 00 00 00,然后使用日期进行比较。比较
小心安装,从0开始而不是1。而且日历也像AM一样使用PM,所以在使用小时和分钟时要非常小心。
1 2 3 4 5 |