Date issue when using New Date () android - android

I am writing to you to see if someone can guide me what is happening with this code ...
I have the following code:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
Date datofecha1 = new Date (time1);
String datotexto1 = formatter.format(datofecha1);
System.out.println(datotexto1);
the long of time1, I am fetching it from my cloud firebase database, it is an item number 1597081737457
That code should print me:
I/System.out: 10.08.2020, 13:48:57
but it prints me
I/System.out: 31.12.1969, 20:00:00
IF I change the long in my database, it gives me another date to print, it always prints December 31, 1969, a total madness (whatever the length I put on it).
As a curious fact if I put the long direct to the code in this way:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
Date datofecha1 = new Date (1597081737457L);
String datotexto1 = formatter.format(datofecha1);
System.out.println(datotexto1);
In the following way if the date prints me perfect
As a curious fact in the database, I can only put a number for the long, I can't put the final L, which must be put for the long, and I'm really stuck trying to print the date of the long used, and not that date of December 31 that always puts me.

Data stored in a Firebase is retrieved by attaching an asynchronous listener. You have to use value inside listener or ensure the listener is triggered before

Related

How to get 2 date objects holding the current time, one in GMT and the other in local

I am trying to fill 2 date objects, one in Local time and the other in UTC.
I AM NOT TRYING TO PRINT THE DATE AS A STRING IN GMT/UTC, please do not suggest DateFormatting, and dont say its a duplicate until you read the full question.
Local, I have no problem:
Date dateLocal = new Date();
The problem is I cant get the utcDate to be UTC.
Using a Calendar like so:
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
Calendar c = new GregorianCalendar();
c.setTime(new Date());
c.setTimeZone(TimeZone.getTimeZone(utcTimeZone.getID()));
Date utcDate = c.getTime();
When debugged or submitted to the webservice, utcDate shows in my local timezone, instead of UTC.
Using Joda:
DateTime utcDateTime = DateTime.now(DateTimeZone.UTC);
Date utcDate = utcDateTime.toDate();
Same issue, utcDate when debugged/submitted to webservice is showing in local time.
Here is how the object looks when debugged:
This is an issue because this causes the webservice (which i have no access to) to think this time is UTC, so when it does its work and conversions, the time is always off by 4 hours, since for me the UTC to Local conversion is GMT -4.
The ONLY way i have been able to get this to submit the date in UTC time is by adding:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
BUT this also changes the LocalTime object, even though this object was defined and set before the default TimeZone was changed.
So i get it, the Date() object uses the JVM locale, so any time a Date is created, its created in the default timezone, and apparently whenever the default timezone is changed, all of the Date objects (even if they are already created) change to the new default timezone... I know Date objects are just the millis between now and 1970 whatever, but the TimeZone is obviously being taken into account in the Webservice and this is messing up my results...how can i get the dates the way i want?

How to convert firebase timestamp into date and time as Date is deprecated

How do i convert ServerValue.TIMESTAMP into SimpleDateFormat("dd MM yyyy")
Date is deprecated so not able to use it , is their is any way to use calender
You cannot use Server.TIMESTAMP to get a date. The doc says:
A placeholder value for auto-populating the current timestamp (time since the Unix epoch, in milliseconds) as determined by the Firebase servers
This means that when you setValue() or updateChildren(), you can put this constant in the Map to tell the server to put the epoch time in that node instead. For example:
mRef = FirebaseDatabase.getInstance().child("whatever/path/in/your/database");
mRef.setValue( Server.TIMESTAMP );
This will set in <your Firebase>/whatever/path/in/your/database a long that will look like 149141530600. This is the current epoch time I fetched while writing this answer. It corresponds to the number of milliseconds passed since january 1st 1970 to when I copied the value. Then, if you have a listener to that node, you can get the calendar using:
Long time = dataSnapshot.getValue(Long.class);
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTimeInMillis(time);
If you only want the time the server is set to (saving it in the database is pointless), you can use the special node:
`FirebaseDatabase.getInstance().getReference(".info/serverTimeOffset");`
A listener to this node returns a Double that represents an approximative offset between the device time and the server time. You can then set the Calendar using:
calendar.setTimeInMillis(System.currentTimeMillis() + offset);
You can use the following.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(ServerValue.TIMESTAMP);
SimpleDateFormat fmt = new SimpleDateFormat("dd MM yyyy",Locale.US);
fmt.format(cal.getTime()); //This returns a string formatted in the above way.
If ServerValue.TIMESTAMP is returned as a string, you can parse the string using Long.parseLong(Server.TIMESTAMP);

Get Firebase Timestamp

I want to know how can I get the value of the timeStamp from the ServerValue.TIMESTAMP, directly from the HashMap? I know that when that value is sent to Firebase I get the long value, but I want to get that long before send it to Firebase.
Okie so after some research I found that we cannot use ServerValue.TIMESTAMP before sending it to database because
ServerValue.TIMESTAMP is set as a Map (containing {.sv: "timestamp"}) which tells Firebase to populate that field with the server's time. When that data is read back, it is the actual unix time stamp which is a Long.
Link
By the way you can use android own timestamp and store it in Firebase.
Calendar cc = Calendar.getInstance();
Date date = cc.getTime();
// SimpleDateFormat format1 = new SimpleDateFormat("dd MMM");
SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
timestamp = format2.format(date);

Android UTC Time Issue (I get incorrect time)

I want to subtract two dates in Android project.
When I use the statement:
DateTime now = new DateTime(DateTimeZone.UTC);
It gives 2014-03-17T12:49:06.670Z value instead of 2014-03-17T14:49:06.670Z (this is my current time on Android device)
When I convert this DateTime (2014-03-17T12:30:08.673+02:00) to UTC Time (2014-03-17T10:30:08.673Z) it gives the correct result but not for DateTime now = new DateTime(DateTimeZone.UTC);
What is wrong with new DateTime(DateTimeZone.UTC);?
Any help would be appreciated.
Try getting your local time this way:
DateTime now = new DateTime();
This time is "your current time on Android device". More precisely DateTime calculates its fields with respect to a time zone. It means it will return UTC+2 if you are located in Eastern European Countries (Winter Time) or Wester European Countries (Summer time).
On my PC nothing is wrong with new DateTime(DateTimeZone.UTC). The type DateTime internally stores the given time zone (here UTC) and uses it for the output of its method toString(), hence the UTC-string-format and not another alternative format with offset = +02:00.

Trouble converting back from int to DateTime

I am returning data from a sqlite3 database the some else wrote. I am trying to retrieve the date and time. To store it into the db I converted the date and time into one int. using this line
int currentTime=(int) ((newTime).toMillis(true) / 1000);
I am able to retrieve the data as an int, but cannot figure out how to convert the number back into a
date and time. Currently the db returns int 13333380180, I am trying to convert it to today's date and time.
long yourmilliseconds = 13333380180;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);
System.out.println(sdf.format(resultdate));
Use the above code to convert
The number you're receiving is the number of milliseconds that have passed since January 1st, 1970. It's a standard, consistent way of representing a date & time on a number of platforms.
Although the value is an integer in SQLite, SQLite integers can be up to 64 bits. So, the value being returned to you should be treated as a long in Java. You can convert it to a Java Date object in a straightforward fashion: new Date(currentTime).

Categories

Resources