Get Integer Value of the Current Year in Java

Get integer value of the current year in Java

For Java 8 onwards:

int year = Year.now().getValue();

For older version of Java:

int year = Calendar.getInstance().get(Calendar.YEAR);

How to get integer value of datetime in java?

Use a formatter.

    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMddHHmmssSSS");

DateTime calendar = new DateTime();
String formatted = calendar.toString(formatter);
Long numericValue = Long.parseLong(formatted);

System.out.println(numericValue);

Output when I ran the code in my time zone just now:

20200722210458862

Alternate way: Only if this is for a library method that I expect to be called often and where efficiency may be a concern, I might consider not formatting and parsing a string. The following gives the same result.

    long numericValue = calendar.getYear();
numericValue = numericValue * 100 + calendar.getMonthOfYear();
numericValue = numericValue * 100 + calendar.getDayOfMonth();
numericValue = numericValue * 100 + calendar.getHourOfDay();
numericValue = numericValue * 100 + calendar.getMinuteOfHour();
numericValue = numericValue * 100 + calendar.getSecondOfMinute();
numericValue = numericValue * 1000 + calendar.getMillisOfSecond();

Did your code work?

Your code probably formatted one-digit values into just one character in the string, so your string would typically be too short and miss some zeroes. For example:

Correct:        20200722210458862 (2020 07 22 21 04 58 862)
From your code: 202072221458862 (2020 7 22 21 4 58 862)

Setting current year value as integer

Not sure how you want to handle anything before 2015, but here you go:

int year = Calendar.getInstance().get(Calendar.YEAR) - 2014;

get present year value to string

You can simple get the year from Calendar instance using Calendar#get(int field) method:

Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
String yearInString = String.valueOf(year);

Convert Current date to integer

The issue is that an Integer is not large enough to store a current date, you need to use a Long.

The date is stored internally as the number of milliseconds since 1/1/1970.

The maximum Integer value is 2147483648, whereas the number of milliseconds since 1970 is currently in the order of 1345618537869

Putting the maximum integer value into a date yields Monday 26th January 1970.

Edit: Code to display division by 1000 as per comment below:

    int i = (int) (new Date().getTime()/1000);
System.out.println("Integer : " + i);
System.out.println("Long : "+ new Date().getTime());
System.out.println("Long date : " + new Date(new Date().getTime()));
System.out.println("Int Date : " + new Date(((long)i)*1000L));

Integer : 1345619256
Long : 1345619256308
Long date : Wed Aug 22 16:37:36 CST 2012
Int Date : Wed Aug 22 16:37:36 CST 2012

Setting current year value as integer

Not sure how you want to handle anything before 2015, but here you go:

int year = Calendar.getInstance().get(Calendar.YEAR) - 2014;

Getting current Year and Month resulting strange results

Just to give a bit more background:

Both new GregorianCalendar() and Calendar.getInstance() will correctly give a calendar initialized at the current date and time.

MONTH and YEAR are constants within the Calendar class. You should not use them "via" a reference which makes it look like they're part of the state of an object. It's an unfortunate part of the design of the Calendar class that to access the values of different fields, you need to call get with a field number, specified as one of those constants, as shown in other answers:

Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);

Note that the month numbers are 0-based, so at the time of this writing (in April) the month number will be 3.

It's an unfortunate part of the design of the Java language that you can reference static members (such as constants) via expressions of that type, rather than only through the type name.

My recommendations:

  • If your IDE allows it (as Eclipse does), make expressions such as c.YEAR give a compile-time error - you'll end up with much clearer code if you always use Calendar.YEAR.
  • Where possible, use Joda Time - a much better date/time library for Java. Admittedly on Android you may be a bit space-constrained, but if your app does a lot of date/time manipulation, it would save you a lot of headaches.

How to get last 5 years and next year in Java

You can get it this way I suppose:

Integer currentYear = Calendar.getInstance().get(Calendar.YEAR) + 1;
int[] yearsSpan = new int[7];
for (int i = 7; i > 0; i--) {
yearsSpan[7-i] = currentYear - (i-1);
}

To quickly display array contents:

System.out.println(Arrays.toString(yearsSpan));


Related Topics



Leave a reply



Submit