-1

The following program Generate Unique key by using Date and Time(using joda time API)

import org.apache.commons.codec.binary.Base64;
import org.joda.time.*;

public class EnDecoding {


    public String EncodeRecieverAddress(String emailaddress){
        byte[] encodedBytes = Base64.encodeBase64(emailaddress.getBytes());
        return new String(encodedBytes);
    }

    public String DecodeRecieverAddress(String encodedemail){
        byte[] decodedBytes = Base64.decodeBase64(encodedemail.getBytes());
        return new String(decodedBytes);
    }

    public int GenerateUniquekey() {
        LocalTime localtime = new LocalTime();
        LocalDate localdate = new LocalDate();
        String  key = "" + localdate.getDayOfYear()   
                    + localdate.getDayOfMonth()
                    + localdate.getDayOfWeek()
                    + localtime.getHourOfDay()
                    + localtime.getMinuteOfHour()
                    + localtime.getSecondOfMinute()
                    + localtime.getMillisOfSecond();
        System.out.println(key);
        System.out.println(Integer.parseInt(key.trim()));
      return 0;
    }
}

System.out.println(key);

Output : 117275232750437

System.out.println(Integer.parseInt(key.trim()));

java.lang.NumberFormatException: For input string: "117275232750437"

I have used id.trim() function to eliminates leading and trailing spaces , but that does not solve my problem too.

Please dont mark this question duplicate as because other similar kind of questions didnt help me much that's why i have created this new question and so I hope to get the best answer over here.

  • It's too large to be stored as an Integer – Compass Apr 27 '18 at 18:50
  • but it also gave me same error when i stored it on long datatype variable – burhanuddin abbas Apr 27 '18 at 18:52
  • 1
    Worked fine for me with Long. – Compass Apr 27 '18 at 18:57
  • It’s quite a peculiar way to generate a supposedly unique key. For something simpler and more standard I suggest [`UUID`](https://docs.oracle.com/javase/9/docs/api/java/util/UUID.html) or [`System.currentTimeMillis()`](https://docs.oracle.com/javase/9/docs/api/java/lang/System.html#currentTimeMillis--) or just an ever incrementing counter. – Ole V.V. Apr 28 '18 at 07:40
  • It’s not a question of *storing* it in a `long`, @burhanuddinabbas. You need to use `Long.parseLong()` too. – Ole V.V. Apr 28 '18 at 07:44
  • How unique are your keys going to be? As far as I can tell, Monday Jan 1, 2018, 02:16:19.500, and Saturday Apr 21, 2018, 19:05:00.000 will both give the key 11121619500. There will be many more clashes. – Ole V.V. Apr 28 '18 at 07:57

3 Answers3

2

Use Long instead of Integer as it is out of range of Integer

  String s = "117275232750437";
  System.out.println(Long.parseLong(s));
  • Integer range : -2,147,483,648 to 2,147,483,647
  • Long range : -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Amit Bera
  • 7,075
  • 1
  • 19
  • 42
1

Maximum value of integer is 2,147,483,647, your input is too large. Use Long instead.

Ondra K.
  • 2,767
  • 4
  • 23
  • 39
  • i have already tried Long datatype , it gave me same exception – burhanuddin abbas Apr 27 '18 at 18:55
  • 1
    `Long.parseLong("117275232750437")` seems to be working just fine for me. Are you sure this is the exact content of the string you're trying to parse? – Ondra K. Apr 27 '18 at 18:57
  • Thanks , my problem is solved now . By the way , does the range of datatypes depends on processor architectures like 32 bit or 64 bit ? – burhanuddin abbas Apr 27 '18 at 19:06
  • Great, glad your problem is resolved, please mark an answer if it did help you, so that others see that this question is answered. And concerning processor architecture - the data ranges are the same for primitives on both 32 and 64 bit, some interesting thoughts are made on atomicity of the operations here: https://stackoverflow.com/q/9511836/5362510 – Ondra K. Apr 27 '18 at 19:10
  • A Java `int` is 32 bits no matter the processor it is running on. Similarly a `long` is always 64 bits independently of the underlying processor. – Ole V.V. Apr 28 '18 at 08:02
0

Your number is too much large, you should use a Long.
Maximum value of int are 2.147.483.647.

Gabriel
  • 464
  • 4
  • 17