Unique int from Date

This is general programming, but I work in Java.

Given a date dd-mm-yyor dd-mm-yyyy(e.g. 13-01-2011), I want to convert it to a unique number, so any two dates have a different number. And the year is not important. Thus, the conversion of dd-mm to one unique int is permissible. Does anyone know an algorithm for this?

I'm SOrry: I want to be more specific:

Unique numbers must be from 1 to 365 (or from 0 to 364) or must be unambiguously modulo 365. (I ignore the case of leap years at the moment).

Thus, the union "ddmm" can be a unique 4-digit number. But modulo 365 would probably not be unique.

+3
source share
6 answers

, ? , " int ".

Calendar#get(Calendar.DAY_OF_YEAR) ( 1).

Date date = getItSomehow();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);

, /- .

+3

.

+1
new Date().getTime()

1970 . getTime()

  DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
  Date date = formatter.format( myInput );
0
0

: 1-364. Calendar get (Calendar.DAY_OF_YEAR) - , , OP .

: , . , , , , , - , , . Java Date Calendar. , - int v = (month * 120) + day. , , - . Java , , String hashCode() .

0
source

If you only need a unique key, Calendar hashCode () should do the trick.
Alternatively, you can combine the two digits of the month and day and the four-digit year.

0
source

Source: https://habr.com/ru/post/1784999/


All Articles