MediaStore.Video.Media.DATE_MODIFIED format - android

Can anyone show me how to format the MediaStore.Video.Media.DATE_MODIFIED to the human readable date format. I read in the docs that the time is in seconds. So i simply multiply it by 1000 and use the SimpleDateFormat . Here is my code
Date d=new Date(mills*1000);
DateFormat df=new SimpleDateFormat("dd-mm-yyyy");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return df.format(d);
This returns a String 25-35-2012 that off course is not correct. Any help with this ?
Kind Regards

you should use capital M, m refers to minute in hour while M refer to Month in year.
see this for further info

Related

How to change hours data from 24-hour format to 12-hour format?

I'm retrieving hours data for places from a service (Factual). It comes to me in 24-hour format and I need to display it in 12-hour format. The data for a specific day comes like this:
"sunday\":[[\"12:00\",\"21:30\"]]
I can successfully retrieve the hours from the JSON. Then, using SimpleDateFormat, I can parse the string to a Date object. But, then I can't figure out how to convert them to 12;-hour format so that I can display them as "12:00 - 9:30" or "12:00pm - 9:30pm" rather than "12:00 - 21:30".
How can I go about doing this? Thanks!
EDIT:
By parsing the string of hours (i.e. "12:00") using SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm a");, I get an error from JSON saying that the value is unparseable. If I use just SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm");, then there's no error but I can't get things to show up in 12-hour format.
If you look in the simple date format syntax docs you will find that 'h' is used for 12-hour time and 'a' is used for AM/PM. You will need to extract the two times using substring before putting them through the dateformatters.
http://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat in = new SimpleDateFormat("<input format goes here>");
Date d = in.parse(INPUT_DATE_STRING);
SimpleDateFormat out = new SimpleDateFormat("<output format goes here>");
String outDate = out.format(d);
Try this:
//Char sequence for a 12 hour format.
CharSequence DEFAULT_FORMAT_12_HOUR = "hh:mm a";
//Char sequence for a 24 hour format.
CharSequence DEFAULT_FORMAT_24_HOUR = "kk:mm";
//date is the Date object. Look for more functions in format.
DateFormat.format(DEFAULT_FORMAT_12_HOUR, date);
Let me know if it works. If you have any issue check the Date you are sending.
//This should give you the default time on the device. To show that it works.
DateFormat.format(DEFAULT_FORMAT_12_HOUR, Calendar.getInstance());

Conversion of Epoch Date to Human Readable format in android

I have a problem that I am having an Epoch date which is coming from Web Services, I want to display it in Human Readable format as, July 12, 2012, but my code always shows 16/01/1970 for any Epoch Date. I don't know where I am Wrong or How to convert Epoch Date to Date in Java. Please suggest me any solution regarding the same.
Code:
Date.setText(new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(Long.parseLong(newsDetail.getDate_Posted()))));
Thanks in advance.
I have a hunch that Long.parseLong(newsDetail.getDate_Posted()) is returning seconds from epoch not milliseconds.
If you always see 16/01/1970, then Long.parseLong(newsDetail.getDate_Posted()) is between 1296000000 and 1382400000. It turns out that if I multiple these minimum and maximum number by 1000, I see dates between 26/1/2011 and 21/10/2013. I'm guessing that the date you are expecting falls in this new range.
Try using:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
date.setText(sdf.format(new Date(Long.parseLong(newsDetail.getDate_Posted()) * 1000)));

android date toString

I"m following a tutorial where they have this line:
int callDate = c.getInt(dateColumn);
Next, they pass the callDate into the Date object to get a readable format.
But, I think the example may be old, because I get a compile error.
String myDate = DateUtils.dateString(callDate).toString();;
So, I'm looking at the API. how do I do it.
http://developer.android.com/reference/java/util/Date.html
String myString = DateFormat.getDateInstance().format(new Date());
or
Date date = new Date();
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
Look at DateFormat and SimpleDateFormat.
You might want to use SimpleDateFormat for that
If I have understood your question correctly, you need to get the date in readable format.In that case you already have the answer you just need to read the link http://developer.android.com/reference/java/util/Date.html carefully for finding the answer. Let me provide you few hint for the same, see for the constructors on this link for initializing the date of your choice. The methods given helps you perform the operations on it. To answer your question specifically refer below snippet.
Date d1=new Date(); //Initializes this Date instance to the current time.
String date= d1.toString(); //this String var date can be used for printing date in readable format
the format of string will be "EEE MMM dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00 PDT 1999".
See the below link. It will definitely help.
Simple java date conversion
the problem is that you are getting from a Integer
int callDate = c.getInt(dateColumn);
the date is a Long type and you're losing precision on this conversion.
try changing to: long callDate = c.getLong(dateColumn);

What is wrong with my date format?

I am trying to get the date in this format "time":"05:09pm 08/02/2011". What I have so far is
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(
"HH:mmaa MM/dd/yyyy");
holder.put("time", sdf.format(c.getTime()));
and this is what comes out.
"time":"21:28PM 08\/26\/2011"
Why is this happening and what can I do to fix it? Thanks
I suspect you want a format of
hh:mmaa MM/dd/yyyy
As hh is the 12-hour format in the range 01-12. I find that's typically how humans represent 12-hour values. Of course if you want 00-11, use KK instead as suggested by MByD.
(Quite what the rationale is for the capitalized form being the 24-hour version of HH, but the lower case form being the 24-hour version of kk, I don't know...)
If you were worried by the backslashes in the output, I suspect that's just JSON-escaping. I'm surprised it's necessary for forward-slashes, but it shouldn't do any harm.
Try changing HH:mmaa MM/dd/yyyy to KK:mmaa MM/dd/yyyy. (KK is hour in am/pm, 0-11)

android timestamp

hi i have a small question please i am new to android and have a date and time stamp
which looks like this yyyy-mm-dd hh:mm:ss
and i want to insert it into an sqlite table then read it back and compare it to the current time
any suggestions or examples
i found "SimpleDateFormat" but was not sure how to use it....?
thanks a lot
Have you considered using Calendar.getInstance.getTimeInMillis()? It's just the long representation of a date in milliseconds since Jan. 1, 1970. Great thing is your data is being stored in a format agnostic way. Just when you need to display it use SimpleDateFormat however you'd like.
Here is what you would do:
String myDate = new String("your date");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(myDate);
date.getTime(); //fetch the time as milliseconds from Jan 1, 1970
Try use
System.currentTimeMillis();
its undepend from Calender.

Categories

Resources