在Youtube中将Youtube Data API V3视频持续时间转换为hh:mm:ss格式?

时间:2014-06-06 09:36:51

标签: java regex youtube-data-api

我正在使用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 - >一点00分01秒
  6. PT1H1M1S - > 1点01分01秒
  7. 我可以使用Joda Time library来解析值并以秒为单位计算持续时间,但是库的大小为500kb,这会增加我不想要的应用程序的大小。

4 个答案:

答案 0 :(得分:4)

看看这段代码:

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个案例和适当的正则表达式替换。

答案 1 :(得分:4)

我自己做了

试试

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 ;
    }
}

答案 2 :(得分:1)

也必须处理这个问题。我必须将长度转换为毫秒,但是一旦你填充了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); 

答案 3 :(得分:0)

我需要一个包含所有这些转换后持续时间的数组。所以我在下面写了一个变通方法,并且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","");




                                }
                            }

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

我认为现在应该进行一些编辑了。