How to convert date and time to text in Android

Sep 26

Written by:
9/26/2011 10:03 AM  RssIcon

When I was looking how to convert date and time to text in Android, I found method  Date.toLocaleString(). This method is deprecated, and we should use DateFormat class. 

The most important methods of DateFormat class are getDateTimeInstace(), getDateInstance() and getTimeInstance(). You need to call one of this methods in order to get correct DateTime class instance for converting dates and times to strings.

If you call any of this methods without parameters you will get formater which will convert your date or time to default string format for the user's default locale.

Important:

It is important that you use java.text.DateFormat  and not  android.text.format.DateFormat, so check your imports if you have any errors.

Some examples

To convert date and time to default format:

Date dt = new Date();
DateFormat formater = DateFormat.getDateTimeInstance();
String formated = formater.format(dt);


To convert date and time to specific format:

Date dt = new Date();
DateFormat formater = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
String formated = formater.format(dt);

For more information about DateFormat class see here



Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

Search Blog

Featured articles

GIMP script for creating Android icons at once

Script for creating Android icons for different screen resolutions at once. Icons can be saved using standard Android icons naming conventions and saved in appropriate folders.

Creating Android button with image and text using relative layout

Source code with examples how to use relative layout to create nice buttons with text and images in Android

Android application and the Internet

Tutorial about connecting to the web pages on the Internet from Android application, using both POST and GET web requests.

Recent Entries

Android application and the Internet
Keep my DNN site alive
Android default icons
How to format code blocks in DotNetNuke 6
How to convert date and time to text in Android

Recent Comments

Flash The Net
Izvorni kod >Blog - How to force Eclipse to rebuild generated resource class (R) in Android project ?
# Flash The Net
Great Info.
Great information you got here. I've been reading about this topic for one week now for my papers in school and thank God I found it here in your blog. I had a great time reading this.
Re: GIMP script for creating Android icons at once
Thank you for supporting me :)
Re: GIMP script for creating Android icons at once
Thank you so much! Have donated some money to you so you can buy yourself a good beer.
You deserve it!
Re: GIMP script for creating Android icons at once
Hello all,
script is updated with some new features.

Added support for:
- action bar icons
- icons with custom size
- xhdpi screen icon

I hope that there is no bugs introduced.

Enjoy