簡體   English   中英

我想從 Java 日期中獲取年、月、日等,以與 Java 中的公歷日期進行比較。 這可能嗎?

[英]I want to get Year, Month, Day, etc from Java Date to compare with Gregorian Calendar date in Java. Is this possible?

我在 Java 中有一個 Date 對象存儲為 Java 的 Date 類型。

我還有一個公歷創建日期。 公歷日期沒有參數,因此是今天日期(和時間?)的一個實例。

使用 java 日期,我希望能夠從 java 日期類型中獲取年、月、日、小時、分鍾和秒,並比較 gregoriancalendar 日期。

我看到目前 Java 日期存儲為 long 並且唯一可用的方法似乎只是將 long 寫入格式化的日期字符串。 有沒有辦法訪問年、月、日等?

我看到Date類的getYear()getMonth()等方法已被棄用。 我想知道將 Java Date 實例與GregorianCalendar日期一起使用的最佳實踐是什么。

我的最終目標是進行日期計算,以便我可以檢查 Java 日期是否在今天日期和時間的幾小時、幾分鍾等范圍內。

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

使用類似的東西:

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 開始,最好使用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 的 int 值。

你可以做這樣的事情,它會解釋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());

編輯: date = Fri Jun 15 09:20:21 CEST 2018是:

DAY FRIDAY
MONTH JUNE
YEAR 2018
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);
}

可能會更容易

     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);
    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,然后使用 date.compare 進行比較

小心地從0而不是1開始安裝。日歷也像am一樣使用pm,以便在使用Hours和minutes時非常小心。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM