SimpleDateFormat的返回24小时日期:如何获得12个小时的时间?小时、如何获得、日期、时间

2023-09-04 11:00:20 作者:15.久奈樱子

我想当前时间米利斯,然后将其存储在12个小时的格式,但与这片code我越来越24小时格式的时间。

 长timeInMillis = System.currentTimeMillis的();
日历吹响--- = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat的日期格式=新的SimpleDateFormat(DD / MM / YYYY HH:MM:SS);
dateforrow = dateFormat.format(cal1.getTime());
 

任何人可以提出修改建议,以获得想要的结果?

解决方案

修改 HH HH

 长timeInMillis = System.currentTimeMillis的();
日历吹响--- = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat的日期格式=新的SimpleDateFormat(
                                DD / MM / YYYY HH:MM:SS);
dateforrow = dateFormat.format(cal1.getTime());
 

注意 DD / MM / YYYY - 会给你的分的代替的月的Java 格式化时间 SimpleDateFormat

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?

解决方案

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.