How to convert date into firebase timestamp format in android
My date is - 01-01-2021
Result Format I need is - January 01, 2021 at 3:37:59 PM UTC+5:30(firebase timestamp format)
Thanks in advance
Date: Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as
"the epoch", namely January 1, 1970, 00:00:00 GMT.
import java.util.Date
Simply use Date() as val date = Date() (firebase timestamp format)
while fetching data from firebase use below code
val date = getReadableDateTime(document.getDate("timestamp")) // timpestamp is the key inside your document
private fun getReadableDateTime(date: Date): String {
return SimpleDateFormat("MMMM dd, yyyy - hh:mm a", Locale.getDefault()).format(date)
}
Related
Where do I get the taken date of the video from MediaStore? I got the following fields from MediaStore.
MediaStore.Video.Media.DATE_MODIFIED
MediaStore.Video.Media.DATE_TAKEN
MediaStore.Video.Media.DATE_ADDED
Those fields returned seemly default values -
dateModified: 1477043336
dateTaken: 1477043336000
dateAdded: 1477043352
Formatted dates -
dateModified: 01/01/1970
dateTaken: 01/01/1970
dateAdded: 01/01/1970
I double checked the stock gallery > random video file and I do see the correct dates. I looked at the video columns in MediaStore and I didn't see any other columns which has correct dates.
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = dateFormat.format(new Date(row.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED)));
String dateModified = dateFormat.format(new Date(row.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED)));
String dateTaken = dateFormat.format(new Date(row.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN) * 1000L));
String dateAdded = dateFormat.format(new Date(row.getColumnIndex(MediaStore.Video.Media.DATE_ADDED) * 1000L));
Log.d(TAG, "dateModified: "+dateModified);
Log.d(TAG, "dateTaken: "+dateTaken);
Log.d(TAG, "dateAdded: "+dateAdded);
Log.d(TAG, "dateModified: "+row.getString(row.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED)));
Log.d(TAG, "dateTaken: "+row.getString(row.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN)));
Log.d(TAG, "dateAdded: "+row.getString(row.getColumnIndex(MediaStore.Video.Media.DATE_ADDED)));
//Just multiply it by 1000 to get correct date
fun convertLongToDate(time: Long): String =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
DateTimeFormatter.ofPattern("dd MMMM yyyy").format(
Instant.ofEpochMilli(time*1000)
.atZone(ZoneId.systemDefault())
.toLocalDate())
} else {
SimpleDateFormat("dd MMMM yyyy").format(
Date(time * 1000)
)
}
Looking at the annotations on the interface, DATE_ADDED and DATE_MODIFIED are annotated as SECONDS since the epoch, rather than milliseconds. DATE_TAKEN however is annotated as milliseconds since the epoch.
This difference in annotation explains the differences in zeroes that CommonsWare's answer notes. It also guides usage:
Since date formatters usually expect timestamps in millis, you should multiply second values by 1000 first.
Here is a simple function to get actual result of date format.
public String getDate(long val){
val*=1000L;
return new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(val));
}
Those fields returned seemly default values
I do not know why your second one has three extra zeros at the end. But, using a Unix date converter site:
dateModified = 1477043336 = Fri, 21 Oct 2016 09:48:56 GMT
dateAdded = 1477043352 = Fri, 21 Oct 2016 09:49:12 GMT
And your dateTaken, without the zeros, is the same as dateModified. So, assuming you can figure out where your zeros came from (such as by randomly deciding to multiply the value by 1000L), you have valid timestamps.
Syntax for convert epoch to normal date in android as follows
long date=System.currentTimeMillis(); //current android time in epoch
Converts epoch to "dd/MM/yyyy HH:mm:ss" dateformat
Means 1477043336 = 21/10/2016 09:48:56
String NormalDate = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(date));
Explanation:
I have time in GMT format i need to convert into IST. I did already but for 24 hour i want to get in 12 hour format with AM/PM.
here is my code
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm'+00':ss");
utcFormat.setTimeZone(TimeZone.getTimeZone("IST"));
Date date1;
date1=utcFormat.parse(time);
Log.e("IST",""+date1.toString());//After convert in IST i got Sat Mar 26 19:30:00 GMT+05:30 2016
Date timestamp;
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
timestamp = utcFormat.parse(time);
Log.e("timestamp", "" + timestamp.toString()); //After convert in UTC i got Sat Mar 26 19:30:00 GMT+05:30 2016
Log.e("Time",""+time.toString());//This is my time 2016-03-26T14:00+00:00
Problem is i got the same time for IST and GMT.
What will i need to do so than i will get a time in 12 hours???
Please help me to solve out this problem.
Remember that the Date class does not hold any actual timezone information, it is just a long millisecond value from UNIX epoch time. And the TimeZone of the SimpleDateFormat is only relevant when you display the date, not when you parse it. So in order to print your Date as an IST date, you must use that format when printing the Date:
// Parse your time string
DateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm'+00':ss");
Date date = myFormat.parse(time);
// Set a new format for displaying your time
DateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
isoFormat.setTimeZone(TimeZone.getTimeZone("IST"));
// This will print the IST time in the format specified: 2016-03-25T09:42:07+5:30
String istTime = isoFormat.format(date);
Log.d(istTime);
As for the 12 hour format, you can set that with a new SimpleDateFormat when you print the time.
/**
* 'hh' is the 12-hour time format, while 'HH' is the 24-hour format
* 'hh' will always print two digits, while 'h' will drop leading zeros
* 'a' is the AM/PM marker
*/
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
// This will print the current time, for example: 09:42 AM
Log.d(dateFormat.format(new Date()));
Android also has a helpful DateUtils class, that will format the Date based on the settings of the current device.
Here i was created a another object of DateFormat and pass a parameter inside it' constructor.
//hh:mm a format of 12 hour with leading 0 before the hours, mm is for minutes and (a) is for AM/PM format.
//where as HH is provides the format of 24 hours.
Here i got a 12 hours format.
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm'+00':ss");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat outputFormat = new SimpleDateFormat("hh:mm a");
String ist=outputFormat.format(utcFormat.parse(time));
In my app i need to convert my current date into UTC format, i can successfully converted, now problem is i need 24 hours format check it out my below code
public static String CurrentDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
LogUtil.d("new Date()" + new Date());
return df.format(new Date());
}
now my CurrentDate returns 1.45 but i need 13.45 how can i convert utc in 24 hours format?
i have search through google but dint get proper answer, all suggestions are most welcome
Thanks in advance
Changing the hour part at your SimpleDateFormat constructor call from hh to HH does the job:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Output:
2015-07-13 13:53:02
See also Table of Date and Time Patterns
As Jon Skeet said, check the String you pass on SimpleDateFormat() : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You need to replace "hh" with "HH" so it will become "yyyy-MM-dd HH:mm:ss"
Of course if you only need the hour and minute that should be "HH:mm"
when i am print System.currentTimeMillis()
give me :
11-03 14:47:05.400: INFO/System.out(7579): date is :: 14475410111
What is the correct procedure to get entire date with time.?
To get current date and time in Android, You can use:
Calendar c = Calendar.getInstance();
System.out.println("Current time => "+c.getTime());
Output:
Current time => Thu Nov 03 15:00:45 GMT+05:30 2011
FYI, once you have this time in 'c' object, you can use SimpleDateFormat class to get date/time in desired format.
Final Solution:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Sring formattedDate = df.format(c.getTime()); // c is Calendar object
System.out.println("========> formatted date => "+formattedDate);
Output:
========> formatted date => 2011-11-03 15:13:37
Date now = new Date(System.currentTimeMillis());
By the way : currentTimeMillis()
Returns the current system time in milliseconds since January 1, 1970 00:00:00 UTC.
Yes This is the correct way to get the current time and date. afte that you need to convert it to desired format.
public static String getDateFromTimeStamp(String timeStamp){
return new Date(Long.parseLong(timeStamp)).toString();
}
Use the Date class.
(What makes you think that's wrong? You're asking for the time in milliseconds.)
Use SimpleDateFormat
In my application the Date Fiend JSON response comes as this type:
"CreatedOn": "\/Date(1313572467987+0000)\/"
I want to convert this date in the "MM DD YYYY" format. How can I convert this?
That date in your JSON response looks like a standard timestamp (e.g. number of milliseconds since January 1, 1970). If you parse out the timestamp something like:
String timestamp = jsonValue.split("\\(")[1].split("\\+")[0];
Date createdOn = new Date(Long.parseLong(timestamp));
Now you can use SimpleDateFormat to format a date string:
SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy");
String formattedDate = sdf.format(createdOn);
This is ignoring the timezone adjustment in that response (the '+0000') you might also want to parse out this value and add/remove the hours from your timestamp value before formatting..
you can use SimpleDateFormat, so just try to search on the same.
Detailed example is here: http://www.helloandroid.com/tutorials/date-handling-android-development