2017-08-31 476 views
-3

我试过这int newHoursValue =(now.get(Calendar.HOUR_OF_DAY); int newMintsValue = (now.get(Calendar.MINUTE);如何在android studio中分别获取小时和分钟?

但我没有得到实际的时间在几分钟内不匹配,它不能在24小时制。我想得到一个在24小时和12小时格式兼容的人。请帮忙。

+0

重复? https://stackoverflow.com/q/22525961/2655092 – IddoE

回答

1

HOUR_OF_DAY将返回小时,前导零,对于分钟您可以使用

String curTime = String.format("%02d:%02d", hrs, mnts); 

SimpleDateFormat df = new SimpleDateFormat("hh:mm"); 
0

//执行此操作。 。 。 日期currentDate = new Date();

/*for 24 hours format*/ 
    SimpleDateFormat simpleDate1 = new SimpleDateFormat("HH"); 
    String hours24 = simpleDate1.format(currentDate); 

    /*for 12 hours format*/ 
    SimpleDateFormat simpleDate2 = new SimpleDateFormat("hh"); 
    String hours12 = simpleDate2.format(currentDate); 

    /*for minutes*/ 
    SimpleDateFormat simpleDate3 = new SimpleDateFormat("mm"); 
    String minutes = simpleDate3.format(currentDate);