parse Youtube Date in yyyy-MM-dd HH:mm:ss format - Android java.util

Android examples for java.util:Date Format

Description

parse Youtube Date in yyyy-MM-dd HH:mm:ss format

Demo Code

import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static Date parseYoutubeDate(String rawDate) {
        final String TAG = "StringUtil.parseYoutubeDate";
        String pattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
        String minusTimezone = rawDate.substring(0, rawDate.indexOf('.'));
        String cleanSpecials = minusTimezone.replace('T', ' ');

        try {//from   ww w.  j  a  v  a2s  .co m
            return sdf.parse(cleanSpecials);
        } catch (ParseException pe) {
            Log.e(TAG, "Error while parsing date: " + cleanSpecials
                    + " with pattern: " + pattern, pe);
            return null;
        }
    }

}

Related Tutorials