FormatDateTime

by Nick Owens » Wed, 24 Feb 2010 21:44:45 GMT


Sponsored Links
 Experts (like Mark M et al.):

Android development is definitely a moving target, the way the SDK changes.
What's most frustrating about it is when it looks like great functionality
is no longer available!  You see some sample code which looks AWESOME and is
EXACTLY what you need, but come to find out it's been removed.  Usually you
can replicate it but I am having the darndest time trying to replicate the
following function from DateUtils!

getRelativeDateTimeString()

This function supposedly provides output similar to the Call Log, which
shows times as "yesterday at 9:30 AM" or "10 mins ago", etc.  Awesome little
function, right?  Can't find a replacement ANYWHERE!

Thanks,
Nick Owens
VP, ThreeClix
Office: (904) 429-7039
Mobile: (847) 565-9392
After Hours: (904) 540-5830



--



FormatDateTime

by Mark Murphy » Wed, 24 Feb 2010 21:55:40 GMT


 


Why do you need a replacement? There's a half-dozen flavors of that
method on DateUtils, all available since API Level 3 (a.k.a., Android 1.5).

If you're looking for the source to those methods, try Google Code Search.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

_Android Programming Tutorials_ Version 1.9 Available!

--


Sponsored Links


FormatDateTime

by Nick Owens » Wed, 24 Feb 2010 22:00:46 GMT


 Mark:

That's great news - I'm cheered up already knowing it can be done easily
again.  I did try searching for a few hours to find a good replacement.  I
searched for:

"android relative date time format" (returns nothing seemingly related)

- and -

"getRelativeDateTimeString()" (returns 0 results)

If I include the word DateUtils, I get 1000+ results for older code, which
includes import android.text.format.DateUtils or a similar library.

Thanks,
Nick Owens
VP, ThreeClix
Office: (904) 429-7039
Mobile: (847) 565-9392
After Hours: (904) 540-5830


-----Original Message-----
From: android-developers@googlegroups.com
[mailto:android-develop...@googlegroups.com] On Behalf Of Mark Murphy
Sent: Wednesday, February 24, 2010 8:55 AM
To: android-developers@googlegroups.com
Subject: Re: [android-developers] FormatDateTime



changes.
is
you
little

Why do you need a replacement? There's a half-dozen flavors of that
method on DateUtils, all available since API Level 3 (a.k.a., Android 1.5).

If you're looking for the source to those methods, try Google Code Search.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

_Android Programming Tutorials_ Version 1.9 Available!

--



FormatDateTime

by Mark Murphy » Wed, 24 Feb 2010 22:11:51 GMT


 


I get 28 results, the third of which is the developer.android.com page:

 http://developer.android.com/reference/android/text/format/DateUtils.html 


That's not a library. That's a class. It's built into Android.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Android App Developer Books:  http://commonsware.com/books 

--



FormatDateTime

by Nick Owens » Wed, 24 Feb 2010 22:39:11 GMT


 Mark:

Sorry about that, it is.  I assumed it had been removed since I am not able
to use getRelativeDateTimeString() in the following:

----------------------------------------------------------------------------
long dateMin = 60000;
long dateMax = (86400000 * 2);

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                          
Date date = df.parse(dateTime);

long when = date.getTime();

int flags = 0;
                         
String finalDateTime = DateUtils.getRelativeDateTimeString(this, (when +
TimeZone.getDefault().getOffset(when)), dateMin, dateMax, flags);

return finalDateTime;
----------------------------------------------------------------------------

So when it was switched over from a Library to a built-in Class, was this
function removed?

Thanks,
Nick Owens
VP, ThreeClix
Office: (904) 429-7039
Mobile: (847) 565-9392
After Hours: (904) 540-5830


-----Original Message-----
From: android-developers@googlegroups.com
[mailto:android-develop...@googlegroups.com] On Behalf Of Mark Murphy
Sent: Wednesday, February 24, 2010 9:11 AM
To: android-developers@googlegroups.com
Subject: Re: [android-developers] FormatDateTime




I get 28 results, the third of which is the developer.android.com page:

 http://developer.android.com/reference/android/text/format/DateUtils.html 


