This question already has answers here:
Convert UTC Epoch to local date
(16 answers)
Closed 6 years ago.
I am creating a chat application and I want to print the date and time in chat bubble whenever the message is sent or received.
I used the below code to get and convert the date and time.
long itemLong = (long) (chatMessage.getTime()*1000);
Date itemDate = new Date(itemLong);
String itemDateStr = new SimpleDateFormat("dd-MMM HH:MM").format(itemLong);
holder.time.setText(itemDateStr);
The itemLong variable gets the value 1.4847986E15, which is converted to date and timestamp in the format I mentioned in SimpleDateFormat, but eveytime some random date and time gets displayed not at which the message was sent or received.
I tried various solutions but I am unable to get the correct date and time.
Any help is appreciated.
Edited the code to this and its working fine.
long itemLong = (long) (chatMessage.getTime()/1000);
java.util.Date d = new java.util.Date(itemLong*1000L);
String itemDateStr = new SimpleDateFormat("dd-MMM HH:mm").format(d);
holder.time.setText(itemDateStr);
Related
This question already has answers here:
Which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()
(4 answers)
JSR 310 :: System.currentTimeMillis() vs Instant.toEpochMilli() :: TimeZone
(3 answers)
Closed 3 years ago.
I want to understand this in detail on how to get this in android and which method to follow and please explain bit more to understand in better way ?
As we have some options to get this in android and find out the best.
It will be helpful if somebody explains with code how to get this.
Thanks in advance for your help
Hi I hope this will help you
//Getting the current date
Date date = new Date();
//This method returns the time in millis
long timeMilli = date.getTime();
System.out.println("Time in milliseconds using Date class: " + timeMilli);
//creating Calendar instance
Calendar calendar = Calendar.getInstance();
//Returns current time in millis
long timeMilli2 = calendar.getTimeInMillis();
System.out.println("Time in milliseconds using Calendar: " + timeMilli2);
//Java 8 - toEpochMilli() method of ZonedDateTime
System.out.println("Getting time in milliseconds in Java 8: " +
ZonedDateTime.now().toInstant().toEpochMilli());
And output fo these options will be
Time in milliseconds using Date class: 1508484583259
Time in milliseconds using Calendar: 1508484583267
Getting time in milliseconds in Java 8: 1508484583331
if we convert those long values to the date format then all three will be the same and it will be
Input 1508484583259
Input (formatted) 1,508,484,583,259
Date (Etc/UTC) Friday, October 20, 2017 7:29:43 AM UTC
Date (GMT) Friday, October 20, 2017 7:29:43 AM GMT
Date (short/short format) 10/20/17 7:29 AM
Over here I posted only one option result but all three will be the same or you can also check it by your own on online long to date convertor.
For getting timestamp in millisecond just call:
//kotlin
val tstamp = System.currentTimeMillis()
//java
long tstamp = System.currentTimeMillis();
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);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to show current time of device in app. following is the code is getting time but problem is if current time is 12:15 but my variable cTime has opposite time that is 00:15. Below is my code for getting time, please help me out
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Calendar c = Calendar.getInstance();
int currentHour = c.get(Calendar.HOUR);
int currentMin = c.get(Calendar.MINUTE);
String currentTime = currentHour+":"+currentMin;
Date cTime = sdf.parse(currentTime);
You should use Calendar.HOUR_OF_DAY instead of Calendar.HOUR to get the hour in 0-24 format.
Try this Code For current time
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm aa");
String datetime = sdf.format(now);
Use this simple method for get device current time in milisecound
System.currentTimeMillis();
Always, always read the documentation for the code you're using.
You are formatting your hours with the H pattern, which describes the 24 hour model:
H | Hour in day | (0-23) | Number | 0
You need to use the small h, which represents the “Hour in am/pm (1-12)”:
SimpleDateFormat format = new SimpleDateFormat("hh:mm");
Date date = new Date();
String dateReadable = format.format(date);
// Use your String elsewhere.
And this is all you need to get started. No need to create ints and Calendars for this. You can also put the a for the period indicator, as Arjun said below.
This is the foundation for this answer, too, but considering the code is slightly different and you are facing difficulties (by looking at your code), I didn't consider it a duplicate.
This question already has answers here:
Convert timestamp in milliseconds to string formatted time in Java
(10 answers)
Closed 9 years ago.
I am getting time in milliseconds ("1369807669") format from JSON and i have to change that in "yyyy-MM-dd'T'HH:mm:ssS" format ... How can i do it??
I used this code but i am getting different time not the actual time.
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
DateTime = df.format(millis);
But i am getting time as "1970-12-23'T'12:54:32+0000" which is wrong...
I should get time something like this "2013-05-29'T'12:06:53+0000"
you have seconds not milliseconds you should multipy your seconds * 1000
check this for your current date and check with your json milliseconds also
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String currentDate = formatter.format(new Date(System
.currentTimeMillis())));
may be you are getting wrong time in json
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate date/time difference in java
I am providing the user with the option to select the date using Date Picker. Is there any in-built method using which I can calculate the duration in days wrt to user selected date and todays date.
I don't like answering this because there were millions of questions like this (use search option before posting questions). Use Joda Time. There is a Period class, which will be useful for you.
Get the difference between the two times in milliseconds. Than you can get the Days via Java's Calendar class.
Date today = new Date(); // the date of today
Date target = new Date(); // the date of when the user picks
long todayEpoch = today.getTime(); // or can use = System.currentTimeMillis();
long targetEpoch = target.getTime();
long daysInMs = targetEpoch - todayEpoch; //days in MS's
//the # of days
float days = (daysInMs/1000/60/60/12);