2012-02-27 66 views
150

我有一个Java日期对象存储为Java的日期类型。我想从Java Date中获得Year,Month,Day等,以便与Java中的Gregorian Calendar日期进行比较。这可能吗?

我也有公历日历创建日期。公历日期没有参数,因此是当今日期(和时间?)的实例。

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

我看到,此刻Java日期被存储为一个很长的时间,唯一可用的方法似乎只是将长整型作为格式化日期字符串来编写。有没有办法访问年,月,日等?

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

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

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

+0

嘿不管你使用不使用'日期。getYear()'。它有问题(我不知道).'Date.getYear()'一次解析我的日期30/06/2017,它返回我的一年117。看看它降落在哪里,两千年背部。但是,当我只打印日期对象,输出很好。但不是'Date.getYear();'。 – Seenivasan 2017-06-30 10:26:23

+0

仅供参考:您正在使用现在已有的麻烦旧日期时间类,由现代java.time类取代。 – 2017-10-25 15:12:16

回答

393

使用类似:

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。请参阅this answer了解如何操作。

+0

非常感谢!这正是我想要实现的。我正在处理从0到11个月的月份。如果我想检查Java日期是否在今天的48小时内,最好是从我的公历日期实例中减去48小时? – daveb 2012-02-27 23:51:05

+4

您可以使用'cal.add(Calendar.DAY_OF_MONTH,-48)'在日历对象上执行日算法。您可以使用'cal.compareTo(anotherCal)'比较两个日历对象。 – 2012-02-27 23:55:56

+1

辉煌,欢呼Florent!以上是否意味着Calender会在使用日历get方法时返回更新的年,日,秒,分和秒? Dave – daveb 2012-02-28 00:02:10

9

或者你可以做这样的事情。它将解释“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"); 
      } 
+0

嗨JavaCity,谢谢你。这非常有用。此刻,我试图回避从日期字符串解析。但是,当我将应用程序的用户设置为年,日和月时,我将需要稍后进行解析。我打算建立一个字符串来解析SimpleDateFormat。以上内容对于实际操作非常有用。我的Java日期是从一天前的实例化中创建的日期和时间。我现在正在寻找与今天实例化的gregoriancalendar日期进行比较。真诚的感谢 – daveb 2012-02-27 23:57:50

+7

请修正代码......小写'mm'表示分钟,大写'MM'表示月份。 – YoYo 2016-05-17 02:40:37

4
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()); 
3
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); 
} 
30

利用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 int值返回到12

+3

现在是2016年,我相信在Java 8中使用'LocalDate'是节省时间的最佳解决方案,使用'Calendar'和'Date'的另一个解决方案充满了麻烦。 – 2016-07-06 11:28:08

+0

伟大的答案 - 绝对最好使用* java.time *并避免麻烦的旧日期时间类。此外,请注意本答案如何明智地解决时区问题。对于任何特定时刻,日期因时区而异。 – 2016-07-13 01:27:36

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

0

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

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

+0

更好地使用替代这里显示的这些麻烦的旧旧类的现代java.time。请参阅[正确答案](https://stackoverflow.com/a/32363174/642706) – 2017-07-21 18:05:27

+0

谢谢您的回答和建议。这是非常好的。 – 2017-07-22 13:41:02

0

可能更容易

 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); 
相关问题