Viking
Viking

Reputation: 127

Getting different DateFormat result on emulator and on phone-Android

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!!

Upvotes: 0

Views: 1749

Answers (3)

Ajay Kumar Meher
Ajay Kumar Meher

Reputation: 1952

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

Upvotes: 2

Adam
Adam

Reputation: 418

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());

Upvotes: 1

Sukitha Udugamasooriya
Sukitha Udugamasooriya

Reputation: 2308

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

Upvotes: 3

Related Questions