0

I am new to Android Studio and have ran into a problem - I am trying to carry out a calculation whereby I need the current date and time in an integer format. I also need the current date and time to then display in a TextBox.

I have declared the date as an integer as follows:

public static int date1 = (int) (new Date().getTime()/1000);


datedisplay = (TextView) findViewById(R.id.date);

Then I have tried to get the current date and time displayed in a textbox, but it isn't displaying. I was just wondering would anyone know why?

   SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy HH:mm");
   System.out.println(new Date(new Date(date1).getTime()));
   datedisplay.setText(dateFormat.format(date1));

Thank you in advance

Aastha Doshi
  • 147
  • 1
  • 14
Almac3
  • 63
  • 7
  • can you rephrase what exactly are you trying to do? For your information `SimpleDateFormat.format` takes as input a `Date` object not an integer you are trying to pass `date1` – pleft Nov 03 '17 at 11:02
  • I am trying to read in a date that needs to be stored as an integer# – Almac3 Nov 03 '17 at 11:04
  • 1
    Ok, so get the milliseconds of that `Date` object as `long`, not `int` as: `long millis = new Date().getTime();` and then use this `millis` variable as desired. If you change the type from `long` to `int` or perform any math operations on it like you do (divide by 1000) you totally destroy the date value (milliseconds) – pleft Nov 03 '17 at 11:06
  • so is this to declare the date at the start – Almac3 Nov 03 '17 at 11:09
  • is you taking epoch time? – Manish Jaiswal Nov 03 '17 at 11:11
  • So you get the date today (in milliseconds) `new Date().getTime()`, then you divide the number by 1000 and assign it to an `int` variable. This way you have just destroyed the Date, you made it smaller, it is no longer pointing to today. Why? – pleft Nov 03 '17 at 11:11
  • I don't know got if off someone on here – Almac3 Nov 03 '17 at 11:12
  • The answer from @AND suits you I think. – pleft Nov 03 '17 at 11:13
  • 1
    I recommend you consider [the modern Java date and time API known as JSR-310 or `java.time`](https://docs.oracle.com/javase/tutorial/datetime/) rather than the outdated `Date` and `SimpleDateFormat`. I know it’s not built-in on most Android phones yet, but you can get [**ThreeTenABP**](https://github.com/JakeWharton/ThreeTenABP) and start using it. It will be much more convenient, not least when it comes to getting date and time as integers. – Ole V.V. Nov 03 '17 at 11:44

4 Answers4

1

Use it in any datatype you want either String or Integer.

java.util.Date date= new java.util.Date();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date.getTime());
datedisplay.setText(timeStamp);
Aastha Doshi
  • 147
  • 1
  • 14
  • thank you, but how to I declare it as a int – Almac3 Nov 03 '17 at 11:09
  • You can use the typecast functionality and if you found my answer helpful then please upvote and marked it correct. – Aastha Doshi Nov 03 '17 at 11:11
  • Why to change its type from `long` to `int`? Is there a specific reason to do this? You might lose in precision doing such a change. – pleft Nov 03 '17 at 11:14
  • I need it to be an int to allow me to use the variable in another page, I'm just not sure how to declare the date as an int from the start – Almac3 Nov 03 '17 at 11:16
  • You should pass it as `long` not `int` you will lose precision, you get a totally different number as `int` than the real `long` one. – pleft Nov 03 '17 at 11:17
0

You can also use Calendar class:

Calendar myCalendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm",Locale.getDefault());
String dateString = sdf.format(myCalendar.getTime());

then

datedisplay.setText(dateString);

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
  • does this allow it to be stored as an integer to use as a variable in another page – Almac3 Nov 03 '17 at 11:13
  • The problem is that you can store a date in a long (64 bit), if you store a date in a int (32 bit) you will not have sufficient bits to represent the date. Use a long if you want to store a date in a primitive type – Nicola Gallazzi Nov 03 '17 at 11:18
0

new Date().getTime() ; return long value if you diving it by 1000

to convert long to int

better if you use long:

    long date1 = new Date().getTime() ;
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy HH:mm");
    Date dt = new Date(date1);
    datedisplay.setText(dateFormat.format(dt));
Manish Jaiswal
  • 442
  • 5
  • 19
0

If you really want to use a int to store your date representation, just use the number of day since the epoch instead of the number of seconds

new Date().getTime() 
    /1000 //second
    /60   //minute
    /60   //hour
    /24;   //day

This will give you a value that will fit in a int for quite some time.

But of course you won't be able to get back the exact precision, if you want the Time part of the Date, you won't be able to. This only allows you to get the Date like yyyy-MM-dd

private static final long EPOCH_DAY = 1000L * 60 * 60 * 24;
public static void main(String[] args){
    int i = (int) (new Date().getTime()/EPOCH_DAY);
    System.out.println(new Date(i * EPOCH_DAY));

}

Fri Nov 03 01:00:00 CET 2017

(the hours is because of my local being in GMT+1)

AxelH
  • 14,325
  • 2
  • 25
  • 55