-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.

6
  • It's too large to be stored as an Integer Commented Apr 27, 2018 at 18:50
  • but it also gave me same error when i stored it on long datatype variable Commented Apr 27, 2018 at 18:52
  • 1
    Worked fine for me with Long. Commented Apr 27, 2018 at 18:57
  • It’s quite a peculiar way to generate a supposedly unique key. For something simpler and more standard I suggest UUID or System.currentTimeMillis() or just an ever incrementing counter. Commented Apr 28, 2018 at 7:40
  • It’s not a question of storing it in a long, @burhanuddinabbas. You need to use Long.parseLong() too. Commented Apr 28, 2018 at 7:44

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Great!!! Happy to help you.
1

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

5 Comments

i have already tried Long datatype , it gave me same exception
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?
Thanks , my problem is solved now . By the way , does the range of datatypes depends on processor architectures like 32 bit or 64 bit ?
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: stackoverflow.com/q/9511836/5362510
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.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.