我有一个日期对象在Java存储为Java的日期类型。

我还创建了一个公历日期。公历日期没有参数,因此是今天日期(和时间?)的一个实例。

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

我看到,目前Java日期存储为长,唯一可用的方法似乎只是将长写为格式化的日期字符串。是否有方法访问年、月、日等?

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

我的最终目标是执行一个日期计算,这样我就可以检查Java日期是否与今天的日期和时间相差那么多小时、分钟等。

我对Java还是个新手,对此有点困惑。


当前回答

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);
}

其他回答

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);
}

在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值。

    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!";
 }
@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,然后使用date.compare进行比较

你可以这样做,它会解释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");
}