0

So I am having trouble assigning multiple variables to one variable.

I have a date picker and time picker in my application and the values that the user picks are stored as separate variables. I then want to take all of the variables and assign them to one variable so I can store the users input and use it publicly in other activities.

EDIT: so I am working with a third party library that uses animated graphs and to generate the graph I must pass through integers. So after trying out all your suggestions this it is working using the Gregorian way:

timeselected = new GregorianCalendar(yearNow, monthNow, dayNow, hourNow, minuteNow).getTime();

Only thing now is this is stored as a date and cannot be cast to an integer??

For example, here is what I have tried so far:

The variables that I use to store the picker selection are:

 int dayNow, monthNow, yearNow, hourNow, minuteNow;

I have tried to assign them to one variable like so:

timeselected = (yearNow) + (monthNow) + (dayNow) + (hourNow) + (minuteNow);

Although this is giving me a total of all the variables added up. e.g. year(2017) plus month(11) etc.

Does anyone know how I can get this to store the selected data (which should be a date format) as an Integer?

Thanks

TheAlmac2
  • 193
  • 2
  • 15
  • 1
    This past answer might help. But you can use the java.time framework and bring in your values: https://stackoverflow.com/questions/16499228/creating-java-date-object-from-year-month-day – Dean Nov 03 '17 at 14:20
  • 2
    You can convert it to timestamp See: https://stackoverflow.com/questions/16777317/convert-java-util-date-default-format-to-timestamp-in-java – nhoxbypass Nov 03 '17 at 14:20
  • 1
    Because all of the variables are int, they getting added as integer. Please use this it should solve ur problem. Timeselected = “” + yearNow + monthNow+ ... – Anil Nov 03 '17 at 14:21
  • just convert to timestamp using Calendar object – Gokhan Celikkaya Nov 03 '17 at 14:22
  • Whats the prob dude ? Just Use [Calender](https://developer.android.com/reference/java/util/Calendar.html) class . – ADM Nov 03 '17 at 14:23
  • @NabinBhandari this is giving me a red line and wants me to change it to type string. Could anyone provide me code on how to convert to timestamp or to use calendar class? – TheAlmac2 Nov 03 '17 at 14:25
  • Can you use `long` instead of `int` in that library? Also provide sample input and expected output for your question. – Nabin Bhandari Nov 03 '17 at 15:39

3 Answers3

1

This is difficult to describe in a comment, but I think this is what you are looking for (I assume you are looking for the format yyyymmddhhmm):

Take the suggestion from Nabin Bhandari:

String sTimeselected = ""+(yearNow) + (monthNow) + (dayNow) + (hourNow) + (minuteNow);

And simply convert to long with:

long timeselected = Long.parseLong(sTimeselected);

If this isn't it than I am still unclear as to what you need.

EDIT:: For any integer values which you need to have at least 2 digits (eg. 2 => 02) then you can format the int to a string like this:

String sDay = String.format("%02d", dayNow);
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
Barns
  • 4,850
  • 3
  • 17
  • 31
  • It seems to be all fine. although my app is crashing with this error: java.lang.NumberFormatException: For input string: "20171131524" – TheAlmac2 Nov 03 '17 at 15:25
  • okay ill try that, do I have to do that for each variable? year month etc – TheAlmac2 Nov 03 '17 at 15:41
  • 1
    Integer cannot hold 12 digits. – Nabin Bhandari Nov 03 '17 at 15:41
  • @NabinBhandari :: Correct I have edited my answer. That is what happens when I don't 'debug' my own solution! `int` allows a maximum value of 2147483647 -- a factor of 10 less then 20171131524. – Barns Nov 03 '17 at 15:43
  • thanks guys this is working now just the format is still as a long number. my library also accepts a long! – TheAlmac2 Nov 03 '17 at 15:53
0

Try this:

int dayNow=3, monthNow=11, yearNow=2017, hourNow=9, minuteNow=20;
Date timeselected = new GregorianCalendar(yearNow, monthNow-1, dayNow, hourNow, minuteNow).getTime();
System.out.println(timeselected );
Vijaya Pandey
  • 4,252
  • 5
  • 32
  • 57
  • I do not want to hardcode the values. the variables are dependent on what the user selects from the date and time pickers – TheAlmac2 Nov 03 '17 at 14:27
  • 2
    This answer is not meant to be a direct copy/paste into your project - it just shows how to take ints and get a full date. You need to modify to fit your needs. – takendarkk Nov 03 '17 at 14:27
  • @TheAlmac2 Instead of hard coded values, assign user input value to dayNow, monthNow etc. – Vijaya Pandey Nov 03 '17 at 14:30
  • tried this method. although I am passing this as an integer through a calculation and therefore it must be an integer. this method sets it as a date which Is not accepted by the calculation method – TheAlmac2 Nov 03 '17 at 14:32
  • @TheAlmac2 :: You did not mention any calculations in your OP. You only stated "store the users input and use it publicly in other activities" . Perhaps you should edit your OP and explain more clearly what you will be doing with the "date" you wish to generate. – Barns Nov 03 '17 at 14:50
0

Use epoch counter.

First, a bit of history: if you try to print the value of a

System.getCurrentMillis();

call, you'll notice that will print something like 1509719045. These are milliseconds from Jan 01 1970, date where this count has started.

To answer your question, i would read this StackOverflow answer.

Now you not only have your single integer variable which stores your entire date and time, but you can even convert it back to Date or Calendar easily.