【问题标题】:Converting Youtube Data API V3 video duration to hh:mm:ss format in java?在 java 中将 Youtube Data API V3 视频持续时间转换为 hh:mm:ss 格式?
【发布时间】:2014-06-06 09:36:51
【问题描述】:

我正在使用 Youtube 数据 api v3 来获取视频信息,例如标题、观看次数和持续时间。持续时间值对我来说是新的,因为它是 ISO8601 date,我需要将其转换为可读格式,例如 hh:mm:ss .持续时间可以有以下不同的值:

  1. PT1S --> 00:01
  2. PT1M --> 01:00
  3. PT1H --> 01:00:00
  4. PT1M1S --> 01:01
  5. PT1H1S --> 01:00:01
  6. PT1H1M1S --> 01:01:01

我可以使用 Joda Time library 解析值并计算持续时间(以秒为单位),但库大小为 500kb,这会增加我不想要的应用程序的大小。

【问题讨论】:

标签: java regex youtube-data-api


【解决方案1】:

看看这段代码:

private static HashMap<String, String> regexMap = new HashMap<>();
private static String regex2two = "(?<=[^\\d])(\\d)(?=[^\\d])";
private static String two = "0$1";

public static void main(String[] args) {

    regexMap.put("PT(\\d\\d)S", "00:$1");
    regexMap.put("PT(\\d\\d)M", "$1:00");
    regexMap.put("PT(\\d\\d)H", "$1:00:00");
    regexMap.put("PT(\\d\\d)M(\\d\\d)S", "$1:$2");
    regexMap.put("PT(\\d\\d)H(\\d\\d)S", "$1:00:$2");
    regexMap.put("PT(\\d\\d)H(\\d\\d)M", "$1:$2:00");
    regexMap.put("PT(\\d\\d)H(\\d\\d)M(\\d\\d)S", "$1:$2:$3");

    String[] dates = { "PT1S", "PT1M", "PT1H", "PT1M1S", "PT1H1S", "PT1H1M", "PT1H1M1S", "PT10H1M13S", "PT10H1S", "PT1M11S" };

    for (String date : dates) {
        String d = date.replaceAll(regex2two, two);
        String regex = getRegex(d);
        if (regex == null) {
            System.out.println(d + ": invalid");
            continue;
        }
        String newDate = d.replaceAll(regex, regexMap.get(regex));
        System.out.println(date + " : " +newDate);
    }    
}

private static String getRegex(String date) {
    for (String r : regexMap.keySet())
        if (Pattern.matches(r, date))
            return r;
    return null;
}

regex2two 已用于将前导零0 添加到 1 位数字。你可以试试这个demo

regexMap 中,我存储了所有 7 个案例和适当的正则表达式替换。

【讨论】:

  • 这是最好的解决方案,到目前为止:)
【解决方案2】:

我自己做的

我们试试

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.

public class YouTubeDurationUtils {
    /**
     * 
     * @param duration
     * @return "01:02:30"
     */
    public static String convertYouTubeDuration(String duration) {
        String youtubeDuration = duration; //"PT1H2M30S"; // "PT1M13S";
        Calendar c = new GregorianCalendar();
        try {
            DateFormat df = new SimpleDateFormat("'PT'mm'M'ss'S'");
            Date d = df.parse(youtubeDuration);
            c.setTime(d);
        } catch (ParseException e) {
            try {
                DateFormat df = new SimpleDateFormat("'PT'hh'H'mm'M'ss'S'");
                Date d = df.parse(youtubeDuration);
                c.setTime(d);
            } catch (ParseException e1) {
                try {
                    DateFormat df = new SimpleDateFormat("'PT'ss'S'");
                    Date d = df.parse(youtubeDuration);
                    c.setTime(d);
                } catch (ParseException e2) {
                }
            }
        }
        c.setTimeZone(TimeZone.getDefault());

        String time = "";
        if ( c.get(Calendar.HOUR) > 0 ) {
            if ( String.valueOf(c.get(Calendar.HOUR)).length() == 1 ) {
                time += "0" + c.get(Calendar.HOUR);
            }
            else {
                time += c.get(Calendar.HOUR);
            }
            time += ":";
        }
        // test minute
        if ( String.valueOf(c.get(Calendar.MINUTE)).length() == 1 ) {
            time += "0" + c.get(Calendar.MINUTE);
        }
        else {
            time += c.get(Calendar.MINUTE);
        }
        time += ":";
        // test second
        if ( String.valueOf(c.get(Calendar.SECOND)).length() == 1 ) {
            time += "0" + c.get(Calendar.SECOND);
        }
        else {
            time += c.get(Calendar.SECOND);
        }
        return time ;
    }
}

