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

前端 未结 9 2378
一生所求
一生所求 2020-12-08 06:41

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         


        
9条回答
  •  时光取名叫无心
    2020-12-08 06:55

    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.

提交回复
热议问题