That's not a library. That's a class. It's built into Android.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Android App Developer Books:  http://commonsware.com/books 

--



FormatDateTime

by Mark Murphy » Wed, 24 Feb 2010 22:51:09 GMT


 


Click on the following link:

 http://developer.android.com/reference/android/text/format/DateUtils.html 

This is what is known as "JavaDocs", the API documentation for Java
classes. Specifically, you are looking at android.text.format.DateUtils,
the class you are trying to use.

If you scroll down on that page, you will see a table named "Public
Methods". This contains a list of public methods available on the class.

If you scroll down into that table, you will see six editions of the
getRelativeDateTimeString() method, with different parameter sets.

Now, I don't know what "this" is in your code snippet. If "this" were a
Context (e.g., Activity, Service), then the first method signature would
seem to match your parameters. Hence, my best guess is that "this" is
not a Context -- fix that, and you should be able to use the method.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

_Beginning Android_ from Apress Now Available!

--



FormatDateTime

by Nick Owens » Wed, 24 Feb 2010 23:21:41 GMT


 Mark:

The error in the screenshot (emailed separately), shows:

"The method getRelativeDateTimeString(ResourcesList, long, long, long, int)
is undefined for the type"

The declaration of my Activity is:

Public class ResourcesList extends ListActivity

I'm missing something stupid, right?

Thanks,
Nick Owens
VP, ThreeClix
Office: (904) 429-7039
Mobile: (847) 565-9392
After Hours: (904) 540-5830


-----Original Message-----
From: android-developers@googlegroups.com
[mailto:android-develop...@googlegroups.com] On Behalf Of Mark Murphy
Sent: Wednesday, February 24, 2010 9:51 AM
To: android-developers@googlegroups.com
Subject: Re: [android-developers] FormatDateTime



able
----------------------------------------------------------------------------
----------------------------------------------------------------------------

Click on the following link:

 http://developer.android.com/reference/android/text/format/DateUtils.html 

This is what is known as "JavaDocs", the API documentation for Java
classes. Specifically, you are looking at android.text.format.DateUtils,
the class you are trying to use.

If you scroll down on that page, you will see a table named "Public
Methods". This contains a list of public methods available on the class.

If you scroll down into that table, you will see six editions of the
getRelativeDateTimeString() method, with different parameter sets.

Now, I don't know what "this" is in your code snippet. If "this" were a
Context (e.g., Activity, Service), then the first method signature would
seem to match your parameters. Hence, my best guess is that "this" is
not a Context -- fix that, and you should be able to use the method.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

_Beginning Android_ from Apress Now Available!

--



FormatDateTime

by Mark Murphy » Wed, 24 Feb 2010 23:29:05 GMT


 


Well, I'm not an Eclipse user. What you have should be fine, and I don't
know why it is coming up with that complaint.

My standard hammer for nails like this is Project > Force Clean, to see
if that clears matters up.

But, I've taught enough people who use Eclipse to see that Eclipse is
more frequently wrong about what's in the SDK than are the SDK docs. So,
if you get errors like this, check the docs first.

And, this is one of the umpteen reasons why I don't use Eclipse.

(saying that in part to see if Eric Cloninger is watching this thread,
to perhaps goad him into providing other solutions for dealing with
Eclipse tossing up errors on what, on the surface, looks like perfectly
good code)

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

_Beginning Android_ from Apress Now Available!

--



FormatDateTime

by Bob Kerns » Thu, 25 Feb 2010 05:50:51 GMT


 I really doubt that Eclipse is wrong here. First, a couple quick-and-
basic steps.

If you hold the mouse over each variable, it will tell you the type.

Finally, if you click on the 'this' the code, and type control-T,
you'll get a pop-up with showing the class hierarchy. The second from
the type should read 'Context -- android.content'

Now, finally: On the left where it shows the Package Explorer, click
right on your project name (at the top level of the hierarchy). Choose
Properties, and then choose Android in the property pane that comes
up, to see the Android-related properties. It will give you a table of
Android target names.

My guess: You have selected Android 1.1 (API Level 2). The method in
question, according to the nice Javadoc, was introduced in API level
3. Then Eclipse should let you build your code.

