13

I have a date column in a Cassandra column family. When I retrieve data from this CF using datastax java API, this date object can be taken as a java.util.Date object.

It has a getYear() method but it is deprecated. The corresponding javadoc says:

As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

How can I get the year, month, day attributes from this date object properly?

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
Chamila Wijayarathna
  • 1,815
  • 5
  • 30
  • 54
  • 2
    If you're getting the value as a `java.util.Date`, what time zone do you want to use for the year? A `Date` is just a point in time. – Jon Skeet Dec 15 '14 at 17:58
  • possible duplicate of [Get integer value of the current year in Java](http://stackoverflow.com/questions/136419/get-integer-value-of-the-current-year-in-java) – Basil Bourque Dec 15 '14 at 18:06

4 Answers4

31

Could you try like tihs;

      // create a calendar
      Calendar cal = Calendar.getInstance();
      cal.setTime(datetime);  //use java.util.Date object as arguement
      // get the value of all the calendar date fields.
      System.out.println("Calendar's Year: " + cal.get(Calendar.YEAR));
      System.out.println("Calendar's Month: " + cal.get(Calendar.MONTH));
      System.out.println("Calendar's Day: " + cal.get(Calendar.DATE));

As mentioned in javadocs;

@Deprecated public int getYear() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900. Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone. Returns: the year represented by this date, minus 1900.

Semih Eker
  • 2,389
  • 1
  • 20
  • 29
  • 2
    You must set the date before getting the year: `cal.setTime(yourDateObjectFromCassandra);` then you can do `cal.get(Calendar.YEAR);` – Narmer Dec 15 '14 at 17:55
  • @Narmer: No, `Calendar.getInstance()` defaults to the current date/time. – Jon Skeet Dec 15 '14 at 17:56
  • 1
    @JonSkeet Yep, but the OP wants the year of the Cassandra object, not the current year – Narmer Dec 15 '14 at 17:57
  • @Narmer: Right, yes - for that case, I see your point. It's also worth noting that `Calendar` will default to the system time zone; it's not clear what time zone the OP wants. – Jon Skeet Dec 15 '14 at 17:58
  • 1
    You are right @Narmer, I answered acording to header of question. – Semih Eker Dec 15 '14 at 18:02
  • @SemihEker Thanks, still the locale issue isn't resolved. You should mention that you can initialize your locale through `Calendar.getInstance(Locale.YOUR_COUNTRY);`. I don't think its needed since he wants the year, but its right to mention. – Narmer Dec 15 '14 at 18:05
  • Oh, the same applies to time zone! – Narmer Dec 15 '14 at 18:06
  • This works fine, but for dates like 2014-01-05 00:08:00+0530, I get month as 0, what is the reason for this? – Chamila Wijayarathna Dec 15 '14 at 18:07
  • 2
    Because months start from 0. January=0, February=1 etc. – Semih Eker Dec 15 '14 at 18:08
  • 2
    @ChamilaWijayarathna [January is 0](http://stackoverflow.com/questions/344380/why-is-january-month-0-in-java-calendar) – Narmer Dec 15 '14 at 18:09
1

A good option is to use date format as follows:

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy");
Date             date = sdf1.parse(datetime);
String           year = sdf2.format(date);
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
1

use LocalDate object in java8

Date date = new Date();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int year  = localDate.getYear();
int month = localDate.getMonthValue();
int day   = localDate.getDayOfMonth();
Ammar Bozorgvar
  • 1,230
  • 19
  • 30
0

To retrieve the date & time fields you can use this code:

DateFormat.getDateTimeInstance().getCalendar().get(DateFormat.MONTH_FIELD)

Just replace MONTH_FIELD with one somehting else like "DAY_OF_WEEK_FIELD" to retrieve the day of the week (I think '1' stands for monday) or "MINUTE_FIELD" for the current minute, etc. :)

Cyphrags
  • 528
  • 1
  • 7
  • 16