SimpleDateFormat returns 24-hour date: how to get 12-hour date?

I want current time in millis and then to store it in 12 hour format but with this piece of code I am getting 24 hour format time.

long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy HH:mm:ss a");
dateforrow = dateFormat.format(cal1.getTime());

can anybody suggest modifications to get the desired results?

  • Preview callback in Camera2 is significantly slower than in Camera1
  • CollapsingToolbarLayout doesn't recognize scroll fling
  • Android ArrayList<MyObject> pass as parcelable
  • Conditionally set first activity in Android
  • Android Studio 2.2 update: aligned APK (zipAlign) not generated using the new Gradle Plugin 2.2.0
  • Google Play services out of date. Requires 10084000 but found 9879470. Can't update
  • Getting current Year and Month resulting strange results
  • how to create a new AVD in eclipse?
  • How to center progress indicator in ProgressDialog easily (when no title/text passed along)
  • How to add apache commons collections in Android Studio (Gradle)
  • Android Studio vs Eclipse + ADT Plugin?
  • Android studio is not working on windows 7
  • 8 Solutions collect form web for “SimpleDateFormat returns 24-hour date: how to get 12-hour date?”

    Change HH to hh as

    long timeInMillis = System.currentTimeMillis();
    Calendar cal1 = Calendar.getInstance();
    cal1.setTimeInMillis(timeInMillis);
    SimpleDateFormat dateFormat = new SimpleDateFormat(
                                    "dd/MM/yyyy hh:mm:ss a");
    dateforrow = dateFormat.format(cal1.getTime());
    

    Note that dd/mm/yyyy – will give you minutes instead of the month.

    Referring to SimpleDataFormat JavaDoc:

    Letter | Date or Time Component | Presentation | Examples
    ---------------------------------------------------------
       H   |  Hour in day (0-23)    |    Number    |    0
       h   |  Hour in am/pm (1-12)  |    Number    |    12
    

    Hi I tested below code that worked fine :

        long timeInMillis = System.currentTimeMillis();
        Calendar cal1 = Calendar.getInstance();
        cal1.setTimeInMillis(timeInMillis);
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss a");
        dateFormat.format(cal1.getTime());
    

    I re-encounter this in the hard way as well. H vs h, for 24-hour vs 12 hour !

    Yep, confirmed that simply using hh instead of HH fixed my issue.

    Changed from this:

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm aa");
    

    To this:

    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
    

    You can still use HH to store the time if you don’t want to bother storing and dealing with the AM/PM. Then when you retrieve it, use hh.

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss a");
    

    use hh in place of HH

    You can try it like this

      Calendar c= Calendar.getInstance();
    
      SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
      String str=sdf.format(c.getTime());
    

    See code example below:

    SimpleDateFormat df = new SimpleDateFormat("hh:mm");
    String formattedDate = df.format(new Date());
    out.println(formattedDate);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.