有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

        object YouTubeDurationConverter {
        
            private fun getHours(time: String): Int {
                return time.substring(time.indexOf("T") + 1, time.indexOf("H")).toInt()
            }
        
            private fun getMinutes(time: String): Int {
                return time.substring(time.indexOf("H") + 1, time.indexOf("M")).toInt()
            }
        
            private fun getMinutesWithNoHours(time: String): Int {
                return time.substring(time.indexOf("T") + 1, time.indexOf("M")).toInt()
            }
        
            private fun getMinutesWithNoHoursAndNoSeconds(time: String): Int {
                return time.substring(time.indexOf("T") + 1, time.indexOf("M")).toInt()
            }
        
            private fun getSecondsOnly(time: String): Int {
                return time.substring(time.indexOf("T") + 1, time.indexOf("S")).toInt()
            }
        
            private fun getSecondsWithMinutes(time: String): Int {
                return time.substring(time.indexOf("M") + 1, time.indexOf("S")).toInt()
            }
        
            private fun getSecondsWithHours(time: String): Int {
                return time.substring(time.indexOf("H") + 1, time.indexOf("S")).toInt()
            }
        
            private fun convertToFormatedTime(time: String): FormatedTime {
        
                val HOURS_CONDITION = time.contains("H")
                val MINUTES_CONDITION = time.contains("M")
                val SECONDS_CONDITION = time.contains("S")
        
                /**
                 *potential cases
                 *hours only
                 *minutes only
                 *seconds only
                 *hours and minutes
                 *hours and seconds
                 *hours and minutes and seconds
                 *minutes and seconds
                 */
                val formatTime = FormatedTime(-1, -1, -1)
        
                if (time.equals("P0D")) {
                    //Live Video
                    return formatTime
                } else {
                    var hours = -1
                    var minutes = -1
                    var seconds = -1
        
                    if (HOURS_CONDITION && !MINUTES_CONDITION && !SECONDS_CONDITION) {
                        //hours only
                        hours = getHours(time)
                    } else if (!HOURS_CONDITION && MINUTES_CONDITION && !SECONDS_CONDITION) {
                        //minutes only
                        minutes = getMinutesWithNoHoursAndNoSeconds(time)
        
                    } else if (!HOURS_CONDITION && !MINUTES_CONDITION && SECONDS_CONDITION) {
                        //seconds only
                        seconds = getSecondsOnly(time)
        
        
                    } else if (HOURS_CONDITION && MINUTES_CONDITION && !SECONDS_CONDITION) {
                        //hours and minutes
                        hours = getHours(time)
                        minutes = getMinutes(time)
        
                    } else if (HOURS_CONDITION && !MINUTES_CONDITION && SECONDS_CONDITION) {
                        //hours and seconds
                        hours = getHours(time)
                        seconds = getSecondsWithHours(time)
        
        
                    } else if (HOURS_CONDITION && MINUTES_CONDITION && SECONDS_CONDITION) {
                        //hours and minutes and seconds
                        hours = getHours(time)
                        minutes = getMinutes(time)
                        seconds = getSecondsWithMinutes(time)
        
        
                    } else if (!HOURS_CONDITION && MINUTES_CONDITION && SECONDS_CONDITION) {
                        //minutes and seconds
                        minutes = getMinutesWithNoHours(time)
                        seconds = getSecondsWithMinutes(time)
        
                    }
                    return FormatedTime(hours, minutes, seconds)
                }
            }
        
            fun getTimeInStringFormated(time: String): String {
                val formatedTime = convertToFormatedTime(time)
                val timeFormate: StringBuilder = StringBuilder("")
        
                if (formatedTime.hour == -1) {
                    timeFormate.append("00:")
                } else if (formatedTime.hour.toString().length == 1) {
                    timeFormate.append("0" + (formatedTime.hour).toString() + ":")
                } else {
                    timeFormate.append((formatedTime.hour).toString() + ":")
                }
                if (formatedTime.minutes == -1) {
                    timeFormate.append("00:")
                } else if (formatedTime.minutes.toString().length == 1) {
                    timeFormate.append("0" + (formatedTime.minutes).toString() + ":")
                } else {
                    timeFormate.append((formatedTime.minutes).toString() + ":")
                }
        
                if (formatedTime.second == -1) {
                    timeFormate.append("00")
                } else if (formatedTime.second.toString().length == 1) {
                    timeFormate.append("0" + (formatedTime.second).toString())
                } else {
                    timeFormate.append(formatedTime.second)
                }
                return timeFormate.toString()
            }
        
            fun getTimeInSeconds(time: String): Int {
        
                val formatedTime = convertToFormatedTime(time)
                var tottalTimeInSeconds = 0
                if (formatedTime.hour != -1) {
                    tottalTimeInSeconds += (formatedTime.hour * 60 * 60)
                }
                if (formatedTime.minutes != -1) {
                    tottalTimeInSeconds += (formatedTime.minutes * 60)
                }
                if (formatedTime.second != -1) {
                    tottalTimeInSeconds += (formatedTime.second)
        
                }
                return tottalTimeInSeconds
            }
            fun getTimeInMillis(time: String):Long
            {
                val timeInSeconds = getTimeInSeconds(time)
                return (timeInSeconds*1000).toLong()
            }}
        
        
        data class FormatedTime (var hour:Int,var minutes:Int,var second:Int)
        
         
    
        Output Sample
        1.getTimeInStringFormated: 11:54:48
    2.getTimeInSeconds: 42888
    3.getTimeInMillis: 42888000
  2. # 2 楼答案

    您可以使用^{},它是以ISO-8601 standards为模型的,是作为JSR-310 implementation的一部分随Java-8引入的。通过Java-9引入了一些更方便的方法

    如果您浏览了以上链接,您可能已经注意到PT18M8S指定了一个18分8秒的持续时间,您可以将其解析为Duration对象,然后从这个对象中,您可以根据需要创建一个格式为天、小时、分钟、秒的字符串

    演示:

    import java.time.Duration;
    
    public class Main {
        public static void main(String[] args) {
            String strIso8601Duration = "PT18M8S";
    
            Duration duration = Duration.parse(strIso8601Duration);
            // Default format
            System.out.println(duration);
    
            // Custom format
            // ####################################Java-8####################################
            String formattedElapsedTime = String.format("%02d:%02d:%02d", duration.toHours() % 24,
                    duration.toMinutes() % 60, duration.toSeconds() % 60);
            System.out.println(formattedElapsedTime);
            // ##############################################################################
    
            // ####################################Java-9####################################
            formattedElapsedTime = String.format("%02d:%02d:%02d", duration.toHoursPart(), duration.toMinutesPart(),
                    duration.toSecondsPart());
            System.out.println(formattedElapsedTime);
            // ##############################################################################
    
            System.out.println(duration.toMillis() + " milliseconds");
        }
    }
    

    输出:

    PT18M8S
    00:18:08
    00:18:08
    1088000 milliseconds
    

    Trail: Date Time了解现代日期时间API