3

I am working simple app where I can get current DateTime and convert that into 24-hour format.

Code:

  String DATE_yyyy_MM_dd_hh_mm_ss = "yyyy-MM-dd hh:mm:ss";

  String DATE_yyyy_MM_dd_HH_mm_ss  = "yyyy-MM-dd HH:mm:ss";  
  

  TextView tv=(TextView)findViewById(R.id.textView);
    
  tv.append("\n in 12-hour format: "+getDateFormatted(bootDate));
    
  tv.append("\n in 24-hour format: "+getDateFormatted2(bootDate));

  public String getDateFormatted(Date date){
    return String.valueOf(DateFormat.format(DATE_yyyy_MM_dd_hh_mm_ss, date));
}

public String getDateFormatted2(Date date){
    return String.valueOf(DateFormat.format(DATE_yyyy_MM_dd_HH_mm_ss, date));
}

Problem:

It's perfectly working in my devices Samsung Galaxy S3(Android 4.3), S4(Android 4.3) and Nexus 5(Android 4.4). while the same code I am running in Huawei Ascend Y330 device(Android 4.2.2) it's not display properly.

Screenshot in Samsung Galaxy S3, S4 and Nexus 5:

enter image description here

Screenshot in Huawei Ascend Y330:

enter image description here

So, what is the problem actually? I don't understand. Is it android system issue? or device issue?

Any idea anyone.

Community
  • 1
  • 1
M D
  • 47,665
  • 9
  • 93
  • 114
  • Do you have this issue only with Huawei Ascend Y330? – Skizo-ozᴉʞS ツ Feb 17 '15 at 10:04
  • 4
    Did you try using [SimpleDateFormat](http://developer.android.com/reference/java/text/SimpleDateFormat.html) instead of DateFormat (like [this post](http://stackoverflow.com/questions/5755073/24-hour-clock-not-working-in-dateformat-android?answertab=active#tab-top) explained) ? – lgd Feb 17 '15 at 10:04
  • @Skizo Only in `Huawei Ascend Y330` particular device while it's perfect working in all other 5 devices – M D Feb 17 '15 at 10:04
  • @heRoy Ok wait let me try... – M D Feb 17 '15 at 10:05
  • Try using `SimpleDateFormat` as @heRoy said, and let us know if it worked, if it didn't probalby it's a device issue... – Skizo-ozᴉʞS ツ Feb 17 '15 at 10:06
  • @heRoy It's working fine with `SimpleDateFormat`. thanx budy. I supposed already try.Please post it with some answer. So other people able to know that... – M D Feb 17 '15 at 10:09

4 Answers4

4

So this post suggested to use SimpleDateFormat instead of DateFormat. For more explanation about the differences between these two classes, you could take a look at this post.

Community
  • 1
  • 1
lgd
  • 1,172
  • 9
  • 10
2

According to the documentation SimpleDateFormat can handle the 'H' for 24h format but DateFormat needs 'k'.

Use a literal 'H' (for compatibility with SimpleDateFormat and Unicode) or 'k' (for compatibility with Android releases up to and including Jelly Bean MR-1) instead. Note that the two are incompatible.

Gorcyn
  • 2,807
  • 1
  • 20
  • 22
1

Try this,

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

TextView tv=(TextView)findViewById(R.id.textView);
tv.append("\n in 12-hour format: " +sdf);
tv.append("\n in 24 -hour format: " +sdf2);
Apurva
  • 7,871
  • 7
  • 40
  • 59
  • @MD I didn't know @heRoy has already told you in the comment to use `SimpleDateFormat` otherwise i wouldn't post the answer. May be was writing an answer that time. – Apurva Feb 17 '15 at 10:22
0

This works in all the devices,

static final SimpleDateFormat IN_DATE_FORMAT = new SimpleDateFormat("HH:mm:ss", Locale.US);
static final SimpleDateFormat OUT_DATE_FORMAT = new SimpleDateFormat("hh:mma", Locale.US);

/**
 * To format the time.
 *
 * @param strDate The input date in HH:mm:ss format.
 * @return The output date in hh:mma format.
 */
private String formatTime(String strDate) {
    try {
        Date date = IN_DATE_FORMAT.parse(strDate);
        return OUT_DATE_FORMAT.format(date);

    } catch (Exception e) {
        return "N/A";
    }
}

use the method : formatTime(YOUR_TIME)

dvs
  • 644
  • 5
  • 14