Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Location time into date [closed]

I'm using the Location class and the getTime() method to get the current time in Android. Then I'm sending that Long number to some other device. That device receives the Long number and should display the data in a message box, but I don't know how to convert it into DD:MM:YY and time of the day (or anything readable by a human, actually). How do I do this? I can't use the current format, it has to be something that the average user can read.

Thank you!

like image 519
user1549053 Avatar asked Jul 29 '26 20:07

user1549053


1 Answers

Assuming you don't want to use Joda Time (might be a bit big for an Android app) you should probably use a DateFormat such as SimpleDateFormat:

// See notes below
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date(location.getTime());
String formatted = format.format(date);

There are cultural issues you should consider though:

  • You may wish to explicitly set the locale, if you believe the default may not suit you
  • Rather than hard-code the format, you may want to use DateFormat.getDateTimeInstance()
  • You should consider which time zone you're interested in; presumably the user's time zone, which should be the default for any DateFormat you create, but perhaps something else...
like image 142
Jon Skeet Avatar answered Aug 01 '26 11:08

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!