【问题标题】:How to read the duration of a video returned from Youtube API Kotlin/Java如何读取从 Youtube API Kotlin/Java 返回的视频的持续时间
【发布时间】:2021-04-08 08:42:25
【问题描述】:

我基本上想将此 ISO 格式 PT18M8S 转换为 13:0910000ms 我从 YouTube Data API v3 获得这种格式:

【问题讨论】:

  • 转换或理解 ISO 格式的问题在哪里?
  • 问题出在转换上,但现在我找到了解决方案,谢谢。

标签: java date kotlin format youtube-api


【解决方案1】:

您可以使用java.time.Duration,它以ISO-8601 standards 为蓝本,并作为JSR-310 implementation 的一部分引入Java-8Java-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。

【讨论】:

    【解决方案2】:
        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)
        
         
    
    输出样本 1.getTimeInStringFormated: 11:54:48
    2.getTimeInSeconds:42888
    3.getTimeInMillis: 42888000

    【讨论】:

      猜你喜欢
      • 2017-07-14
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 2021-01-26
      • 2015-07-20
      • 2014-08-15
      • 2016-06-06
      相关资源
      最近更新 更多