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?

  • HAX kernel module is not installed
  • What document is Play Store expecting me to upload to show that I'm not impersonating others?
  • Are Custom Objects Passed by value or passed by Reference in Fragments and Activities.?
  • Android - google map move camera from position to another
  • ExoPlayer AudioTrack Stuttering
  • FragmentManager popBackStack doesn't remove fragment
  • Related posts:

    SpeechRecognizer on Android Wear
    how to center icons in toolbar in android studio
    Create Eclipse Project from Android Git
    Advantages of using Bundle instead of direct Intent putExtra() in Android
    How to programmatically check availibilty of internet connection in Android?
    AccountManager does not add custom account in Android N preview
  • Making facebook login work with an Android Webview
  • Android BlurMaskFilter has no effect in canvas.drawOval while text is blurred
  • Rotate an ImageView clockwise using ObjectAnimator
  • Run Android app in debugging mode
  • How to add BackStackEntry to BackStack if not exists
  • Reset Android textview maxlines
  • 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.