0

I wanted to display date and time at a particular instance in my android app.I used the following piece of code to achieve it.

Date now = new Date();
final String time = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT).format(now);

Now the problem is that on emulator it shows dates and time in a perfect format just the way i wanted

Sep 1,2012 10:30pm

But when i run my app on phone it displays date and time on the following format:

2012 9 1 10:30

Can anyone please tell me why im getting different behavior on emulator and on phone? and what should i do in order to achieve the same result on phone as im getting on emulator?

Thank You!!

Viking
  • 127
  • 2
  • 13
  • check this link once http://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android – shassss Sep 03 '12 at 05:00
  • @shassss thank you for your response.I did checked that link.But i didn't get that how can i get the result like "Sep 1, 2012 10:30pm" from those examples posted on that question? – Viking Sep 03 '12 at 05:08
  • Have you looked at all at the locale settings you've got on the emulator vs the real device? – brianestey Sep 03 '12 at 05:31
  • @brianestet thank you for your response.yes its different but i'm trying to get same date everywhere regardless of device,OS,settings – Viking Sep 03 '12 at 06:08

3 Answers3

3

Date formats may change phone to phone, pc to phone or so. But you may use a date formatter so that you can achive uniformity.

Documentation - SimpleDateFormat

Example

Sukitha Udugamasooriya
  • 2,268
  • 1
  • 35
  • 56
  • thank you for your response.i almost got the result.I changed my format in this line > SimpleDateFormat df3 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a"); to SimpleDateFormat df3 = new SimpleDateFormat("MMM dd,yyyy HH:mm a"); this is giving me date and time in format 9 03,2012 2:05 AM.Can you please tell me how can i make month "9" to display as "Sep"? – Viking Sep 03 '12 at 06:06
  • it should be "yyyy-MMMM-dd" ex output: 1970-January-01 ---AND--- "yyyy-MMM-dd" ex output: 1970-Jan-01 – Sukitha Udugamasooriya Sep 03 '12 at 06:42
  • thank for your response.i tried that but it didn't work.It works after adding Locale. – Viking Sep 03 '12 at 17:51
2

Sukitha your hint works. I guess Viking missed it.

Anyways, here it goes.(Tested in Motorola DROID)

String format = "MMM d,yyyy H:m a";
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
        System.out.println("Time : "+sdf.format(new Date(System.currentTimeMillis())));
        Toast.makeText(this, "Time : "+sdf.format(new Date(System.currentTimeMillis())), 1000).show();

Output: Time : Jan 4,1970 6:52 AM

Ajay Kumar Meher
  • 1,932
  • 16
  • 24
  • thanks for your response.This code gave me an output 9 2,2012 2:0 AM thus its not working for me. – Viking Sep 03 '12 at 06:03
1

Edit:

Use the following code then you will get it same on both emulator and device.

Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("MMM-dd-yyyy HH:mm a");
String formattedDate1 = df.format(c.getTime());
Adam
  • 418
  • 4
  • 14
  • thank you for your response.im running android 2.2 on both is that what you meant by setting up api?? Also i want to achieve same result regardless of api is that possible? – Viking Sep 03 '12 at 05:09