【讨论】:

    【解决方案3】:

    也必须处理这个问题。我必须将长度转换为毫秒,但是一旦您填充了 secs/mins/hours 变量,您就可以转换为您想要的任何格式:

    // Test Value
    $vidLength = 'PT1H23M45S';
    
    $secs = '';
    $mins = '';
    $hours = '';
    $inspecting = '';
    for($i=(strlen($vidLength)-1); $i>0; $i--){
        if(is_numeric($vidLength[$i])){
            if($inspecting == 'S'){
                $secs = $vidLength[$i].$secs;
            }
            else if($inspecting == 'M'){
                $mins = $vidLength[$i].$mins;
            }
            else if($inspecting == 'H'){
                $hours = $vidLength[$i].$hours;
            }
        }
        else {
            $inspecting = $vidLength[$i];
        }
    }
    
    $lengthInMS = 1000*(($hours*60*60) + ($mins*60) + $secs); 
    

    【讨论】:

      【解决方案4】:

      我需要一个包含所有这些转换后的持续时间的数组。所以我写了以下作为一种解决方法,而且 java.time.duration 对我不起作用,不知道为什么。

      String[] D_uration = new String[10];
      while(iteratorSearchResults.hasNext()){String Apiduration1=Apiduration.replace("PT","");
                                  if(Apiduration.indexOf("H")>=0){
                                      String Apiduration2=Apiduration1.replace("H",":");
                                      if(Apiduration.indexOf("M")>=0){
                                          String Apiduration3=Apiduration2.replace("M",":");
                                          if(Apiduration.indexOf("S")>=0){
                                              D_uration[i]=Apiduration3.replace("S","");
      
      
                                          }
                                          else{
                                              String Apiduration4=Apiduration2.replace("M",":00");
                                              D_uration[i]=Apiduration4;
                                          }
                                      }
                                      else{
                                          String Apiduration4=Apiduration2.replace(":",":00:");
                                          if(Apiduration.indexOf("S")>=0){
                                              D_uration[i]=Apiduration4.replace("S","");
      
      
                                          }
                                          else{
                                              String Apiduration3=Apiduration4.replace(":00:",":00:00");
                                              D_uration[i]=Apiduration3;
                                          }
                                      }
                                  }
                                  else{
      
                                      if(Apiduration.indexOf("M")>=0){
                                          String Apiduration2=Apiduration1.replace("M",":");
                                          if(Apiduration.indexOf("S")>=0){
                                              D_uration[i]=Apiduration2.replace("S","");
      
      
                                          }
                                          else{
                                              String Apiduration4=Apiduration2.replace(":",":00");
                                              D_uration[i]=Apiduration4;
                                          }
      
                                      }
                                      else{
      
                                          D_uration[i]=Apiduration1.replace("S","");
      
      
      
      
                                      }
                                  }
      

      “Apiduration”由 Youtube 数据 Api 以 ISO8601 格式返回。

      现在做了一些修改,我认为它应该可以正常工作。

      【讨论】:

        猜你喜欢
        • 2021-08-05
        • 2014-04-04
        • 2013-11-02
        • 2014-08-15
        • 2013-05-24
        • 2015-08-24
        • 2017-12-01
        • 2015-10-03
        • 2016-06-06
        相关资源
        最近更新 更多