Home Articles

我希望从Java Date获得年,月,日等,以便与Java中的公历日历进行比较 . 这可能吗?

Asked
Viewed 1690 times
174

我在Java中有一个Date对象存储为Java的Date类型 .

我还有一个格里高利历创建日期 . 格里历日历日期没有参数,因此是今天日期(和时间?)的实例 .

使用java日期,我希望能够从java日期类型中获取年,月,日,小时,分钟和秒,并比较gregoriancalendar日期 .

我看到目前Java日期存储为long,并且唯一可用的方法似乎只是将long写为格式化的日期字符串 . 有没有办法访问年,月,日等?

我看到 Date 类的 getYear()getMonth() 等方法已被弃用 . 我想知道在 GregorianCalendar 日期使用Java Date实例的最佳做法是什么 .

我的最终目标是进行日期计算,以便我可以检查Java日期是否在今天的日期和时间的这么多小时,分钟等之内 .

我仍然是Java的新手,我对此感到有些困惑 .

9 Answers

  • 456
    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());
    

    编辑: date = Fri Jun 15 09:20:21 CEST 2018 的输出是:

    DAY FRIDAY
    MONTH JUNE
    YEAR 2018
    
  • 44

    你可以做这样的事情,它将解释 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");
    }
    
  • 9
    Date queueDate = new SimpleDateFormat("yyyy-MM-dd").parse(inputDtStr);
        Calendar queueDateCal = Calendar.getInstance();
        queueDateCal.setTime(queueDate);
        if(queueDateCal.get(Calendar.DAY_OF_YEAR)==Calendar.getInstance().get(Calendar.DAY_OF_YEAR))
    {
        "same day of the year!";
     }
    
  • 6
    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);
    }
    
  • 3

    使用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之间的int值 .

  • 2

    这可能更容易

    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

    使用类似的东西:

    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 .

    Edit :从Java 8开始,最好使用java.time.LocalDate而不是java.util.Calendar . 有关如何操作,请参阅this answer .

  • 1

    小心安装从0开始而不是1.并且日历使用pm就像我在使用小时和分钟时非常小心 .

    Date your_date;
    Calendar cal = Calendar.getInstance(); 
    cal.setTime(your_date);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MINUTE);
    
  • -1
    @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,然后使用date.compare进行比较

Related