Eclipse is telling you that the method isn't available in that version
of the SDK. It compiles against that specific version of android.jar,
based on that checkbox.

So what does it mean to select a different version? It means your code
will compile, but will only work on devices newer than Android 1.1. In
the androidManifest.xml file, there are flags indicating what the
minimum and intended SDK versions are. If you indicate it should work
on API 2, they'll be able to install it, but get a NoSuchMethodError
at runtime.

There are very few devices running Android 1.1, but you'll have to
decide what you want to do about that. Statistics are available.






--



FormatDateTime

by Nick Owens » Fri, 26 Feb 2010 04:40:51 GMT


 ob:

You're probably right b/c I've been building for Android 1.1. In an effort
to make a decision about this feature, I did some searching for statistics
on installed API levels but couldn't find them.

On another note, is it possible to provide a conditional instruction for
formatting date/time based on the user's Android package:

if (package > 1.1) {

//cooler date time format

} else {

//regular date time format

}

Let me guess? It is technically possible, but not in Eclipse since Eclipse
won't even let it compile w/ a function that doesn't compile. Or can I
build it for Android 1.2 but allow users of 1.1 to download it by specifying
a different min-SDK level than it is built for? If so, can I write a
conditional instruction based on the device's SDK level?

Thanks,
Nick Owens
VP, ThreeClix
Office: (904) 429-7039
Mobile: (847) 565-9392
After Hours: (904) 540-5830

-----Original Message-----
From: android-developers@googlegroups.com
[mailto:android-develop...@googlegroups.com] On Behalf Of Bob Kerns
Sent: Wednesday, February 24, 2010 4:51 PM
To: Android Developers
Subject: [android-developers] Re: FormatDateTime

I really doubt that Eclipse is wrong here. First, a couple quick-and-
basic steps.

If you hold the mouse over each variable, it will tell you the type.

Finally, if you click on the 'this' the code, and type control-T,
you'll get a pop-up with showing the class hierarchy. The second from
the type should read 'Context -- android.content'

Now, finally: On the left where it shows the Package Explorer, click
right on your project name (at the top level of the hierarchy). Choose
Properties, and then choose Android in the property pane that comes
up, to see the Android-related properties. It will give you a table of
Android target names.

My guess: You have selected Android 1.1 (API Level 2). The method in
question, according to the nice Javadoc, was introduced in API level
3. Then Eclipse should let you build your code.

Eclipse is telling you that the method isn't available in that version
of the SDK. It compiles against that specific version of android.jar,
based on that checkbox.

So what does it mean to select a different version? It means your code
will compile, but will only work on devices newer than Android 1.1. In
the androidManifest.xml file, there are flags indicating what the
minimum and intended SDK versions are. If you indicate it should work
on API 2, they'll be able to install it, but get a NoSuchMethodError
at runtime.

There are very few devices running Android 1.1, but you'll have to
decide what you want to do about that. Statistics are available.

On Feb 24, 7:28am, Mark Murphy <mmur...@commonsware.com> wrote:
int)
Guy)http://commonsware.com|http://twitter.com/commonsguy

--



FormatDateTime

by Mark Murphy » Fri, 26 Feb 2010 05:23:32 GMT


 


Yes, either via reflection or via conditional class loading.


No, it should work fine. However, you need to compile for the higher
version.


Dunno. What's Android 1.2? :-)


Yes, either via reflection or via conditional class loading.

This was discussed here:

 http://stackoverflow.com/questions/2312321/how-to-use-contacts-api-for-android-1-x-and-2-x-simultaneously 

and here:

 http://stackoverflow.com/questions/2044985/android-contactscontract-and-building-across-multiple-sdk-versions 

Some sample projects are here:

 http://github.com/commonsguy/cw-advandroid/tree/master/Contacts/Pick/ 
 http://github.com/commonsguy/cw-advandroid/tree/master/Contacts/Spinners/ 

First one uses reflection, second one uses conditional class loading.

Here are two more samples:

 http://github.com/commonsguy/cw-android/tree/master/APIVersions/ 

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Warescription: Three Android Books, Plus Updates, One Low Price!

--



FormatDateTime

by Nick Owens » Fri, 26 Feb 2010 06:38:32 GMT


 ark (et. al. interested parties):

Thank you for the example on reflection. I got it working and thought it
might be prudent to share my results w/ the world so the completed code is
below. For the record, this date value (as a string) is being retrieved
from a SQLite database and the function formatDateTime() is used in a custom
adapter for display of the date for each record in a ListView.

This compiles in Eclipse using Android 1.5. This allows me to set a minimum
sdk version of 2, while still utilizing this really nifty date display
function.

----------------------------------------------------------------------------
------------
public class ResourcesList extends ListActivity {

...

int sdk = new Integer(Build.VERSION.SDK).intValue();

private static final long dateMin = 60000;
private static final long dateMax = (86400000 * 2);
private static final int dateFlags = 0;

...

public String formatDateTime(String dateTime) {

if (dateTime == null) {

return "Never";

} else {

String finalDateTime;

DateFormat df = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");

try {

Date date = df.parse(dateTime);

if (sdk < 3) {

finalDateTime = df.format(date);

} else {

long when = date.getTime();

finalDateTime =
DateUtils.getRelativeDateTimeString(this, (when +
TimeZone.getDefault().getOffset(when)), dateMin, dateMax,
dateFlags).toString();

}

} catch (Exception e) {

finalDateTime = "Unknown";

}

return finalDateTime;

}

}

...

}

Thanks,
Nick Owens
VP, ThreeClix
Office: (904) 429-7039
Mobile: (847) 565-9392
After Hours: (904) 540-5830


-----Original Message-----
From: android-developers@googlegroups.com
[mailto:android-develop...@googlegroups.com] On Behalf Of Mark Murphy
Sent: Thursday, February 25, 2010 4:23 PM
To: android-developers@googlegroups.com
Subject: Re: [android-developers] Re: FormatDateTime

Nick Owens wrote:

Yes, either via reflection or via conditional class loading.

Eclipse

No, it should work fine. However, you need to compile for the higher
version.

specifying

Dunno. What's Android 1.2? :-)


Yes, either via reflection or via conditional class loading.

This was discussed here:

http://stackoverflow.com/questions/2312321/how-to-use-contacts-api-for-andro
id-1-x-and-2-x-simultaneously

and here:

http://stackoverflow.com/questions/2044985/android-contactscontract-and-buil
ding-across-multiple-sdk-versions

So



Other Threads

1. OOT: bikinin stiker gini donk

Ada yg bisa buatin sticker gini gak?

Siapa tau aja..lutju sih

-- 
"Indonesian Android Community [id-android]" 

2. Adding menu items in a WebView context menu

Hi all,

I'm trying to add custom menu items in a WebView Context Menu.
Unfortunately, my items are shown in the menu of the first web page I
load , but then if I load a second page, a long press only shows the
standard context menu items. Mine are gone.

I've put a Toast inside the code and it is displayed as well on the
first loaded page, but not on the second.

I'm using the code below :

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                        ContextMenuInfo menuInfo) {
                contextMenu=menu;

                Toast.makeText(this, "getpicturefromweb", 
Toast.LENGTH_LONG).show();

                MenuItem menuItem=contextMenu.add("getpicturefromweb");
                menuItem.setEnabled(true);
                menuItem.setIcon(R.drawable.icon);

                super.onCreateContextMenu(menu, v, menuInfo);
        }

I think the context menu is repopulated depending on what you
longpress(link, image, ...) and I didn't find the correct hook.

I've read about a OnPopulateContextMenuListener hook, but I get a
unresolved import when I try to add it. Maybe it's deprecated but I
don't know I could not find it in the Api docs.

Last and not least, if someone knows how to solve the avobe, I'd like
to know if it's possible in some way to add my item only on specific
objects ?
Like a "Use this picture" on a picture.

Thanks in advance.

Yahel

-- 

3. hp ber-android...

4. Tanya Wipe & No Wipe

5. htc magic tdk bisa booting

6. Option menu, which way is better?

7. Screen Color depth